Skip to content

Commit

Permalink
applyBabelJob: Discard plural forms not used by a locale, and emit a …
Browse files Browse the repository at this point in the history
…warning.
  • Loading branch information
papandreou committed Apr 9, 2015
1 parent fc5e9cc commit 2e5c729
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
37 changes: 37 additions & 0 deletions bin/applyBabelJob
Expand Up @@ -7,6 +7,7 @@ var path = require('path'),
extend = require('extend'),
AssetGraph = require('../lib/AssetGraph'),
i18nTools = require('../lib/i18nTools'),
pluralsCldr = require('plurals-cldr'),
urlTools = require('urltools'),
commandLineOptions = require('optimist')
.usage('$0 [--label <labelName>=<dir> ...] [--parentdir] [--i18n <pathToI18nFile>] [--locales <localeId>[,<localeId>...]] [--defaultlocale <localeId>] --babeldir=<dirContainingTheBabelFilesToApply> --root <inputRootDirectory> <htmlFile>...')
Expand Down Expand Up @@ -175,6 +176,42 @@ new AssetGraph({root: commandLineOptions.root})
}).filter(function (asset) {
return asset !== i18nAssetForAllKeys;
});

Object.keys(translationsByKeyAndLocaleId).forEach(function (key) {
var translationsByLocaleId = translationsByKeyAndLocaleId[key];
Object.keys(translationsByLocaleId).forEach(function (localeId) {
var value = translationsByLocaleId[localeId];
// Mostly copied from coalescePluralsToLocale
translationsByLocaleId[localeId] = (function traverse(obj) {
if (Array.isArray(obj)) {
return obj.map(traverse);
} else if (typeof obj === 'object' && obj !== null) {
var coalescedObj = {},
keys = Object.keys(obj);
if (keys.length > 0 && keys.every(function (key) {
return ['zero', 'one', 'two', 'few', 'many', 'other'].indexOf(key) !== -1;
})) {
keys = [];
pluralsCldr.forms(localeId).forEach(function (pluralForm) {
coalescedObj[pluralForm] = obj[pluralForm];
keys.push(pluralForm);
});
if (Object.keys(obj).length > keys.length) {
console.log(key + ': Discarding plural forms not used in ' + localeId + ':', obj, '=>', coalescedObj);
}
obj = coalescedObj;
}
keys.forEach(function (propertyName) {
coalescedObj[propertyName] = traverse(obj[propertyName]);
});
return coalescedObj;
} else {
return obj;
}
}(value));
});
});

Object.keys(translationsByKeyAndLocaleId).forEach(function (key) {
var i18nAsset;
if (i18nAssetForAllKeys) {
Expand Down
58 changes: 58 additions & 0 deletions test/bin/applyBabelJob.js
Expand Up @@ -96,4 +96,62 @@ describe('applyBabelJob', function () {
});
});
});

it('should warn about and discard plural cases not supported by a locale', function (done) {
var babelDir = Path.resolve(__dirname, '..', '..', 'testdata', 'bin', 'applyBabelJob', 'invalidPlurals', 'translationjob'),
tmpTestCaseCopyDir = temp.mkdirSync(),
copyCommand = 'cp \'' + __dirname + '/../../testdata/bin/applyBabelJob\'/invalidPlurals/index.* ' + tmpTestCaseCopyDir;
childProcess.exec(copyCommand, function (err, stdout, stderr) {
if (err) {
return done(new Error(copyCommand + ' failed: STDERR:' + stderr + '\nSTDOUT:' + stdout));
}
var applyBabelJobProcess = childProcess.spawn(__dirname + '/../../bin/applyBabelJob', [
'--babeldir', babelDir,
'--root', tmpTestCaseCopyDir,
'--defaultlocale', 'en',
'--locales', 'en,cs',
'--i18n', tmpTestCaseCopyDir + '/index.i18n',
'--replace',
tmpTestCaseCopyDir + '/index.html'
]);

applyBabelJobProcess.on('exit', function (exitCode) {
if (exitCode) {
done(new Error('The applyBabelJob process ended with a non-zero exit code: ' + exitCode));
} else {
expect(JSON.parse(fs.readFileSync(tmpTestCaseCopyDir + '/index.i18n')), 'to equal', {
MyPlurals: {
en: {
one: 'The plural',
other: 'The plurals'
},
cs: {
one: 'xxxx',
other: 'yyyy',
few: 'zzzz',
many: 'wwww'
}
}
});
expect(fs.readFileSync(tmpTestCaseCopyDir + '/index.html', 'utf-8'), 'to equal',
'<!DOCTYPE html>\n' +
'<html>\n' +
' <head>\n' +
' </head>\n' +
' <body>\n' +
' <script>\n' +
' INCLUDE(\'index.i18n\');\n' +
' alert(TR(\'MyPlurals\', {\n' +
' one: \'The plural\',\n' +
' other: \'The plurals\'\n' +
' }));\n' +
' </script>\n' +
' </body>\n' +
'</html>\n'
);
done();
}
});
});
});
});
14 changes: 14 additions & 0 deletions testdata/bin/applyBabelJob/invalidPlurals/index.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
INCLUDE('index.i18n');
alert(TR('MyPlurals', {
one: 'The plural',
other: 'The plurals'
}));
</script>
</body>
</html>
14 changes: 14 additions & 0 deletions testdata/bin/applyBabelJob/invalidPlurals/index.i18n
@@ -0,0 +1,14 @@
{
"MyPlurals": {
"en": {
"one": "The plural",
"other": "The plurals"
},
"cs": {
"one": null,
"other": null,
"few": null,
"many": null
}
}
}
@@ -0,0 +1,4 @@
MyPlurals[one]=xxxx
MyPlurals[other]=yyyy
MyPlurals[few]=zzzz
MyPlurals[many]=wwww
@@ -0,0 +1,4 @@
MyPlurals[one]=The plural
MyPlurals[other]=The plurals
MyPlurals[few]=argh
MyPlurals[many]=blargh

0 comments on commit 2e5c729

Please sign in to comment.