Skip to content

Commit

Permalink
Fixed the remaining problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 15, 2015
1 parent 70c5148 commit f748e44
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
69 changes: 40 additions & 29 deletions bin/makeBabelJob
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ new AssetGraph({root: commandLineOptions.root})
var initialHtmlAssets = assetGraph.findAssets({type: 'Html', isInitial: true}),
occurrencesByKey = i18nTools.findOccurrences(assetGraph, initialHtmlAssets),
allKeys = i18nTools.extractAllKeys(assetGraph),
allKeysFlattened = {},
alreadyTranslatedByFlattenedKey = {},
i18nAssetForAllKeys;

if (i18nUrl) {
Expand All @@ -193,41 +191,54 @@ new AssetGraph({root: commandLineOptions.root})

var isRelevantInLocaleByFlattenedKeyByLocaleId = {},
isRelevantInAnyLocaleByFlattenedKey = {},
keyByFlattenedKey = {};
keyByFlattenedKey = {},
isTranslatedByFlattenedKeyByLocaleId = {};

Object.keys(occurrencesByKey).forEach(function (key) {
var occurrences = occurrencesByKey[key],
defaultValueInTheOccurrence,
defaultValue;

// Look for a default value in the occurrences:
occurrences.forEach(function (occurrence) {
// FIXME: Warn about multiple different default values?
defaultValueInTheOccurrence = occurrence.defaultValue;
});

if (key in allKeys && defaultLocaleId in allKeys[key]) {
defaultValue = allKeys[key][defaultLocaleId];
} else {
defaultValue = defaultValueInTheOccurrence;
}

localeIds.forEach(function (localeId) {
var value,
isDefaultValue = false;
if (key in allKeys && localeId in allKeys[key]) {
value = allKeys[key][localeId];
} else {
value = nullOutLeaves(defaultValue);
isDefaultValue = true;
}

Object.keys(allKeys).forEach(function (key) {
Object.keys(allKeys[key]).forEach(function (localeId) {
isRelevantInLocaleByFlattenedKeyByLocaleId[localeId] = isRelevantInLocaleByFlattenedKeyByLocaleId[localeId] || {};
var valueByFlattenedKey = flattenKey(key, allKeys[key][localeId]);
Object.keys(valueByFlattenedKey).forEach(function (flattenedKey) {
allKeysFlattened[flattenedKey] = allKeysFlattened[flattenedKey] || {};
allKeysFlattened[flattenedKey][localeId] = valueByFlattenedKey[flattenedKey];
keyByFlattenedKey[flattenedKey] = key;
});
isTranslatedByFlattenedKeyByLocaleId[localeId] = isTranslatedByFlattenedKeyByLocaleId[localeId] || {};
var flattenedAndCoalesced = flattenKey(key, coalescePluralsToLocale(value, localeId));

var flattenedAndCoalesced = flattenKey(key, coalescePluralsToLocale(allKeys[key][localeId], localeId));
Object.keys(flattenedAndCoalesced).forEach(function (flattenedKey) {
isRelevantInLocaleByFlattenedKeyByLocaleId[localeId][flattenedKey] = true;
var value = flattenedAndCoalesced[flattenedKey];
isTranslatedByFlattenedKeyByLocaleId[localeId][flattenedKey] = !isDefaultValue && typeof value !== 'undefined';
isRelevantInAnyLocaleByFlattenedKey[flattenedKey] = true;
keyByFlattenedKey[flattenedKey] = key;
});
});
});

// Note which flattened keys have already been translated to all the locales we've been asked to process:
Object.keys(isRelevantInAnyLocaleByFlattenedKey).forEach(function (flattenedKey) {
alreadyTranslatedByFlattenedKey[flattenedKey] = true;
localeIds.forEach(function (localeId) {
if (!isRelevantInLocaleByFlattenedKeyByLocaleId[localeId][flattenedKey]) {
return;
}
var prioritizedLocaleIds = i18nTools.expandLocaleIdToPrioritizedList(localeId);
for (var i = 0 ; i < prioritizedLocaleIds.length ; i += 1) {
if (allKeysFlattened[flattenedKey] && prioritizedLocaleIds[i] in allKeysFlattened[flattenedKey]) {
return;
}
}
alreadyTranslatedByFlattenedKey[flattenedKey] = false;
var alreadyTranslatedByFlattenedKey = {};
Object.keys(keyByFlattenedKey).forEach(function (flattenedKey) {
alreadyTranslatedByFlattenedKey[flattenedKey] = localeIds.every(function (localeId) {
return !isRelevantInLocaleByFlattenedKeyByLocaleId[localeId][flattenedKey] || isTranslatedByFlattenedKeyByLocaleId[localeId][flattenedKey];
});
});

Expand Down Expand Up @@ -350,11 +361,11 @@ new AssetGraph({root: commandLineOptions.root})
i18nAssetForKey.markDirty();
}
i18nAssetForKey.parseTree[key] = i18nAssetForKey.parseTree[key] || {};
var newValue;
if (!(localeId in i18nAssetForKey.parseTree[key])) {
if (localeId.indexOf(defaultLocaleId) === 0) {
i18nAssetForKey.parseTree[key][localeId] = defaultValue;
} else {
var newValue;
if (omitExistingValues) {
newValue = nullOutLeaves(coalescePluralsToLocale(value, localeId));
} else {
Expand All @@ -364,8 +375,8 @@ new AssetGraph({root: commandLineOptions.root})
}
i18nAssetForKey.markDirty();
} else {
var existingValue = i18nAssetForKey.parseTree[key][localeId],
newValue = nullOutLeaves(coalescePluralsToLocale(existingValue, localeId), true);
var existingValue = i18nAssetForKey.parseTree[key][localeId];
newValue = nullOutLeaves(coalescePluralsToLocale(existingValue, localeId), true);
i18nAssetForKey.parseTree[key][localeId] = newValue;
i18nAssetForKey.markDirty();
}
Expand Down
4 changes: 0 additions & 4 deletions test/bin/makeBabelJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ describe('makeBabelJob', function () {
expect(fs.readFileSync(Path.resolve(babelDir, 'cs.txt'), 'utf-8'), 'to equal', [
'KeyAlreadyPartiallyTranslatedInIndexI18n=',
'KeyAlreadyPartiallyTranslatedInOtherI18n=',
'KeyAlreadyTranslatedToAllLanguages[few]=fzd',
'KeyAlreadyTranslatedToAllLanguages[many]=fzd',
'KeyAlreadyTranslatedToCzech[few]=fzd',
'KeyAlreadyTranslatedToCzech[many]=fzd',
'KeyAlreadyTranslatedToCzech[one]=fzd',
Expand All @@ -122,8 +120,6 @@ describe('makeBabelJob', function () {
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]=',
Expand Down

0 comments on commit f748e44

Please sign in to comment.