Skip to content

Commit 70f2860

Browse files
committed
i18n 初始化提前
1 parent 5f99601 commit 70f2860

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/i18n.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import type { createI18n } from "vue-i18n";
22
type I18nOpt = Parameters<typeof createI18n>[0];
33

4+
const locale = (() => {
5+
if (localStorage) {
6+
const localLocale = localStorage.getItem("lang");
7+
const langStr = localLocale ?? navigator.language;
8+
if (langStr.startsWith("zh")) return "zh-CN";
9+
if (langStr.startsWith("en")) return "en-US";
10+
}
11+
return "en-US";
12+
})();
13+
14+
if (localStorage && !localStorage.getItem("lang"))
15+
localStorage.setItem("lang", locale);
16+
417
export default {
518
legacy: false,
6-
locale: "zh-CN",
19+
locale,
720
messages: {
821
"zh-CN": {
922
buttons: {

src/ui/navbar.vue

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,26 @@
4646
</template>
4747

4848
<script setup lang="ts">
49-
import { onMounted, watch } from "vue";
49+
import { watch } from "vue";
5050
import { useI18n } from "vue-i18n";
5151
5252
const { locale, t } = useI18n();
53-
onMounted(() => {
54-
const localLocale = localStorage.getItem("lang");
55-
const lang = localLocale ?? navigator.language;
56-
if (lang.startsWith("zh")) locale.value = "zh-CN";
57-
if (lang.startsWith("en")) locale.value = "en-US";
53+
54+
watch(locale, () => {
5855
document.documentElement.setAttribute("lang", locale.value);
59-
watch(locale, () => {
60-
document.documentElement.setAttribute("lang", locale.value);
61-
localStorage.setItem("lang", locale.value);
62-
});
56+
localStorage.setItem("lang", locale.value);
6357
});
6458
6559
import SIconLanguage from "@/ui/icons/language.vue";
6660
import SIconGtihub from "@/ui/icons/github.vue";
6761
import SIconAutoTheme from "@/ui/icons/autotheme.vue";
6862
69-
function openGithub() {
63+
const openGithub = () =>
7064
window.open("https://github.com/Linho1219/function-plot-GUI");
71-
}
7265
7366
const emit = defineEmits(["toggleDrawer"]);
7467
75-
import { useTheme } from "@/consts";
68+
import { useTheme } from "@/states";
7669
const theme = useTheme();
7770
</script>
7871

0 commit comments

Comments
 (0)