adamduncan / eleventy-plugin-i18n

Eleventy plugin to assist with internationalization and dictionary translations
MIT License
104 stars 10 forks source link

po files #1

Open hankhero opened 4 years ago

hankhero commented 4 years ago

I am setting up a site with this way, however I wanted to make sure to use a file format for translations that can be sent to translators, or used with standard translation tools. Found out that .po file seems common, also it is an easy text format. So: I have an example, basic code. Wonder if it should be cleaned up and added to the project, or maybe be an example? It introduces one dependency on a po file parser.

This is how it works:

Mty sample now have english and spanish. I make two .po files (en.po and es.po), add in a folder somewhere, in my case src/developer/_translations (src/developer is my input directory). This is how the spanish .po file looks like (es.po):

# Main page
#, fuzzy
msgid "hello"
msgstr "Hola"

msgid "world"
msgstr "mundo"

Added dependency: npm install gettext-parser --save-dev

And in .eleventy.js this is added. Yes can be cleaned up. And could potentially be a made into a patch, but first I want to hear what you think....

const i18n = require('eleventy-plugin-i18n');

const fs = require('fs');
const gettextParser = require("gettext-parser");

function getTranslations() {
    const baseDir = './src/developer/_translations'

    const locales = ['en-US', 'es-ES']

    var translations = {}

    const loadLocale = (locale) => {

        const getTranForLang = (lang) => {
            var poFile = baseDir + "/" + lang + ".po";
            var input = fs.readFileSync(poFile);
            var po = gettextParser.po.parse(input);
            var poTranslations = po.translations['']; // output translations for the default context
//            console.log(poTranslations);
            return poTranslations
        }
        const addPoTranslations = (poTranslations) => {
            for (var key in poTranslations) {
                var poEntry = poTranslations[key]
                var msgid = poEntry.msgid
                var msgstr = poEntry.msgstr[0]

                var obj = translations[msgid] || {};
                obj[locale] = msgstr
                translations[msgid] = obj
            }
        }
        const lang = locale.substr(0,2);

        const poTranslations = getTranForLang(lang)
        addPoTranslations(poTranslations)
    }

    locales.forEach(loadLocale)

    console.log("Translations")
    console.log(translations)

    return translations;
}

module.exports = function(eleventyConfig) {

    var translations = getTranslations()
        // translations: {
        //     hello: {
        //         'en-US': 'Hello',
        //         'es-ES': 'Hola'
        //     }
        // },

    eleventyConfig.addPlugin(i18n, {
        translations: translations,
        fallbackLocales: {
            '*': 'en-US'
        }
    });
adamduncan commented 4 years ago

Hey @hankhero, thanks for taking the time to write this up. Definitely think that enabling plugin users the ability to work from PO files is a great idea.

Perhaps this could be a helper function exported from the plugin? I'll take some time to explore this too, and we'll see where we can get it to.

adamduncan commented 4 years ago

I've not had time to dig back into this, but have spotted https://github.com/sgissinger/eleventy-plugin-i18n-gettext/, which might offer more of what you're after 🙂