Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to support pluralizable objects for default string #901

Open
wants to merge 5 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions __tests__/transCore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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)
})
})
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type Translate = <T = string>(
options?: {
returnObjects?: boolean
fallback?: string | string[]
default?: string
default?: string | Record<string, string>
ns?: string
}
) => T
Expand Down
23 changes: 17 additions & 6 deletions src/transCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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' ||
Expand Down Expand Up @@ -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
}

Expand Down