Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion public/locales
Submodule locales updated 107 files
38 changes: 20 additions & 18 deletions src/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
flx-sta marked this conversation as resolved.
Show resolved Hide resolved
if (isNaN(Number(amount))) {
console.warn(`i18nMoneyFormatter: value "${amount}" is not a number!`);
}

return `@[MONEY]{${i18next.t("common:money", { amount })}}`;
}

//#region Exports

/**
Expand Down Expand Up @@ -249,24 +265,10 @@ export async function initI18n(): Promise<void> {
postProcess: [ "korean-postposition" ],
});

// Input: {{myMoneyValue, money}}
// Output: @[MONEY]{₽100,000,000} (useful for BBCode coloring of text)
// If you don't want the BBCode tag applied, just use 'number' formatter
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}}`;
}
});

if (i18next.services.formatter) {
i18next.services.formatter.add("money", i18nMoneyFormatter);
}

await initFonts(localStorage.getItem("prLang") ?? undefined);
}
Expand Down
Loading