diff --git a/README.md b/README.md index 450e7ee..9cae31c 100644 --- a/README.md +++ b/README.md @@ -526,6 +526,7 @@ Below are the configuration options with their default values: keySeparator: '.', pluralSeparator: '_', contextSeparator: '_', + contextDefaultValues: [], interpolation: { prefix: '{{', suffix: '}}' @@ -745,6 +746,13 @@ Type: `String` Default: `'_'` The character to split context from key. +#### contextDefaultValues + +Type: `Array` Default: `[]` + +A list of default context values, used when the scanner encounters dynamic value as a `context`. +For a list of `['male', 'female']` the scanner will generate an entry for each value. + #### plural Type: `Boolean` or `Function` Default: `true` diff --git a/src/parser.js b/src/parser.js index 8c294bd..5d7b444 100644 --- a/src/parser.js +++ b/src/parser.js @@ -80,6 +80,7 @@ const defaults = { context: true, // whether to add context form key contextFallback: true, // whether to add a fallback key as well as the context form key contextSeparator: '_', // char to split context from key + contextDefaultValues: [], // list of values for dynamic values // Plural Form plural: true, // whether to add plural form key @@ -855,6 +856,7 @@ class Parser { context, contextFallback, contextSeparator, + contextDefaultValues, plural, pluralFallback, pluralSeparator, @@ -927,6 +929,16 @@ class Parser { : !!plural; })(); + const contextValues = (() => { + if (options.context !== '') { + return [options.context]; + } + if (ensureArray(contextDefaultValues).length > 0) { + return ensureArray(contextDefaultValues); + } + return []; + })(); + if (containsPlural) { let suffixes = pluralFallback ? this.pluralSuffixes[lng] @@ -938,7 +950,9 @@ class Parser { if (containsContext && containsPlural) { suffixes.forEach((pluralSuffix) => { - resKeys.push(`${key}${contextSeparator}${options.context}${pluralSuffix}`); + contextValues.forEach(contextValue => { + resKeys.push(`${key}${contextSeparator}${contextValue}${pluralSuffix}`); + }); }); } } else { @@ -947,7 +961,9 @@ class Parser { } if (containsContext) { - resKeys.push(`${key}${contextSeparator}${options.context}`); + contextValues.forEach(contextValue => { + resKeys.push(`${key}${contextSeparator}${contextValue}`); + }); } } diff --git a/test/fixtures/context-plural.js b/test/fixtures/context-plural.js index 2aca0dd..af23ed2 100644 --- a/test/fixtures/context-plural.js +++ b/test/fixtures/context-plural.js @@ -2,5 +2,9 @@ i18next.t('friend', { context: 'male', count: 1 }); // output: 'A boyfriend' i18next.t('friend', { context: 'female', count: 1 }); // output: 'A girlfriend' i18next.t('friend', { context: 'male', count: 100 }); // output: '100 boyfriends' i18next.t('friend', { context: 'female', count: 100 }); // output: '100 girlfriends' + i18next.t('friendWithDefaultValue', '{{count}} boyfriend', { context: 'male', count: 100 }); // output: '100 boyfriends' i18next.t('friendWithDefaultValue', '{{count}} girlfriend', { context: 'female', count: 100 }); // output: '100 girlfriends' + +i18next.t('friendDynamic', { context: dynamicVal, count: 1 }); +i18next.t('friendDynamic', { context: dynamicVal, count: 100 }); diff --git a/test/fixtures/context.js b/test/fixtures/context.js index 59f213d..ae799a3 100644 --- a/test/fixtures/context.js +++ b/test/fixtures/context.js @@ -1,2 +1,3 @@ i18next.t('friend', {context: 'male'}); // output: 'A boyfriend' i18next.t('friend', {context: 'female'}); // output: 'A girlfriend' +i18next.t('friendDynamic', {context: dynamicVal}); diff --git a/test/parser.js b/test/parser.js index a01ed8e..c7a3b6f 100644 --- a/test/parser.js +++ b/test/parser.js @@ -863,7 +863,8 @@ test('Context', (t) => { translation: { 'friend': '', 'friend_male': '', - 'friend_female': '' + 'friend_female': '', + 'friendDynamic': '', } } }); @@ -888,7 +889,8 @@ test('Context', (t) => { en: { translation: { 'friend': '', - 'friend_male': '' + 'friend_male': '', + 'friendDynamic': '', } } }); @@ -902,7 +904,9 @@ test('Context with plural combined', (t) => { const content = fs.readFileSync(path.resolve(__dirname, 'fixtures/context-plural.js'), 'utf-8'); test('Default options', (t) => { - const parser = new Parser(); + const parser = new Parser({ + contextDefaultValues: ['male', 'female'], + }); parser.parseFuncFromString(content); t.same(parser.get(), { en: { @@ -918,7 +922,13 @@ test('Context with plural combined', (t) => { 'friendWithDefaultValue_male': '{{count}} boyfriend', 'friendWithDefaultValue_male_plural': '{{count}} boyfriend', 'friendWithDefaultValue_female': '{{count}} girlfriend', - 'friendWithDefaultValue_female_plural': '{{count}} girlfriend' + 'friendWithDefaultValue_female_plural': '{{count}} girlfriend', + 'friendDynamic': '', + 'friendDynamic_plural': '', + 'friendDynamic_male': '', + 'friendDynamic_male_plural': '', + 'friendDynamic_female': '', + 'friendDynamic_female_plural': '', } } }); @@ -939,7 +949,8 @@ test('Context with plural combined', (t) => { 'friend_female': '', 'friendWithDefaultValue': '{{count}} boyfriend', 'friendWithDefaultValue_male': '{{count}} boyfriend', - 'friendWithDefaultValue_female': '{{count}} girlfriend' + 'friendWithDefaultValue_female': '{{count}} girlfriend', + 'friendDynamic': '', } } }); @@ -950,6 +961,7 @@ test('Context with plural combined', (t) => { const parser = new Parser({ context: true, contextFallback: false, + contextDefaultValues: ['male', 'female'], plural: false }); parser.parseFuncFromString(content); @@ -959,7 +971,9 @@ test('Context with plural combined', (t) => { 'friend_male': '', 'friend_female': '', 'friendWithDefaultValue_male': '{{count}} boyfriend', - 'friendWithDefaultValue_female': '{{count}} girlfriend' + 'friendWithDefaultValue_female': '{{count}} girlfriend', + 'friendDynamic_male': '', + 'friendDynamic_female': '', } } }); @@ -978,7 +992,9 @@ test('Context with plural combined', (t) => { 'friend': '', 'friend_plural': '', 'friendWithDefaultValue': '{{count}} boyfriend', - 'friendWithDefaultValue_plural': '{{count}} boyfriend' + 'friendWithDefaultValue_plural': '{{count}} boyfriend', + 'friendDynamic': '', + 'friendDynamic_plural': '', } } }); @@ -996,7 +1012,8 @@ test('Context with plural combined', (t) => { en: { translation: { 'friend_plural': '', - 'friendWithDefaultValue_plural': '{{count}} boyfriend' + 'friendWithDefaultValue_plural': '{{count}} boyfriend', + 'friendDynamic_plural': '', } } });