diff --git a/__tests__/transCore.test.js b/__tests__/transCore.test.js index dc344e76..c87b3114 100644 --- a/__tests__/transCore.test.js +++ b/__tests__/transCore.test.js @@ -21,6 +21,14 @@ const nsInterpolate = { key_2: 'message 2', } +const nsPlural = { + key_1: { + 0: 'No messages', + one: '{{count}} message', + other: '{{count}} messages', + }, +} + describe('transCore', () => { test('should return an object of root keys', async () => { const t = transCore({ @@ -100,4 +108,30 @@ describe('transCore', () => { expected ) }) + + test('should work with pluralization', async () => { + const t = transCore({ + config: {}, + allNamespaces: { nsObject: nsPlural }, + pluralRules: { + select: (count) => (Math.abs(count) === 1 ? 'one' : 'other'), + }, + lang: 'en', + }) + + const oneCount = 1 + const otherCount = 4 + const oneExpected = '1 message' + const otherExpected = '4 messages' + + expect(typeof t).toBe('function') + expect(t('nsObject:key_1', { count: oneCount })).toEqual(oneExpected) + expect(t('nsObject:key_1', { count: otherCount })).toEqual(otherExpected) + expect( + t('nsObject:key_2', { count: oneCount }, { default: nsPlural.key_1 }) + ).toEqual(oneExpected) + expect( + t('nsObject:key_2', { count: otherCount }, { default: nsPlural.key_1 }) + ).toEqual(otherExpected) + }) }) diff --git a/src/index.tsx b/src/index.tsx index 14ec7598..2c53f6a1 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -12,7 +12,7 @@ export type Translate = ( options?: { returnObjects?: boolean fallback?: string | string[] - default?: string + default?: string | Record ns?: string } ) => T diff --git a/src/transCore.tsx b/src/transCore.tsx index 6a8ae658..358ade28 100644 --- a/src/transCore.tsx +++ b/src/transCore.tsx @@ -7,6 +7,8 @@ import { } from '.' import { Translate } from './index' +const PLURAL_KEY_REGEX = /^(\d+|zero|one|two|few|many|other)$/ + function splitNsKey(key: string, nsSeparator: string | false) { if (!nsSeparator) return { i18nKey: key } const i = key.indexOf(nsSeparator) @@ -41,7 +43,7 @@ export default function transCore({ const dic = (namespace && allNamespaces[namespace]) || {} const keyWithPlural = plural(pluralRules, dic, i18nKey, config, query) - const value = getDicValue(dic, keyWithPlural, config, options) + let value = getDicValue(dic, keyWithPlural, config, options) const empty = typeof value === 'undefined' || @@ -70,11 +72,20 @@ export default function transCore({ } if (empty && options?.default && fallbacks?.length == 0) { - return interpolation({ text: options?.default, query, config, lang }) - } - - // no need to try interpolation - if (empty) { + // Use the default value if necessary + value = + typeof options.default === 'string' || + typeof options.default?.other !== 'string' || + Object.keys(options.default).some((x) => !PLURAL_KEY_REGEX.test(x)) + ? options.default + : getDicValue( + options.default || {}, + pluralRules.select(query?.count || 0), + config, + options + ) || options.default.other + } else if (empty) { + // no need to try interpolation return k }