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

fix(i18n): correct usage of language str having underscore #922

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions web/src/app/utils.js
Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/App.jsx
Expand Up @@ -11,7 +11,7 @@ import ActionBar from "./ActionBar";
import Preferences from "./Preferences";
import subscriptionManager from "../app/SubscriptionManager";
import userManager from "../app/UserManager";
import { expandUrl } from "../app/utils";
import { expandUrl, getKebabCaseLangStr } from "../app/utils";
import ErrorBoundary from "./ErrorBoundary";
import routes from "./routes";
import { useAccountListener, useBackgroundProcesses, useConnectionListeners, useWebPushTopics } from "./hooks";
Expand Down Expand Up @@ -56,7 +56,7 @@ const App = () => {
);

useEffect(() => {
document.documentElement.setAttribute("lang", i18n.language);
document.documentElement.setAttribute("lang", getKebabCaseLangStr(i18n.language));
document.dir = languageDir;
}, [i18n.language, languageDir]);

Expand Down