Skip to content

Commit

Permalink
made sure that the i18n does use navigator.languages instead of `na…
Browse files Browse the repository at this point in the history
…vigator.language` for automatic language detection (#4244)
  • Loading branch information
CommanderStorm authored Jan 7, 2024
1 parent 458cdf9 commit 7635ab5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ for (let lang in languageList) {

const rtlLangs = [ "fa", "ar-SY", "ur" ];

export const currentLocale = () => localStorage.locale
|| languageList[navigator.language] && navigator.language
|| languageList[navigator.language.substring(0, 2)] && navigator.language.substring(0, 2)
|| "en";
/**
* Find the best matching locale to display
* If no locale can be matched, the default is "en"
* @returns {string} the locale that should be displayed
*/
export function currentLocale() {
const potentialLocales = [ localStorage.locale, navigator.language, navigator.language.substring(0, 2), ...navigator.languages ];
const availableLocales = potentialLocales.filter(l => languageList[l]);
return availableLocales[0] || "en";
}

export const localeDirection = () => {
return rtlLangs.includes(currentLocale()) ? "rtl" : "ltr";
Expand Down
12 changes: 8 additions & 4 deletions test/cypress/unit/i18n.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ describe("Test i18n.js", () => {

it("currentLocale()", () => {
const setLanguage = (language) => {
Object.defineProperty(window.navigator, 'language', {
value: language,
writable: true
Object.defineProperty(window.navigator, 'language', {
value: language,
writable: true
});
Object.defineProperty(window.navigator, 'languages', {
value: [language],
writable: true
});
}
setLanguage('en-EN');
Expand Down Expand Up @@ -41,4 +45,4 @@ describe("Test i18n.js", () => {
expect(currentLocale()).equal("zh-HK");
});

});
});

0 comments on commit 7635ab5

Please sign in to comment.