Merge "Grunt task for validating i18next translations"

This commit is contained in:
Jenkins 2014-01-29 10:24:08 +00:00 committed by Gerrit Code Review
commit 1ec98d0b56
4 changed files with 110 additions and 1 deletions

View File

@ -19,3 +19,17 @@ If you want to add new strings to the translations file, follow these rules:
#. If some keys are used in a few places (for example, in utils), move them to
"common.*" namespace.
#. Use defaultValue ONLY with dynamically generated keys.
Validating translations
=========================================
To search for absent and unnecessary translation keys you can perform the following steps:
#. Open terminal and cd to fuel-web/nailgun directory.
#. Run "grunt i18n:validate" to start the validation.
If there are any mismatches, you'll see the list of mismatching keys.
Grunt task "i18n:validate" has one optional argument - a comma-separated list of
languages to compare with base English en-US translations. Run
"grunt i18n:validate:zh-CN" to perform comparison only between English and
Chinese keys. You can also run "grunt i18n:validate:zh-CN,ru-RU" to perform
comparison between English-Chinese and English-Russian keys.

View File

@ -91,6 +91,8 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-jslint');
grunt.loadNpmTasks('grunt-bower-task');
grunt.loadNpmTasks('grunt-debug-task');
grunt.registerTask('build', ['bower', 'less', 'requirejs']);
grunt.registerTask('default', ['build']);
grunt.task.loadTasks('grunt');
};

90
nailgun/grunt/i18n.js Normal file
View File

@ -0,0 +1,90 @@
'use strict';
module.exports = function(grunt) {
var _ = require('lodash-node');
grunt.registerTask('i18n', 'Search for missing keys from different locales in translation.json', function(task, param) {
if (task == 'validate') {
startValidationTask(param);
}
function startValidationTask(language) {
var file = 'static/i18n/translation.json',
globalValues = {},
fileContents = grunt.file.readJSON(file),
existingTranslationLanguages = _.keys(fileContents),
optionsLang = _.isUndefined(language) ? existingTranslationLanguages : (_.indexOf(language,',') > 0) ? language.split(',') : language;
globalValues.baseLang = 'en-US';
if (_.isArray(optionsLang)) {
_.each(optionsLang, function(lang){
initializeLanguage(lang, fileContents);
});
}
else {
initializeLanguage(optionsLang, fileContents);
}
function initializeLanguage(language, fileContent) {
var englishTranslations,
comparingTranslations;
if (_.indexOf(_.keys(fileContent), language) < 0) {
grunt.log.errorlns('No language named ' + language + ' found!');
}
else {
englishTranslations = _.first(_.pluck(_.pick(fileContent, globalValues.baseLang), 'translation'));
comparingTranslations = _.first(_.pluck(_.pick(fileContent, language), 'translation'));
globalValues.languageToCompareToEnglish = language;
globalValues.viceVersaComparison = false;
initializeForCalculation(englishTranslations, comparingTranslations);
globalValues.viceVersaComparison = true;
initializeForCalculation(comparingTranslations, englishTranslations);
}
}
function initializeForCalculation(obj1, obj2) {
globalValues.stackedKeys = [];
globalValues.arrayToCompareWith = obj2;
globalValues.missingKeys = [];
globalValues.currentDepth = globalValues.arrayToCompareWith;
compare(obj1);
if (globalValues.missingKeys.length) displayMissingKeys();
}
function compare(obj) {
_.each(obj, function (value, key) {
if (!_.isArray(value)) {
if (!_.contains(_.keys(getLastObject()), key)) {
globalValues.missingKeys.push(globalValues.stackedKeys.join('.')+'.'+key);
}
else {
if (_.isObject(value)) {
globalValues.stackedKeys.push(key);
compare(value);
globalValues.stackedKeys.pop();
}
}
}
});
}
function getLastObject() {
var temp = globalValues.arrayToCompareWith;
_.each(globalValues.stackedKeys, function (elem) {
temp = temp[elem];
}, this);
return temp;
}
function displayMissingKeys() {
grunt.log.writeln();
(globalValues.viceVersaComparison)
? grunt.log.errorlns('The list of keys present in ' + globalValues.languageToCompareToEnglish + ' but absent in '+ globalValues.baseLang + ':')
: grunt.log.errorlns('The list of keys missing in ' + globalValues.languageToCompareToEnglish + ':');
_.each(globalValues.missingKeys, function(elem) {
grunt.log.writeln(elem);
});
}
}
});
};

View File

@ -15,6 +15,9 @@
"grunt-contrib-requirejs": "~0.4.1",
"grunt-contrib-less": "~0.8.2",
"grunt-bower": "~0.7.0",
"grunt-bower-task": "~0.3.4"
"grunt-bower-task": "~0.3.4",
"grunt-debug-task": "~0.1.3",
"lodash-node": "~2.4.1",
"i18n": "grunt/i18n.js"
}
}