github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/hooks/web/useLocale.ts (about)

     1  import { i18n } from '@/plugins/vueI18n'
     2  import { useLocaleStoreWithOut } from '@/store/modules/locale'
     3  import { setHtmlPageLang } from '@/plugins/vueI18n/helper'
     4  
     5  const setI18nLanguage = (locale: LocaleType) => {
     6    const localeStore = useLocaleStoreWithOut()
     7  
     8    if (i18n.mode === 'legacy') {
     9      i18n.global.locale = locale
    10    } else {
    11      ;(i18n.global.locale as any).value = locale
    12    }
    13    localeStore.setCurrentLocale({
    14      lang: locale
    15    })
    16    setHtmlPageLang(locale)
    17  }
    18  
    19  export const useLocale = () => {
    20    // Switching the language will change the locale of useI18n
    21    // And submit to configuration modification
    22    const changeLocale = async (locale: LocaleType) => {
    23      const globalI18n = i18n.global
    24  
    25      const langModule = await import(`../../locales/${locale}.ts`)
    26  
    27      globalI18n.setLocaleMessage(locale, langModule.default)
    28  
    29      setI18nLanguage(locale)
    30    }
    31  
    32    return {
    33      changeLocale
    34    }
    35  }