Skip to content

Instantly share code, notes, and snippets.

@ngocvantran
Created July 10, 2016 10:58
Show Gist options
  • Save ngocvantran/99c72117a836daa516137601f296d884 to your computer and use it in GitHub Desktop.
Save ngocvantran/99c72117a836daa516137601f296d884 to your computer and use it in GitHub Desktop.
Extract translation texts from aurelia-i18n
"use strict";
const fs = require('fs');
const path = require('path');
const readdirp = require('readdirp');
const Parser = require('i18next-scanner').Parser;
const WebpackPreBuildPlugin = require('pre-build-webpack');
const {i18nextToPo, i18nextToPot} = require('i18next-conv');
const parseConfig = {
attr: {
list: ['t']
},
func: {
list: ['i18next.t', 'i18n.t']
},
nsSeparator: false,
keySeparator: false,
defaultNs: 'translation',
interpolation: {
prefix: '${',
suffix: '}'
},
};
const convertConfig = {
language: 'en',
project: 'aurelia-skeleton-navigation-webpack',
};
// charsMap is not needed if you do not use pseudo translation
const charsMap = {
'a': 'ààà', 'b': 'ƀ', 'c': 'ç', 'd': 'ð', 'e': 'ééé', 'f': 'ƒ', 'g': 'ĝ', 'h': 'ĥ', 'i': 'îîî', 'l': 'ļ', 'k': 'ķ', 'j': 'ĵ', 'm': 'ɱ',
'n': 'ñ', 'o': 'ôôô', 'p': 'þ', 'q': 'ǫ', 'r': 'ŕ', 's': 'š', 't': 'ţ', 'u': 'ûûû', 'v': 'ṽ', 'w': 'ŵ', 'x': 'ẋ', 'y': 'ý', 'z': 'ž',
'A': 'ÀÀÀ', 'B': 'Ɓ', 'C': 'Ç', 'D': 'Ð', 'E': 'ÉÉÉ', 'F': 'Ƒ', 'G': 'Ĝ', 'H': 'Ĥ', 'I': 'ÎÎÎ', 'L': 'Ļ', 'K': 'Ķ', 'J': 'Ĵ', 'M': 'Ṁ',
'N': 'Ñ', 'O': 'ÔÔÔ', 'P': 'Þ', 'Q': 'Ǫ', 'R': 'Ŕ', 'S': 'Š', 'T': 'Ţ', 'U': 'ÛÛÛ', 'V': 'Ṽ', 'W': 'Ŵ', 'X': 'Ẋ', 'Y': 'Ý', 'Z': 'Ž'
};
module.exports = new WebpackPreBuildPlugin(function() {
let parser = new Parser(parseConfig);
readdirp({
root: 'src',
entryType: 'files'
}, function(file) {
let fullPath = file.fullPath;
let extension = path.extname(fullPath).toLowerCase();
switch (extension) {
case '.html':
case '.htm':
let template = fs.readFileSync(fullPath, 'utf-8');
parser.parseAttrFromString(template);
break;
case '.js':
let script = fs.readFileSync(fullPath, 'utf-8');
parser.parseFuncFromString(script);
break;
}
}, function() {
let translations = parser.get().en.translation;
delete translations[''];
// Ensure translations folder exists
if (!fs.existsSync('translations'))
fs.mkdirSync('translations');
// POT file
i18nextToPot('en', JSON.stringify(translations), convertConfig)
.then(function(result) {
fs.writeFileSync('translations/translation.pot', result)
});
// End of extraction, remove the rest if you do not need pseudo translation
// Pseudo translation
Object.keys(translations).forEach(function(key) {
let pseudo = '';
for (let i = 0; i < key.length; i++) {
let alternatives = charsMap[key[i]];
if (!alternatives) {
pseudo += key[i];
continue;
}
pseudo += alternatives[i % alternatives.length];
}
translations[key] = pseudo;
});
// Pseudo json file
fs.writeFileSync('translations/translation.qps-PLOC.json',
JSON.stringify({'qps-ploc': translations}, null, 2));
});
});
const translate = require('./translate');
const baseConfig = {
// Webpack config here
plugins: [
translate,
// Other webpack plugins, if needed here
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment