提交 | 用户 | 时间
|
314507
|
1 |
<script lang="ts" setup> |
H |
2 |
import { useLocaleStore } from '@/store/modules/locale' |
|
3 |
import { useLocale } from '@/hooks/web/useLocale' |
|
4 |
import { propTypes } from '@/utils/propTypes' |
|
5 |
import { useDesign } from '@/hooks/web/useDesign' |
|
6 |
|
|
7 |
defineOptions({ name: 'LocaleDropdown' }) |
|
8 |
|
|
9 |
const { getPrefixCls } = useDesign() |
|
10 |
|
|
11 |
const prefixCls = getPrefixCls('locale-dropdown') |
|
12 |
|
|
13 |
defineProps({ |
|
14 |
color: propTypes.string.def('') |
|
15 |
}) |
|
16 |
|
|
17 |
const localeStore = useLocaleStore() |
|
18 |
|
|
19 |
const langMap = computed(() => localeStore.getLocaleMap) |
|
20 |
|
|
21 |
const currentLang = computed(() => localeStore.getCurrentLocale) |
|
22 |
|
|
23 |
const setLang = (lang: LocaleType) => { |
|
24 |
if (lang === unref(currentLang).lang) return |
|
25 |
// 需要重新加载页面让整个语言多初始化 |
|
26 |
window.location.reload() |
|
27 |
localeStore.setCurrentLocale({ |
|
28 |
lang |
|
29 |
}) |
|
30 |
const { changeLocale } = useLocale() |
|
31 |
changeLocale(lang) |
|
32 |
} |
|
33 |
</script> |
|
34 |
|
|
35 |
<template> |
|
36 |
<ElDropdown :class="prefixCls" trigger="click" @command="setLang"> |
|
37 |
<Icon |
|
38 |
:class="$attrs.class" |
|
39 |
:color="color" |
|
40 |
:size="18" |
|
41 |
class="cursor-pointer !p-0" |
|
42 |
icon="ion:language-sharp" |
|
43 |
/> |
|
44 |
<template #dropdown> |
|
45 |
<ElDropdownMenu> |
|
46 |
<ElDropdownItem v-for="item in langMap" :key="item.lang" :command="item.lang"> |
|
47 |
{{ item.name }} |
|
48 |
</ElDropdownItem> |
|
49 |
</ElDropdownMenu> |
|
50 |
</template> |
|
51 |
</ElDropdown> |
|
52 |
</template> |