fix(i18n): correct usage of language str having underline

This commit is contained in:
imkero
2023-10-19 07:48:06 +00:00
parent b7f37138f8
commit ef45ea5a50
2 changed files with 10 additions and 4 deletions

View File

@@ -130,14 +130,20 @@ export const hashCode = (s) => {
return hash;
};
/**
* convert `i18n.language` style str (e.g.: `en_US`) to kebab-case (e.g.: `en-US`),
* which is expected by `<html lang>` and `Intl.DateTimeFormat`
*/
export const getKebabCaseLangStr = (language) => language.replace(/_/g, '-');
export const formatShortDateTime = (timestamp, language) =>
new Intl.DateTimeFormat(language, {
new Intl.DateTimeFormat(getKebabCaseLangStr(language), {
dateStyle: "short",
timeStyle: "short",
}).format(new Date(timestamp * 1000));
export const formatShortDate = (timestamp, language) =>
new Intl.DateTimeFormat(language, { dateStyle: "short" }).format(new Date(timestamp * 1000));
new Intl.DateTimeFormat(getKebabCaseLangStr(language), { dateStyle: "short" }).format(new Date(timestamp * 1000));
export const formatBytes = (bytes, decimals = 2) => {
if (bytes === 0) return "0 bytes";