feat: plurality
This commit is contained in:
parent
c0111169b8
commit
9c9a02f9bf
|
@ -80,8 +80,10 @@ function getByPath(key: string, object: any) {
|
|||
}
|
||||
}
|
||||
|
||||
type Translation = string | ({ [rule in Intl.LDMLPluralRule]?: string } & { other: string; });
|
||||
|
||||
// translation retrieval function
|
||||
function _t(key: string, bundle: any): string {
|
||||
function _t(key: string, bundle: any): Translation {
|
||||
const translation = getByPath(key, bundle);
|
||||
|
||||
if (!translation) {
|
||||
|
@ -98,14 +100,27 @@ function _t(key: string, bundle: any): string {
|
|||
/**
|
||||
* Translates a key. Soft-fails and returns the key if it is not valid.
|
||||
* @param key The key to translate.
|
||||
* @param variables The variables to interpolate into the resultant string.
|
||||
* @param variables The variables to interpolate into the resultant string. If dealing with plurals, `count` must be set.
|
||||
* @returns A translated string.
|
||||
*/
|
||||
export function $t(key: string, variables?: Record<string, any>): string {
|
||||
const translation = _t(key, loadedLocale);
|
||||
|
||||
if (!variables) return translation;
|
||||
return format(translation, variables);
|
||||
if (typeof translation !== "string") {
|
||||
if (!variables || !variables.count) throw new Error(`translation key ${key} is an object (requires plurality?)`);
|
||||
|
||||
if (variables.count) {
|
||||
const pluralTag = new Intl.PluralRules(bestLocale).select(variables.count);
|
||||
|
||||
if (translation[pluralTag]) {
|
||||
return format(translation[pluralTag]!, variables);
|
||||
} else {
|
||||
return format(translation.other, variables);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$t("vencord.hello");
|
||||
if (!variables) return translation as string;
|
||||
|
||||
return format(translation as string, variables);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
{
|
||||
"hello": "Hello {name}!"
|
||||
"hello": "Hello {name}!",
|
||||
"plural": {
|
||||
"one": "One thing.",
|
||||
"other": "{count} things."
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue