Skip to content

Commit

Permalink
refactor: set locale function
Browse files Browse the repository at this point in the history
  • Loading branch information
ikxin committed Aug 21, 2024
1 parent bc6077d commit 6e0ec7a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@elysiajs/cors": "^1.1.0",
"@elysiajs/cron": "^1.1.0",
"@elysiajs/static": "^1.1.0",
"@iconify-json/flag": "^1.1.54",
"@vueuse/core": "^11.0.1",
"@vueuse/integrations": "^11.0.1",
"axios": "^1.7.4",
Expand Down
26 changes: 13 additions & 13 deletions src/components/custom-header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { t } = useI18n()
const appStore = useAppStore()
const { setLocale } = useAppStore()
const navItems = computed(() => [
{
name: 'activate',
Expand Down Expand Up @@ -58,20 +60,18 @@ const themeIcon = computed(() => {
const themeChange = val => (appStore.theme = val)
const languagesItems = computed(() => [
const locales: LocaleItem[] = [
{
lable: t('label.zh-cn'),
lable: '简体中文',
value: 'zh-cn',
icon: 'i-languages:zh',
icon: 'i-flag:cn-4x3',
},
{
lable: t('label.en'),
lable: 'English',
value: 'en',
icon: 'i-languages:en',
icon: 'i-flag:us-4x3',
},
])
const languagesChange = (val: string) => (appStore.locale = val)
]
</script>

<template>
Expand Down Expand Up @@ -118,12 +118,12 @@ const languagesChange = (val: string) => (appStore.locale = val)
</AButton>
<template #content>
<ADoption
v-for="item in languagesItems"
:key="item.value"
@click="languagesChange(item.value)"
v-for="locale in locales"
:key="locale.value"
@click="setLocale(locale.value)"
>
<template #icon><i :class="item.icon" /></template>
<template #default>{{ item.lable }}</template>
<template #icon><i :class="locale.icon" /></template>
<template #default>{{ locale.lable }}</template>
</ADoption>
</template>
</ADropdown>
Expand Down
16 changes: 12 additions & 4 deletions src/store/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ export const useAppStore = defineStore('app', () => {
storageKey: 'theme',
})

const { locale } = useI18n()
const { locale: _locale } = useI18n()

const { language } = useNavigatorLanguage()

const _locale = useStorage(
const locale = useStorage(
'locale',
language.value?.toLocaleLowerCase() || 'zh-cn',
)

watchEffect(() => (locale.value = _locale.value))
function setLocale(val: LocaleValue) {
locale.value = val
}

return { theme, locale }
watchEffect(() => (_locale.value = locale.value))

return {
locale,
theme,
setLocale,
}
})
12 changes: 8 additions & 4 deletions src/typings/custom.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/// <reference types="vite/client" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />

declare module '*.vue' {
import type { ComponentOptions } from 'vue'
const Component: ComponentOptions
Expand All @@ -13,3 +9,11 @@ declare module '*.md' {
const Component: ComponentOptions
export default Component
}

type LocaleValue = 'zh-cn' | 'en'

interface LocaleItem {
lable: string
icon?: string
value: LocaleValue
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"paths": { "@/*": ["src/*"] },
"skipLibCheck": true,
"strict": true,
"types": ["unplugin-vue-router/client"]
"types": ["unplugin-vue-router/client", "vite/client"]
},
"include": ["**/*.vue", "**/*.ts", "**/*.d.ts"]
}

0 comments on commit 6e0ec7a

Please sign in to comment.