Skip to content

Commit 2e6f33d

Browse files
committed
feat: switch language by navigator.language
1 parent 323c626 commit 2e6f33d

File tree

5 files changed

+67
-11
lines changed

5 files changed

+67
-11
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"dependencies": {
1212
"@unocss/reset": "^0.44.5",
13+
"@vueuse/core": "^8.9.4",
1314
"vue": "^3.2.37",
1415
"vue-i18n": "9"
1516
},

pnpm-lock.yaml

+58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/locales.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const locales = {
2121
back: "Back"
2222
}
2323
},
24-
zh_cn: {
24+
zh: {
2525
basic: {
2626
o: "奥",
2727
r: "利",
@@ -65,7 +65,7 @@ const locales = {
6565
back: "元に戻す"
6666
}
6767
},
68-
de_de: {
68+
de: {
6969
basic: {
7070
o: "O | o",
7171
r: "Re | re",

src/main.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import '@unocss/reset/eric-meyer.css'
66
import App from './App.vue'
77
import messages from './locales'
88

9+
const localeKeyList = ['en', 'zh', 'ja', 'de']
10+
const currentLocale = localeKeyList.find(key => navigator.language.startsWith(key)) || 'en'
11+
912
const i18n = createI18n({
1013
legacy: false,
11-
locale: 'zh_cn',
12-
fallbackLocale: 'zh_cn',
14+
locale: currentLocale,
15+
fallbackLocale: 'zh',
1316
messages,
1417
})
1518

src/pages/Home.vue

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const emit = defineEmits<{
77
(e: 'submit', value: OreoKey[]): void
88
}>()
99
10-
const { t, locale } = useI18n()
10+
const { t } = useI18n()
1111
let oreoList = $ref<OreoKey[]>([])
1212
const oreoFormattedStr = $computed(() => formattedOreoStr(oreoList, t))
1313
@@ -99,10 +99,4 @@ const generateRandomOreo = () => {
9999
{{ t('input.generate') }}
100100
</footer>
101101
</main>
102-
<select v-model="locale" mt-3>
103-
<option value="en">en</option>
104-
<option value="zh_cn">zh</option>
105-
<option value="ja">ja</option>
106-
<option value="de_de">de</option>
107-
</select>
108102
</template>

0 commit comments

Comments
 (0)