Skip to content

Commit

Permalink
support plurals in returning objecttree array
Browse files Browse the repository at this point in the history
  • Loading branch information
jamuhl committed Feb 7, 2019
1 parent 3b31cf3 commit 51bb65e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
### 14.1.0

- support plurals in returning objecttree array [1196](https://github.com/i18next/i18next/issues/1196)

### 14.0.1

- typescript: Parameterized use of TFunction fails while WithT use works [1188](https://github.com/i18next/i18next/pull/1188)
Expand Down
13 changes: 9 additions & 4 deletions i18next.js
Expand Up @@ -663,6 +663,7 @@ var Translator = function (_EventEmitter) {
var resolved = this.resolve(keys, options);
var res = resolved && resolved.res;
var resUsedKey = resolved && resolved.usedKey || key;
var resExactUsedKey = resolved && resolved.exactUsedKey || key;

var resType = Object.prototype.toString.apply(res);
var noObject = ['[object Number]', '[object Function]', '[object RegExp]'];
Expand All @@ -680,12 +681,14 @@ var Translator = function (_EventEmitter) {
// if we got a separator we loop over children - else we just return object as is
// as having it set to false means no hierarchy so no lookup for nested values
if (keySeparator) {
var copy$$1 = resType === '[object Array]' ? [] : {}; // apply child translation on a copy
var resTypeIsArray = resType === '[object Array]';
var copy$$1 = resTypeIsArray ? [] : {}; // apply child translation on a copy

/* eslint no-restricted-syntax: 0 */
var newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
for (var m in res) {
if (Object.prototype.hasOwnProperty.call(res, m)) {
var deepKey = '' + resUsedKey + keySeparator + m;
var deepKey = '' + newKeyToUse + keySeparator + m;
copy$$1[m] = this.translate(deepKey, _extends({}, options, { joinArrays: false, ns: namespaces }));
if (copy$$1[m] === deepKey) copy$$1[m] = res[m]; // if nothing found use orginal value as fallback
}
Expand Down Expand Up @@ -811,7 +814,8 @@ var Translator = function (_EventEmitter) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var found = void 0;
var usedKey = void 0;
var usedKey = void 0; // plain key
var exactUsedKey = void 0; // key with context / plural
var usedLng = void 0;
var usedNS = void 0;

Expand Down Expand Up @@ -863,14 +867,15 @@ var Translator = function (_EventEmitter) {
/* eslint no-cond-assign: 0 */
while (possibleKey = finalKeys.pop()) {
if (!_this4.isValidLookup(found)) {
exactUsedKey = possibleKey;
found = _this4.getResource(code, ns, possibleKey, options);
}
}
});
});
});

return { res: found, usedKey: usedKey, usedLng: usedLng, usedNS: usedNS };
return { res: found, usedKey: usedKey, exactUsedKey: exactUsedKey, usedLng: usedLng, usedNS: usedNS };
};

Translator.prototype.isValidLookup = function isValidLookup(res) {
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "i18next",
"version": "14.0.1",
"version": "14.1.0",
"description": "i18next internationalization framework",
"main": "./index.js",
"types": "index.d.ts",
Expand Down
13 changes: 9 additions & 4 deletions src/Translator.js
Expand Up @@ -98,6 +98,7 @@ class Translator extends EventEmitter {
const resolved = this.resolve(keys, options);
let res = resolved && resolved.res;
const resUsedKey = (resolved && resolved.usedKey) || key;
const resExactUsedKey = (resolved && resolved.exactUsedKey) || key;

const resType = Object.prototype.toString.apply(res);
const noObject = ['[object Number]', '[object Function]', '[object RegExp]'];
Expand Down Expand Up @@ -125,12 +126,14 @@ class Translator extends EventEmitter {
// if we got a separator we loop over children - else we just return object as is
// as having it set to false means no hierarchy so no lookup for nested values
if (keySeparator) {
const copy = resType === '[object Array]' ? [] : {}; // apply child translation on a copy
const resTypeIsArray = resType === '[object Array]';
const copy = resTypeIsArray ? [] : {}; // apply child translation on a copy

/* eslint no-restricted-syntax: 0 */
let newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
for (const m in res) {
if (Object.prototype.hasOwnProperty.call(res, m)) {
const deepKey = `${resUsedKey}${keySeparator}${m}`;
const deepKey = `${newKeyToUse}${keySeparator}${m}`;
copy[m] = this.translate(deepKey, {
...options,
...{ joinArrays: false, ns: namespaces },
Expand Down Expand Up @@ -295,7 +298,8 @@ class Translator extends EventEmitter {

resolve(keys, options = {}) {
let found;
let usedKey;
let usedKey; // plain key
let exactUsedKey; // key with context / plural
let usedLng;
let usedNS;

Expand Down Expand Up @@ -355,14 +359,15 @@ class Translator extends EventEmitter {
/* eslint no-cond-assign: 0 */
while ((possibleKey = finalKeys.pop())) {
if (!this.isValidLookup(found)) {
exactUsedKey = possibleKey;
found = this.getResource(code, ns, possibleKey, options);
}
}
});
});
});

return { res: found, usedKey, usedLng, usedNS };
return { res: found, usedKey, exactUsedKey, usedLng, usedNS };
}

isValidLookup(res) {
Expand Down
10 changes: 10 additions & 0 deletions test/translator/translator.translate.array.spec.js
Expand Up @@ -17,6 +17,8 @@ describe('Translator', () => {
search: {
flagList: [['basic', 'Basic'], ['simple', 'Simple']],
},
keyArray: ["hello world {{count}}", "hey {{count}}"],
keyArray_plural: ["hello world plural {{count}}", "hey plural {{count}}"],
},
},
});
Expand Down Expand Up @@ -54,6 +56,14 @@ describe('Translator', () => {
args: [['search.flagList', 'flagList'], {}],
expected: [['basic', 'Basic'], ['simple', 'Simple']],
},
{
args: ['keyArray', { count: 1 }],
expected: ["hello world 1", "hey 1"],
},
{
args: ['keyArray', { count: 100 }],
expected: ["hello world plural 100", "hey plural 100"],
},
];

tests.forEach(test => {
Expand Down

0 comments on commit 51bb65e

Please sign in to comment.