Skip to content

Commit

Permalink
context can also be a number, fixes #1622
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Jun 17, 2021
1 parent 2482b61 commit d304941
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 20.3.2

- context can also be a number [1622](https://github.com/i18next/i18next/issues/1622)

### 20.3.1

- add 'ns' to the 'returnedObjectHandler' options [1619](https://github.com/i18next/i18next/pull/1619)
Expand Down
2 changes: 1 addition & 1 deletion i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@
var namespaces = extracted.namespaces;
if (_this4.options.fallbackNS) namespaces = namespaces.concat(_this4.options.fallbackNS);
var needsPluralHandling = options.count !== undefined && typeof options.count !== 'string';
var needsContextHandling = options.context !== undefined && typeof options.context === 'string' && options.context !== '';
var needsContextHandling = options.context !== undefined && (typeof options.context === 'string' || typeof options.context === 'number') && options.context !== '';
var codes = options.lngs ? options.lngs : _this4.languageUtils.toResolveHierarchy(options.lng || _this4.language, options.fallbackLng);
namespaces.forEach(function (ns) {
if (_this4.isValidLookup(found)) return;
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class Translator extends EventEmitter {
const needsPluralHandling = options.count !== undefined && typeof options.count !== 'string';
const needsContextHandling =
options.context !== undefined &&
typeof options.context === 'string' &&
(typeof options.context === 'string' || typeof options.context === 'number') &&
options.context !== '';

const codes = options.lngs
Expand Down
18 changes: 18 additions & 0 deletions test/translator/translator.translate.context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ describe('Translator', () => {
test: 'test_en',
test_male: 'test_male_en',
test_female: 'test_female_en',
test_1: 'test_one_en',
},
},
de: {
translation: {
test: 'test_de',
test_male: 'test_male_de',
test_female: 'test_female_de',
test_1: 'test_one_de',
},
},
});
Expand Down Expand Up @@ -71,6 +73,22 @@ describe('Translator', () => {
args: ['translation:test', { context: 'female', lng: 'en-US' }],
expected: 'test_female_en',
},
{
args: ['translation:test', { context: '1', lng: 'en-US' }],
expected: 'test_one_en',
},
{
args: ['translation:test', { context: '1', lng: 'de' }],
expected: 'test_one_de',
},
{
args: ['translation:test', { context: 1, lng: 'en-US' }],
expected: 'test_one_en',
},
{
args: ['translation:test', { context: 1, lng: 'de' }],
expected: 'test_one_de',
},
];

tests.forEach(test => {
Expand Down

0 comments on commit d304941

Please sign in to comment.