[READ-ONLY] a fast, modern browser for the npm registry
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(i18n): fallback on missing plural translation (#860)

authored by

Bobbie Goede and committed by
GitHub
c57d3b83 146963a6

+26 -28
+26 -28
config/i18n.ts
··· 78 78 ],*/ 79 79 } 80 80 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 + // oxlint-disable-next-line no-console -- warn logging 90 + console.warn( 91 + `Plural form index ${plural} for choice ${choice} exceeds available forms ${choicesLength} for locale ${locale}.`, 92 + ) 93 + } 94 + return choicesLength - 1 95 + } 96 + 97 + return plural 98 + } 99 + } 100 + 81 101 const locales: (LocaleObjectData | (Omit<LocaleObjectData, 'code'> & { code: string }))[] = [ 82 102 { 83 103 code: 'en', ··· 89 109 file: 'ar.json', 90 110 name: 'العربية', 91 111 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 - }, 112 + pluralRule: createPluralRule('ar-EG', { zero: 0, one: 1, two: 2, few: 3, many: 4, other: 5 }), 96 113 }, 97 114 { 98 115 code: 'az-AZ', ··· 148 165 code: 'hu-HU', 149 166 file: 'hu-HU.json', 150 167 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 - }, 168 + pluralRule: createPluralRule('hu-HU', { zero: 0, one: 0, two: 1, few: 1, many: 1, other: 1 }), 155 169 }, 156 170 { 157 171 code: 'zh-CN', ··· 197 211 code: 'ru-RU', 198 212 file: 'ru-RU.json', 199 213 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 - }, 214 + pluralRule: createPluralRule('ru-RU', { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 3 }), 204 215 }, 205 216 { 206 217 code: 'uk-UA', 207 218 file: 'uk-UA.json', 208 219 name: 'Українська', 209 - pluralRule: (choice: number) => { 210 - if (choice === 0) return 0 211 - 212 - const name = new Intl.PluralRules('uk-UA').select(choice) 213 - return { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }[name] 214 - }, 220 + pluralRule: createPluralRule('uk-UA', { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }), 215 221 }, 216 222 /*{ 217 223 code: 'ru-RU', ··· 226 232 code: 'cs-CZ', 227 233 file: 'cs-CZ.json', 228 234 name: 'Čeština', 229 - pluralRule: (choice: number) => { 230 - const name = new Intl.PluralRules('cs-CZ').select(choice) 231 - return { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 2 }[name] 232 - }, 235 + pluralRule: createPluralRule('cs-CZ', { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 2 }), 233 236 } /* 234 237 { 235 238 code: 'pl-PL', ··· 287 290 code: 'pl-PL', 288 291 file: 'pl-PL.json', 289 292 name: 'Polski', 290 - pluralRule: (choice: number) => { 291 - if (choice === 0) return 0 292 - 293 - const name = new Intl.PluralRules('pl-PL').select(choice) 294 - return { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }[name] 295 - }, 293 + pluralRule: createPluralRule('pl-PL', { zero: 0, one: 1, two: 0, few: 2, many: 3, other: 4 }), 296 294 }, 297 295 { 298 296 code: 'pt-BR',