Skip to content

Commit

Permalink
Merge 9d6ae46 into a7f6256
Browse files Browse the repository at this point in the history
  • Loading branch information
rumbcam committed Feb 24, 2016
2 parents a7f6256 + 9d6ae46 commit 7ef0916
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Translator.js
Expand Up @@ -201,11 +201,17 @@ class Translator extends EventEmitter {
let finalKey = key;
let finalKeys = [finalKey];

let pluralSuffix;
if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code, options.count);

// fallback for plural if context not found
if (needsPluralHandling && needsContextHandling) finalKeys.push(finalKey + pluralSuffix);

// get key for context if needed
if (needsContextHandling) finalKeys.push(finalKey += `${this.options.contextSeparator}${options.context}`);

// get key for plural if needed
if (needsPluralHandling) finalKeys.push(finalKey += this.pluralResolver.getSuffix(code, options.count));
if (needsPluralHandling) finalKeys.push(finalKey += pluralSuffix);

// iterate over finalKeys starting with most specific pluralkey (-> contextkey only) -> singularkey only
let possibleKey;
Expand Down
17 changes: 15 additions & 2 deletions test/translator/translator.translate.combination.spec.js
Expand Up @@ -15,7 +15,13 @@ describe('Translator', () => {
translation: {
'key1': 'hello world',
'key2': 'It is: $t(key1)',
'key3': 'It is: {{val}}'
'key3': 'It is: {{val}}',

// context with pluralization
test: 'test_en',
test_plural: 'tests_en',
test_male: 'test_male_en',
test_male_plural: 'tests_male_en',
}
}
});
Expand All @@ -26,6 +32,7 @@ describe('Translator', () => {
pluralResolver: new PluralResolver(lu, {prepend: '_'}),
interpolator: new Interpolator()
}, {
contextSeparator: '_',
ns: 'translation',
defaultNS: 'translation',
interpolation: {}
Expand All @@ -36,7 +43,13 @@ describe('Translator', () => {
var tests = [
// interpolation and nesting in var
{ args: ['key2'], expected: 'It is: hello world' },
{ args: ['key3', { val: '$t(key1)' }], expected: 'It is: hello world' }
{ args: ['key3', { val: '$t(key1)' }], expected: 'It is: hello world' },

// context with pluralization
{args: ['test', { context: 'unknown', count: 1 }], expected: 'test_en'},
{args: ['test', { context: 'unknown', count: 2 }], expected: 'tests_en'},
{args: ['test', { context: 'male', count: 1 }], expected: 'test_male_en'},
{args: ['test', { context: 'male', count: 2 }], expected: 'tests_male_en'},
];

tests.forEach((test) => {
Expand Down
1 change: 1 addition & 0 deletions test/translator/translator.translate.context.spec.js
Expand Up @@ -44,6 +44,7 @@ describe('Translator', () => {
});

var tests = [
{args: ['translation:test', { context: 'unknown' }], expected: 'test_en'},
{args: ['translation:test', { context: 'male' }], expected: 'test_male_en'},
{args: ['translation:test', { context: 'female' }], expected: 'test_female_en'},
{args: ['translation:test', { context: 'male', lngs: ['en-US', 'en'] }], expected: 'test_male_en'},
Expand Down

0 comments on commit 7ef0916

Please sign in to comment.