-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
useLocale.ts
48 lines (39 loc) · 1.25 KB
/
useLocale.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Multi-language related operations
*/
import { computed } from 'vue'
import { useLocaleStoreWithOut } from '../stores/modules/locale'
import { i18n } from './'
import type { LocaleType } from './config'
import { loadLocalePool, setHtmlPageLang } from './helper'
function setI18nLanguage(locale: LocaleType) {
const localeStore = useLocaleStoreWithOut()
if (i18n.mode === 'legacy') i18n.global.locale = locale
else (i18n.global.locale as any).value = locale
localeStore.setLocale(locale)
setHtmlPageLang(locale)
}
export function useLocale() {
const localeStore = useLocaleStoreWithOut()
const getLocale = computed(() => localeStore.getLocale)
// Switching the language will change the locale of useI18n
// And submit to configuration modification
function changeLocale(locale: LocaleType) {
// const globalI18n = i18n.global;
// const currentLocale = unref(globalI18n.locale);
// console.log(currentLocale, "currentLocale", locale, "locale");
// if (currentLocale === locale)
// return locale
if (loadLocalePool.includes(locale)) {
setI18nLanguage(locale)
return locale
}
loadLocalePool.push(locale)
setI18nLanguage(locale)
return locale
}
return {
getLocale,
changeLocale
}
}