fix: objects counted as plural incorrectly

This commit is contained in:
Lewis Crichton 2024-06-23 21:18:43 +01:00
parent cc7675eccb
commit e55c6f99e2
No known key found for this signature in database

View file

@ -105,7 +105,8 @@ export function t(key: string, variables?: Record<string, any>): string {
const translation = _t(key, loadedLocale);
if (typeof translation !== "string") {
if (!variables || !variables.count) throw new Error(`translation key ${key} is an object (requires plurality?)`);
if (!variables || !variables.count)
throw new Error(`translation key ${key} is an object (is it a plural?)`);
if (variables.count) {
const pluralTag: Intl.LDMLPluralRule = variables.count === 0 ? "zero" :
@ -113,8 +114,10 @@ export function t(key: string, variables?: Record<string, any>): string {
if (translation[pluralTag]) {
return format(translation[pluralTag]!, variables);
} else {
} else if (translation.other) {
return format(translation.other, variables);
} else {
throw new Error(`translation key ${key} is an object and doesn't seem to be plural`);
}
}
}