[Qol] Make i18n money formatter controlled by translators (#4550)

* fix: i18n money formatter

* fix wrongful console.warn on i18n money formatter

* update locales submodule

update reference to `56eeb809eb5a2de40cfc5bc6128a78bef14deea9` (from `3ccef8472dd7cc7c362538489954cb8fdad27e5f`)
This commit is contained in:
flx-sta 2024-10-05 14:01:41 -07:00 committed by GitHub
parent f562a76332
commit 42b75e8440
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 19 deletions

@ -1 +1 @@
Subproject commit 3ccef8472dd7cc7c362538489954cb8fdad27e5f Subproject commit 56eeb809eb5a2de40cfc5bc6128a78bef14deea9

View File

@ -100,6 +100,22 @@ async function initFonts(language: string | undefined) {
} }
} }
/**
* I18n money formatter with. (useful for BBCode coloring of text)\
* *If you don't want the BBCode tag applied, just use 'number' formatter*
* @example Input: `{{myMoneyValue, money}}`
* Output: `@[MONEY]{₽100,000,000}`
* @param amount the money amount
* @returns a money formatted string
*/
function i18nMoneyFormatter(amount: any): string {
if (isNaN(Number(amount))) {
console.warn(`i18nMoneyFormatter: value "${amount}" is not a number!`);
}
return `@[MONEY]{${i18next.t("common:money", { amount })}}`;
}
//#region Exports //#region Exports
/** /**
@ -249,24 +265,10 @@ export async function initI18n(): Promise<void> {
postProcess: [ "korean-postposition" ], postProcess: [ "korean-postposition" ],
}); });
// Input: {{myMoneyValue, money}}
// Output: @[MONEY]{₽100,000,000} (useful for BBCode coloring of text) if (i18next.services.formatter) {
// If you don't want the BBCode tag applied, just use 'number' formatter i18next.services.formatter.add("money", i18nMoneyFormatter);
i18next.services.formatter?.add("money", (value, lng, options) => { }
const numberFormattedString = Intl.NumberFormat(lng, options).format(value);
switch (lng) {
case "ja":
return `@[MONEY]{${numberFormattedString}}円`;
case "de":
case "es":
case "fr":
case "it":
return `@[MONEY]{${numberFormattedString} ₽}`;
default:
// English and other languages that use same format
return `@[MONEY]{₽${numberFormattedString}}`;
}
});
await initFonts(localStorage.getItem("prLang") ?? undefined); await initFonts(localStorage.getItem("prLang") ?? undefined);
} }