Skip to content

Commit

Permalink
src: prefer cockpit's language settings over browser default
Browse files Browse the repository at this point in the history
  • Loading branch information
jkozol committed Feb 21, 2024
1 parent cc3e583 commit bc9df6c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ const Content = () => {
};

const main = async () => {
// use language without region code
const userLocale = window.navigator.language.split("-")[0];
// cockpit's language is stored in localStorage as `cockpit.lang`
// https://github.com/cockpit-project/cockpit/blob/bafd1067f7f8b9d16479c90bd77dcdda08f044ce/pkg/shell/shell-modals.jsx#L104C38-L104C50
const cockpitLocale = localStorage.getItem("cockpit.lang");
// browser default language
const userLocale = window.navigator.language;
// use cockpit's language if it's available
// otherwise use the browser's default language
const locale = cockpitLocale || userLocale;
// strip region code
const language = locale.split("-")[0];
let translations;
try {
translations = (await import(`../translations/compiled/${userLocale}.json`))
translations = (await import(`../translations/compiled/${language}.json`))
.default;
} catch (error) {
console.error(error);
Expand All @@ -59,9 +67,9 @@ const main = async () => {
ReactDOM.render(
<Provider store={store}>
<IntlProvider
key={userLocale}
key={language}
defaultLocale="en"
locale={userLocale}
locale={language}
messages={translations}
>
<Content />
Expand Down

0 comments on commit bc9df6c

Please sign in to comment.