Skip to content

Commit 421b4ad

Browse files
committed
fix(i18n): implement createPluralRule function for better plural handling
1 parent a7b13cd commit 421b4ad

1 file changed

Lines changed: 61 additions & 36 deletions

File tree

config/i18n.ts

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ export const countryLocaleVariants: Record<string, (LocaleObjectData & { country
7878
],*/
7979
}
8080

81+
function createPluralRule(locale: string, mapping: Record<string, number>) {
82+
return (choice: number, choicesLength: number) => {
83+
const name = new Intl.PluralRules(locale).select(choice)
84+
const plural = mapping[name] || 0
85+
86+
// In case translation doesn't have all plural forms, use the last available form
87+
if (plural > choicesLength - 1) {
88+
if (import.meta.dev) {
89+
console.warn(
90+
`Plural form index ${plural} for choice ${choice} exceeds available forms ${choicesLength} for locale ${locale}.`,
91+
)
92+
}
93+
return choicesLength - 1
94+
}
95+
96+
return plural
97+
}
98+
}
99+
81100
const locales: (LocaleObjectData | (Omit<LocaleObjectData, 'code'> & { code: string }))[] = [
82101
{
83102
code: 'en',
@@ -89,10 +108,11 @@ const locales: (LocaleObjectData | (Omit<LocaleObjectData, 'code'> & { code: str
89108
file: 'ar.json',
90109
name: 'العربية',
91110
dir: 'rtl',
92-
pluralRule: (choice: number) => {
93-
const name = new Intl.PluralRules('ar-EG').select(choice)
94-
return { zero: 0, one: 1, two: 2, few: 3, many: 4, other: 5 }[name]
95-
},
111+
pluralRule: createPluralRule('ar-EG', { zero: 0, one: 1, two: 2, few: 3, many: 4, other: 5 }),
112+
// pluralRule: (choice: number) => {
113+
// const name = new Intl.PluralRules('ar-EG').select(choice)
114+
// return { zero: 0, one: 1, two: 2, few: 3, many: 4, other: 5 }[name]
115+
// },
96116
},
97117
{
98118
code: 'az-AZ',
@@ -148,10 +168,11 @@ const locales: (LocaleObjectData | (Omit<LocaleObjectData, 'code'> & { code: str
148168
code: 'hu-HU',
149169
file: 'hu-HU.json',
150170
name: 'Magyar',
151-
pluralRule: (choice: number) => {
152-
const name = new Intl.PluralRules('hu-HU').select(choice)
153-
return { zero: 0, one: 0, two: 1, few: 1, many: 1, other: 1 }[name]
154-
},
171+
pluralRule: createPluralRule('hu-HU', { zero: 0, one: 0, two: 1, few: 1, many: 1, other: 1 }),
172+
// pluralRule: (choice: number) => {
173+
// const name = new Intl.PluralRules('hu-HU').select(choice)
174+
// return { zero: 0, one: 0, two: 1, few: 1, many: 1, other: 1 }[name]
175+
// },
155176
},
156177
{
157178
code: 'zh-CN',
@@ -197,33 +218,35 @@ const locales: (LocaleObjectData | (Omit<LocaleObjectData, 'code'> & { code: str
197218
code: 'ru-RU',
198219
file: 'ru-RU.json',
199220
name: 'Русский',
200-
pluralRule: (choice: number) => {
201-
const name = new Intl.PluralRules('ru-RU').select(choice)
202-
return { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 3 }[name]
203-
},
221+
pluralRule: createPluralRule('ru-RU', { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 3 }),
222+
// pluralRule: (choice: number) => {
223+
// const name = new Intl.PluralRules('ru-RU').select(choice)
224+
// return { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 3 }[name]
225+
// },
204226
},
205227
{
206228
code: 'uk-UA',
207229
file: 'uk-UA.json',
208230
name: 'Українська',
209-
pluralRule: (choice: number, choicesLength: number) => {
210-
if (choice === 0) return 0
231+
pluralRule: createPluralRule('uk-UA', { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }),
232+
// pluralRule: (choice: number, choicesLength: number) => {
233+
// if (choice === 0) return 0
211234

212-
const name = new Intl.PluralRules('uk-UA').select(choice)
213-
const plural = { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }[name]
235+
// const name = new Intl.PluralRules('uk-UA').select(choice)
236+
// const plural = { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }[name]
214237

215-
// In case translation doesn't have all plural forms, use the last available form
216-
if (plural > choicesLength - 1) {
217-
if (import.meta.dev) {
218-
console.warn(
219-
`Plural form index ${plural} for choice ${choice} exceeds available forms ${choicesLength} for locale uk-UA.`,
220-
)
221-
}
222-
return choicesLength - 1
223-
}
238+
// // In case translation doesn't have all plural forms, use the last available form
239+
// if (plural > choicesLength - 1) {
240+
// if (import.meta.dev) {
241+
// console.warn(
242+
// `Plural form index ${plural} for choice ${choice} exceeds available forms ${choicesLength} for locale uk-UA.`,
243+
// )
244+
// }
245+
// return choicesLength - 1
246+
// }
224247

225-
return plural
226-
},
248+
// return plural
249+
// },
227250
},
228251
/*{
229252
code: 'ru-RU',
@@ -238,10 +261,11 @@ const locales: (LocaleObjectData | (Omit<LocaleObjectData, 'code'> & { code: str
238261
code: 'cs-CZ',
239262
file: 'cs-CZ.json',
240263
name: 'Čeština',
241-
pluralRule: (choice: number) => {
242-
const name = new Intl.PluralRules('cs-CZ').select(choice)
243-
return { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 2 }[name]
244-
},
264+
pluralRule: createPluralRule('cs-CZ', { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 2 }),
265+
// pluralRule: (choice: number) => {
266+
// const name = new Intl.PluralRules('cs-CZ').select(choice)
267+
// return { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 2 }[name]
268+
// },
245269
} /*
246270
{
247271
code: 'pl-PL',
@@ -299,12 +323,13 @@ const locales: (LocaleObjectData | (Omit<LocaleObjectData, 'code'> & { code: str
299323
code: 'pl-PL',
300324
file: 'pl-PL.json',
301325
name: 'Polski',
302-
pluralRule: (choice: number) => {
303-
if (choice === 0) return 0
326+
pluralRule: createPluralRule('pl-PL', { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }),
327+
// pluralRule: (choice: number) => {
328+
// if (choice === 0) return 0
304329

305-
const name = new Intl.PluralRules('pl-PL').select(choice)
306-
return { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }[name]
307-
},
330+
// const name = new Intl.PluralRules('pl-PL').select(choice)
331+
// return { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }[name]
332+
// },
308333
},
309334
{
310335
code: 'pt-BR',

0 commit comments

Comments
 (0)