Skip to content

Commit

Permalink
makeBabelJob: Only output one # NOTE... per flattened key that needs …
Browse files Browse the repository at this point in the history
…additional plural translation(s).
  • Loading branch information
papandreou committed Feb 2, 2015
1 parent 431c976 commit b8372e3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 27 deletions.
39 changes: 25 additions & 14 deletions bin/makeBabelJob
Original file line number Diff line number Diff line change
Expand Up @@ -280,25 +280,36 @@ new AssetGraph({root: commandLineOptions.root})
});

if (keyNeedsTranslation && isDefaultLocale && relevantPluralFormsNotInTheDefaultLocale.length > 0 && valueContainsPlurals(value)) {
var localeIdsByFlattenedKey = {};
relevantPluralFormsNotInTheDefaultLocale.forEach(function (pluralForm) {
var localeIdsNeedingThisKey = localeIds.filter(function (localeId) {
return pluralsCldr.forms(localeId).indexOf(pluralForm) !== -1;
});
localeIdsNeedingThisKey.forEach(function (localeId) {
var valueByFlattenedKey = flattenKey(key, nullOutLeaves(coalescePluralsToLocale(defaultValueInTheOccurrence, localeId, pluralForm))),
existingTranslationByFlattenedKey = allKeys[key] && localeId in allKeys[key] ? flattenKey(key, allKeys[key][localeId]) : {},
notes = Object.keys(valueByFlattenedKey).filter(function (flattenedKey) {
return !(flattenedKey in existingTranslationByFlattenedKey);
}).map(function (flattenedKey) {
return '# ' + flattenedKey + '=\n';
});
localeIds.forEach(function (localeId) {
if (pluralsCldr.forms(localeId).indexOf(pluralForm) !== -1) {
var valueByFlattenedKey = flattenKey(key, nullOutLeaves(coalescePluralsToLocale(defaultValueInTheOccurrence, localeId, pluralForm))),
existingTranslationByFlattenedKey = allKeys[key] && localeId in allKeys[key] ? flattenKey(key, allKeys[key][localeId]) : {};

if (notes.length > 0) {
babelSrc += '# NOTE: The language ' + localeId + ' needs this additional key to cover all plural forms:\n' +
notes.join('');
Object.keys(valueByFlattenedKey).forEach(function (flattenedKey) {
if (!(flattenedKey in existingTranslationByFlattenedKey)) {
(localeIdsByFlattenedKey[flattenedKey] = localeIdsByFlattenedKey[flattenedKey] || []).push(localeId);
}
});
}
});
});
var flattenedKeysByJoinedLocaleIds = {};
Object.keys(localeIdsByFlattenedKey).forEach(function (flattenedKey) {
var localeIds = localeIdsByFlattenedKey[flattenedKey];
localeIds.sort();
(flattenedKeysByJoinedLocaleIds[localeIds.join(',')] = flattenedKeysByJoinedLocaleIds[localeIds.join(',')] || []).push(flattenedKey);
});

Object.keys(flattenedKeysByJoinedLocaleIds).forEach(function (joinedLocaleIds) {
var flattenedKeys = flattenedKeysByJoinedLocaleIds[joinedLocaleIds],
localeIds = joinedLocaleIds.split(',');
babelSrc += '# NOTE: The language' + (localeIds.length > 1 ? 's ' : ' ') + localeIds.join(', ') + (localeIds.length > 1 ? ' need' : ' needs') + (flattenedKeys.length > 1 ? ' these additional keys' : ' this additional key') + ' to cover all plural forms:\n' +
flattenedKeys.map(function (flattenedKey) {
return '# ' + flattenedKey + '=\n';
}).join('');
});
}

var i18nAssetForKey = assetGraph.findAssets({type: 'I18n', isLoaded: true, parseTree: function (parseTree) {
Expand Down
55 changes: 42 additions & 13 deletions test/bin/makeBabelJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('makeBabelJob', function () {
'--i18n', Path.resolve(tmpTestCaseCopyDir, 'index.i18n'),
Path.resolve(tmpTestCaseCopyDir, 'index.html'),
'--defaultlocale', 'en',
'--locales', 'en,da,de,cs'
'--locales', 'en,pl,da,de,cs'
]),
buffersByStreamName = {},
streamNames = ['stdout', 'stderr'];
Expand All @@ -48,25 +48,26 @@ describe('makeBabelJob', function () {
return done(new Error('The makeBabelJob process ended with a non-zero exit code: ' + exitCode + getStreamOutputText()));
}

expect(fs.readdirSync(babelDir).sort(), 'to equal', ['cs.txt', 'da.txt', 'de.txt', 'en.txt']);
expect(fs.readdirSync(babelDir).sort(), 'to equal', ['cs.txt', 'da.txt', 'de.txt', 'en.txt', 'pl.txt']);

expect(fs.readFileSync(Path.resolve(babelDir, 'en.txt'), 'utf-8'), 'to equal', [
'KeyAlreadyPartiallyTranslatedInIndexI18n=Key already partially translated in index.i18n',
'KeyAlreadyPartiallyTranslatedInOtherI18n=Key already partially translated in other.i18n',
'KeyAlreadyTranslatedToCzech[one]=foo',
'KeyAlreadyTranslatedToCzech[other]=foo',
'# NOTE: The language pl needs these additional keys to cover all plural forms:',
'# KeyAlreadyTranslatedToCzech[few]=',
'# KeyAlreadyTranslatedToCzech[many]=',
'KeyDestinedForIndexI18n=Key destined for index.i18n',
'NotYetTranslatedKeyWithPluralCases[one]=one week',
'NotYetTranslatedKeyWithPluralCases[other]={0} weeks',
'# NOTE: The language cs needs this additional key to cover all plural forms:',
'# NOTE: The languages cs, pl need these additional keys to cover all plural forms:',
'# NotYetTranslatedKeyWithPluralCases[few]=',
'# NOTE: The language cs needs this additional key to cover all plural forms:',
'# NotYetTranslatedKeyWithPluralCases[many]=',
'NotYetTranslatedKeyWithPluralCasesInNestedStructure[foo][one]=one week',
'NotYetTranslatedKeyWithPluralCasesInNestedStructure[foo][other]={0} weeks',
'# NOTE: The language cs needs this additional key to cover all plural forms:',
'# NOTE: The languages cs, pl need these additional keys to cover all plural forms:',
'# NotYetTranslatedKeyWithPluralCasesInNestedStructure[foo][few]=',
'# NOTE: The language cs needs this additional key to cover all plural forms:',
'# NotYetTranslatedKeyWithPluralCasesInNestedStructure[foo][many]=',
''
].join('\n'));
Expand Down Expand Up @@ -118,42 +119,69 @@ describe('makeBabelJob', function () {
''
].join('\n'));

expect(fs.readFileSync(Path.resolve(babelDir, 'pl.txt'), 'utf-8'), 'to equal', [
'KeyAlreadyPartiallyTranslatedInIndexI18n=',
'KeyAlreadyPartiallyTranslatedInOtherI18n=',
'KeyAlreadyTranslatedToAllLanguages[few]=fzz',
'KeyAlreadyTranslatedToAllLanguages[many]=fzz',
'KeyAlreadyTranslatedToCzech[few]=',
'KeyAlreadyTranslatedToCzech[many]=',
'KeyAlreadyTranslatedToCzech[one]=',
'KeyAlreadyTranslatedToCzech[other]=',
'KeyDestinedForIndexI18n=',
'NotYetTranslatedKeyWithPluralCases[few]=',
'NotYetTranslatedKeyWithPluralCases[many]=',
'NotYetTranslatedKeyWithPluralCases[one]=',
'NotYetTranslatedKeyWithPluralCases[other]=',
'NotYetTranslatedKeyWithPluralCasesInNestedStructure[foo][few]=',
'NotYetTranslatedKeyWithPluralCasesInNestedStructure[foo][many]=',
'NotYetTranslatedKeyWithPluralCasesInNestedStructure[foo][one]=',
'NotYetTranslatedKeyWithPluralCasesInNestedStructure[foo][other]=',
''
].join('\n'));

expect(JSON.parse(fs.readFileSync(Path.resolve(tmpTestCaseCopyDir, 'index.i18n'), 'utf-8')), 'to equal', {
KeyDestinedForIndexI18n: {
cs: null,
en: 'Key destined for index.i18n',
de: null,
da: null
da: null,
pl: null
},
KeyAlreadyPartiallyTranslatedInIndexI18n: {
cs: null,
en: 'Key already partially translated in index.i18n',
de: 'Existing translation to German',
da: null
da: null,
pl: null
},
NotYetTranslatedKeyWithPluralCases: {
cs: { one: null, few: null, many: null, other: null },
da: { one: null, other: null },
de: { one: null, other: null },
en: { one: 'one week', other: '{0} weeks' }
en: { one: 'one week', other: '{0} weeks' },
pl: { one: null, few: null, many: null, other: null }
},
NotYetTranslatedKeyWithPluralCasesInNestedStructure: {
cs: { foo: { one: null, few: null, many: null, other: null } },
da: { foo: { one: null, other: null } },
de: { foo: { one: null, other: null } },
en: { foo: { one: 'one week', other: '{0} weeks' } }
en: { foo: { one: 'one week', other: '{0} weeks' } },
pl: { foo: { few: null, many: null, one: null, other: null } }
},
KeyAlreadyTranslatedToAllLanguages: {
cs: { one: 'fzd', few: 'fzd', many: 'fzd', other: 'fzd' },
da: { one: 'føø', other: 'føø' },
de: { one: 'voo', other: 'voo' },
en: { one: 'foo', other: 'foo' }
en: { one: 'foo', other: 'foo' },
pl: { one: 'fzz', few: 'fzz', many: 'fzz', other: 'fzz' },
},
KeyAlreadyTranslatedToCzech: {
cs: { one: 'fzd', few: 'fzd', many: 'fzd', other: 'fzd' },
da: { one: null, other: null },
de: { one: null, other: null },
en: { one: 'foo', other: 'foo' }
en: { one: 'foo', other: 'foo' },
pl: { few: null, many: null, one: null, other: null }
}
});

Expand All @@ -162,7 +190,8 @@ describe('makeBabelJob', function () {
cs: null,
en: 'Key already partially translated in other.i18n',
de: 'Existing translation to German',
da: null
da: null,
pl: null
}
});

Expand Down
6 changes: 6 additions & 0 deletions testdata/bin/makeBabelJob/index.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"en": {
"one": "foo",
"other": "foo"
},
"pl": {
"one": "fzz",
"few": "fzz",
"many": "fzz",
"other": "fzz"
}
}
}

0 comments on commit b8372e3

Please sign in to comment.