mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-05-05 12:44:52 +08:00
feat: add lang type & add returnUserLangText func
This commit is contained in:
@@ -7,8 +7,13 @@ const messages = {
|
||||
'zh-CN': zhCN,
|
||||
};
|
||||
|
||||
type Languages = keyof typeof messages;
|
||||
|
||||
export const useMyI18n = createSharedComposable(() => {
|
||||
const lang = useLocalStorage('lang', navigator.language);
|
||||
const lang = useLocalStorage<Languages>(
|
||||
'lang',
|
||||
navigator.language as Languages
|
||||
);
|
||||
|
||||
const i18n = createI18n({
|
||||
legacy: false,
|
||||
@@ -17,21 +22,30 @@ export const useMyI18n = createSharedComposable(() => {
|
||||
messages,
|
||||
});
|
||||
|
||||
watch(lang, (val) => {
|
||||
i18n.global.locale.value = val;
|
||||
});
|
||||
|
||||
function changeLocale() {
|
||||
if (lang.value === 'zh-CN') {
|
||||
i18n.global.locale.value = 'en';
|
||||
lang.value = 'en';
|
||||
} else {
|
||||
i18n.global.locale.value = 'zh-CN';
|
||||
lang.value = 'zh-CN';
|
||||
}
|
||||
}
|
||||
|
||||
function returnUserLangText(texts: {
|
||||
[k in Languages]: string;
|
||||
}) {
|
||||
return computed(() => texts[lang.value]);
|
||||
}
|
||||
|
||||
return {
|
||||
lang,
|
||||
i18n,
|
||||
t: i18n.global.t,
|
||||
locale: i18n.global.locale,
|
||||
changeLocale,
|
||||
getText: returnUserLangText,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user