From 9c107f7b1d14edc9e20f1de0a67968a303fcab78 Mon Sep 17 00:00:00 2001 From: Rewrite0 Date: Sun, 15 Oct 2023 13:05:16 +0800 Subject: [PATCH] feat: add lang type & add returnUserLangText func --- webui/src/hooks/useMyI18n.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/webui/src/hooks/useMyI18n.ts b/webui/src/hooks/useMyI18n.ts index 01e30974..f5c1b1aa 100644 --- a/webui/src/hooks/useMyI18n.ts +++ b/webui/src/hooks/useMyI18n.ts @@ -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( + '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, }; });