useLocale.ts 899 B

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