mirror of
https://github.com/usememos/memos.git
synced 2025-02-20 21:30:55 +01:00
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: boojack <stevenlgtm@gmail.com>
This commit is contained in:
parent
8c5ba63f8c
commit
ddf1eb0219
@ -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",
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
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,7 +16,15 @@ 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 = {
|
||||||
|
order: ["navigator"],
|
||||||
|
};
|
||||||
|
|
||||||
|
i18n
|
||||||
|
.use(LanguageDetector)
|
||||||
|
.use(initReactI18next)
|
||||||
|
.init({
|
||||||
|
detection: DETECTION_OPTIONS,
|
||||||
resources: {
|
resources: {
|
||||||
en: {
|
en: {
|
||||||
translation: enLocale,
|
translation: enLocale,
|
||||||
@ -60,8 +69,7 @@ i18n.use(initReactI18next).init({
|
|||||||
translation: koLocale,
|
translation: koLocale,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
lng: "en",
|
|
||||||
fallbackLng: "en",
|
fallbackLng: "en",
|
||||||
});
|
});
|
||||||
|
|
||||||
export default i18n;
|
export default i18n;
|
||||||
|
@ -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 () => {
|
|||||||
externalUrl: "",
|
externalUrl: "",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
defaultGlobalState.locale = customizedProfile.locale;
|
defaultGlobalState.locale = storageLocale || convertLanguageCodeToLocale(i18n.language);
|
||||||
defaultGlobalState.appearance = customizedProfile.appearance;
|
defaultGlobalState.appearance = customizedProfile.appearance;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
7
web/src/utils/convertLanguageCodeToLocale.ts
Normal file
7
web/src/utils/convertLanguageCodeToLocale.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
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;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user