* Transifex transition

Scripts and configuration for transition to Transifex for translation.

* Transifex transition update

changed the name of the place where translations are saved from ‘translations’ to localizations so that we can have a consistent name in both scratchr2, and scratch-www.

A couple of other little clean-ups to make sure that it’s ES5 compliant.
- don’t use const
- don’t use template strings (backticks)
This commit is contained in:
chrisgarrity 2017-01-18 12:57:14 -05:00 committed by GitHub
parent 0b857892d2
commit 9e4ef5fb1b
5 changed files with 336 additions and 0 deletions

View file

@ -62,6 +62,22 @@ Helpers.mergeNewTranslations = function (existingTranslations, newTranslations,
return existingTranslations;
};
Helpers.mergeNewTranslationsWithoutDefaults = function (existingTranslations, newTranslations, icuTemplate, md5Map) {
for (var id in newTranslations) {
var md5 = Helpers.getMD5(id);
if (md5Map.hasOwnProperty(md5) && newTranslations[id].length > 0) {
md5Map[md5].forEach(function (msgId) {
existingTranslations[msgId] = newTranslations[id];
});
}
}
//Fill in empty string for any missing translations
for (var icuId in icuTemplate) {
if (!existingTranslations.hasOwnProperty(icuId)) existingTranslations[icuId] = '';
}
return existingTranslations;
};
/**
* Converts a map of icu strings with react-intl id values into a map
* with md5 hashes of the icu strings as keys and react-intl id values.
@ -139,6 +155,65 @@ Helpers.getTranslationsForLanguage = function (lang, idsWithICU, md5WithIds, sep
return translationsByView;
};
/**
* Grabs the translated strings from the po files for the given language and strings
* @param {str} lang iso code of the language to use
* @param {object} idsWithICU key: '<viewName>-<react-intl string id>'.
* value: english strings for translation
* @param {object} md5WithIds key: md5 hash of the english strings for translation.
* value: '<viewName>-<react-intl string id>'
* @return {object} translations sub-objects by view containing:
* key: '<react-intl string id>'
* value: translated version of string
* empty if no translation present
*/
Helpers.getTranslationsForLanguageWithoutDefaults = function (lang, idsWithICU, md5WithIds, separator) {
var poUiDir = path.resolve(__dirname, '../../node_modules/scratchr2_translations/ui');
var jsFile = path.resolve(poUiDir, lang + '/LC_MESSAGES/djangojs.po');
var pyFile = path.resolve(poUiDir, lang + '/LC_MESSAGES/django.po');
var translations = {};
separator = separator || ':';
try {
fs.accessSync(jsFile, fs.R_OK);
var jsTranslations = po2icu.poFileToICUSync(lang, jsFile);
translations = Helpers.mergeNewTranslationsWithoutDefaults(translations,
jsTranslations, idsWithICU, md5WithIds);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
try {
fs.accessSync(pyFile, fs.R_OK);
var pyTranslations = po2icu.poFileToICUSync(lang, pyFile);
translations = Helpers.mergeNewTranslationsWithoutDefaults(translations,
pyTranslations, idsWithICU, md5WithIds);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
var translationsByView = {};
for (var id in translations) {
var ids = id.split(separator); // [viewName, stringId]
var viewName = ids[0];
var stringId = ids[1];
if (!translationsByView.hasOwnProperty(viewName)) {
translationsByView[viewName] = {};
}
if (!translationsByView[viewName].hasOwnProperty(lang)) {
translationsByView[viewName][lang] = {};
}
translationsByView[viewName][lang][stringId] = translations[id];
}
return translationsByView;
};
Helpers.writeTranslationsToJS = function (outputDir, viewName, translationObject) {
var objectString = JSON.stringify(translationObject);
var fileString = 'window._messages = ' + objectString + ';';
@ -169,4 +244,30 @@ Helpers.icuToIdMap = function (viewName, ids, separator) {
return icuToIds;
};
Helpers.writeTranslations = function (name, l10n, languages) {
var ids = require(l10n);
var idsWithICU = Helpers.idToICUMap(name, ids);
var icuWithIds = Helpers.icuToIdMap(name, ids);
var md5WithIds = Helpers.getMD5Map(icuWithIds);
var isoCodes = Object.keys(languages);
var outputDir = path.resolve(__dirname, '../../localizations/', name);
try {
fs.accessSync(outputDir, fs.F_OK);
} catch (err) {
// Doesn't exist - create it.
fs.mkdirSync(outputDir);
}
process.stdout.write(`Writing translations to ${outputDir}\n`);
for (var isoCode in isoCodes) {
if (isoCodes[isoCode] !== 'en'){
var translations = Helpers.getTranslationsForLanguageWithoutDefaults(
isoCodes[isoCode], idsWithICU, md5WithIds);
var fileString = JSON.stringify(translations[name][isoCodes[isoCode]]);
fs.writeFileSync(outputDir + '/' + isoCodes[isoCode] + '.json', fileString);
}
}
};
module.exports = Helpers;