Skip to content

Commit

Permalink
fix incorrect singular -> plural mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Jan 12, 2018
1 parent f5aaf3f commit 24abe89
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
],
"cwd": "${workspaceRoot}",
"preLaunchTask": "pretest",
"env": {
"NODE_ENV": "debug",
},
"runtimeArgs": [
"--nolazy"
],
Expand Down
29 changes: 24 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,23 @@ export default function sync({
!keyMatchesPluralForLanguage(key, targetLanguage) &&
!pluralFormsMatch()
) {
copyPlurals(createPlurals(key, source), target);
if (!targetPluralsPopulated(target, key)) {
copyPlurals(createPlurals(key, source), target);
}
} else {
//base case: source contains key not present in target
target[key] = sourceValue;
record.keyAdded(key);
}
}

function targetPluralsPopulated(target: object, key: string) {
//given 'x' for key, do we have 'x' and 'x_plural' for en?
const singular = getSingularForm(key);
const pluralKeys = getPluralsForLanguage(targetLanguage).map(p => p.replace('key', singular));
const targetKeys = Object.keys(target);
return pluralKeys.every(expectedPluralKeys => targetKeys.indexOf(expectedPluralKeys) > -1);
}

function copyPlurals(plurals: Object, target: Object) {
for (const key of Object.keys(plurals)) {
if (target.hasOwnProperty(key)) {
Expand All @@ -172,6 +181,10 @@ export default function sync({
return false;
}

if (languageOnlyHasOneForm(primaryLanguage)) {
return false;
}

return (
keyMatchesPluralForLanguageIncludingSingular(key, allPimaryKeys, primaryLanguage) &&
!keyMatchesPluralForLanguageIncludingSingular(key, allTargetKeys, targetLanguage)
Expand Down Expand Up @@ -277,11 +290,17 @@ export default function sync({

function createPlurals(key: string, source: Object) {
const singular = getSingularForm(key);
const fillValue = getPluralFillValue(singular, source);
const plurals = {};
for (const form of getPluralsForLanguage(targetLanguage)) {
plurals[form.replace('key', singular)] = fillValue;

if (languageOnlyHasOneForm(primaryLanguage)) {
plurals[key] = source[key];
} else {
const fillValue = getPluralFillValue(singular, source);
for (const form of getPluralsForLanguage(targetLanguage)) {
plurals[form.replace('key', singular)] = fillValue;
}
}

return plurals;
}

Expand Down
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noImplicitReturns": true,
"outDir": "../dist",
"pretty": true,
Expand Down
2 changes: 2 additions & 0 deletions testRunner/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module.exports = {
compilerOptions: Object.assign(config, {
inlineSourceMap: true,
inlineSources: true,
target: process.env.NODE_ENV === 'debug' ? 'es2017' : 'es5',
module: 'commonjs'
})
}).outputText;
}
Expand Down
35 changes: 13 additions & 22 deletions tests/primary-language-zh/__snapshots__/runner.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
exports[`primary-language-zh:locales/en.json 1`] = `
"{
add: {
simple_value: simple zh value,
simple_value_plural: simple zh value,
value: add from zh,
value_plural: add from zh
value: add from zh
},
keep: {
singular_value: singular en value,
value: value en,
value_plural: values en
},
Expand All @@ -20,14 +18,11 @@ exports[`primary-language-zh:locales/en.json 1`] = `
exports[`primary-language-zh:locales/pt-BR.json 1`] = `
"{
add: {
simple_value: simple zh value,
simple_value_plural: simple zh value,
value: add from zh,
value_plural: add from zh
value: add from zh
},
keep: {
value: value zh,
value_plural: value zh
singular_value: singular zh value,
value: value zh
},
remove: {
}
Expand All @@ -37,14 +32,10 @@ exports[`primary-language-zh:locales/pt-BR.json 1`] = `
exports[`primary-language-zh:locales/ru.json 1`] = `
"{
add: {
simple_value_0: simple zh value,
simple_value_1: simple zh value,
simple_value_2: simple zh value,
value_0: add from zh,
value_1: add from zh,
value_2: add from zh
value: add from zh
},
keep: {
singular_value: singular ru value,
value_0: values ru 0,
value_1: values ru 1,
value_2: values ru 2
Expand All @@ -57,10 +48,10 @@ exports[`primary-language-zh:locales/ru.json 1`] = `
exports[`primary-language-zh:locales/zh.json 1`] = `
"{
add: {
simple_value: simple zh value,
value: add from zh
},
keep: {
singular_value: singular zh value,
value: value zh
},
remove: {
Expand All @@ -71,10 +62,10 @@ exports[`primary-language-zh:locales/zh.json 1`] = `
exports[`primary-language-zh:stderr.txt 1`] = `""`;

exports[`primary-language-zh:stdout.txt 1`] = `
"Pushed keys to actual/locales/en.json: value, value_plural, simple_value, simple_value_plural
Removed keys from actual/locales/en.json: simple_value, remove, remove_0, remove_plural
Pushed keys to actual/locales/ru.json: value_0, value_1, value_2, simple_value_0, simple_value_1, simple_value_2
Removed keys from actual/locales/ru.json: simple_value, remove, remove_0, remove_1, remove_2, remove_plural
Pushed keys to actual/locales/pt-BR.json: value, value_plural, simple_value, simple_value_plural, value, value_plural
"Pushed keys to actual/locales/en.json: value
Removed keys from actual/locales/en.json: remove, remove_0, remove_plural
Pushed keys to actual/locales/ru.json: value
Removed keys from actual/locales/ru.json: remove, remove_0, remove_1, remove_2, remove_plural
Pushed keys to actual/locales/pt-BR.json: value, value, singular_value
"
`;
2 changes: 1 addition & 1 deletion tests/primary-language-zh/project/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"keep": {
"value": "value en",
"value_plural": "values en",
"simple_value": "simple en value"
"singular_value": "singular en value"
},
"remove": {
"remove": "remove en",
Expand Down
2 changes: 1 addition & 1 deletion tests/primary-language-zh/project/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"value_0": "values ru 0",
"value_1": "values ru 1",
"value_2": "values ru 2",
"simple_value": "simple ru value"
"singular_value": "singular ru value"
},
"remove": {
"remove": "remove ru",
Expand Down
6 changes: 3 additions & 3 deletions tests/primary-language-zh/project/locales/zh.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"add": {
"value": "add from zh",
"simple_value": "simple zh value"
"value": "add from zh"
},
"keep": {
"value": "value zh"
"value": "value zh",
"singular_value": "singular zh value"
},
"remove": {}
}

0 comments on commit 24abe89

Please sign in to comment.