Unverified Commit ddf1eb02 authored by CorrectRoadH's avatar CorrectRoadH Committed by GitHub

feat: automatically change language on first launch (#1278)

* feat: automatically change language to browser language on first launch(#1238)

* Update web/src/store/module/global.ts

* chroe: rename languageCodeCovert to convertLanguageCodeToLocale

---------
Co-authored-by: 's avatarboojack <stevenlgtm@gmail.com>
parent 8c5ba63f
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
"dayjs": "^1.11.3", "dayjs": "^1.11.3",
"highlight.js": "^11.6.0", "highlight.js": "^11.6.0",
"i18next": "^21.9.2", "i18next": "^21.9.2",
"i18next-browser-languagedetector": "^7.0.1",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"lucide-react": "^0.105.0", "lucide-react": "^0.105.0",
"qrcode.react": "^3.1.0", "qrcode.react": "^3.1.0",
......
import i18n from "i18next"; import i18n from "i18next";
import { initReactI18next } from "react-i18next"; import { initReactI18next } from "react-i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import enLocale from "./locales/en.json"; import enLocale from "./locales/en.json";
import zhLocale from "./locales/zh.json"; import zhLocale from "./locales/zh.json";
import viLocale from "./locales/vi.json"; import viLocale from "./locales/vi.json";
...@@ -15,53 +16,60 @@ import hantLocale from "./locales/zh-Hant.json"; ...@@ -15,53 +16,60 @@ import hantLocale from "./locales/zh-Hant.json";
import trLocale from "./locales/tr.json"; import trLocale from "./locales/tr.json";
import koLocale from "./locales/ko.json"; import koLocale from "./locales/ko.json";
i18n.use(initReactI18next).init({ const DETECTION_OPTIONS = {
resources: { order: ["navigator"],
en: { };
translation: enLocale,
}, i18n
zh: { .use(LanguageDetector)
translation: zhLocale, .use(initReactI18next)
}, .init({
vi: { detection: DETECTION_OPTIONS,
translation: viLocale, resources: {
}, en: {
fr: { translation: enLocale,
translation: frLocale, },
}, zh: {
nl: { translation: zhLocale,
translation: nlLocale, },
}, vi: {
sv: { translation: viLocale,
translation: svLocale, },
}, fr: {
de: { translation: frLocale,
translation: deLocale, },
}, nl: {
es: { translation: nlLocale,
translation: esLocale, },
}, sv: {
uk: { translation: svLocale,
translation: ukLocale, },
}, de: {
ru: { translation: deLocale,
translation: ruLocale, },
}, es: {
it: { translation: esLocale,
translation: itLocale, },
}, uk: {
hant: { translation: ukLocale,
translation: hantLocale, },
}, ru: {
tr: { translation: ruLocale,
translation: trLocale, },
}, it: {
ko: { translation: itLocale,
translation: koLocale, },
hant: {
translation: hantLocale,
},
tr: {
translation: trLocale,
},
ko: {
translation: koLocale,
},
}, },
}, fallbackLng: "en",
lng: "en", });
fallbackLng: "en",
});
export default i18n; export default i18n;
...@@ -2,6 +2,8 @@ import * as api from "../../helpers/api"; ...@@ -2,6 +2,8 @@ import * as api from "../../helpers/api";
import * as storage from "../../helpers/storage"; import * as storage from "../../helpers/storage";
import store, { useAppSelector } from "../"; import store, { useAppSelector } from "../";
import { setAppearance, setGlobalState, setLocale } from "../reducer/global"; import { setAppearance, setGlobalState, setLocale } from "../reducer/global";
import i18n from "../../i18n";
import { convertLanguageCodeToLocale } from "../../utils/convertLanguageCodeToLocale";
export const initialGlobalState = async () => { export const initialGlobalState = async () => {
const defaultGlobalState = { const defaultGlobalState = {
...@@ -46,7 +48,7 @@ export const initialGlobalState = async () => { ...@@ -46,7 +48,7 @@ export const initialGlobalState = async () => {
externalUrl: "", externalUrl: "",
}, },
}; };
defaultGlobalState.locale = customizedProfile.locale; defaultGlobalState.locale = storageLocale || convertLanguageCodeToLocale(i18n.language);
defaultGlobalState.appearance = customizedProfile.appearance; defaultGlobalState.appearance = customizedProfile.appearance;
} }
} catch (error) { } catch (error) {
......
export const convertLanguageCodeToLocale = (codename: string): Locale => {
if (codename === "zh-TW" || codename === "zh-HK") {
return "hant";
}
const shortCode = codename.substring(0, 2);
return shortCode as Locale;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment