fix: Fix the problem that the `lang` attribute of `HTML` will not be set when it is first loaded (#682)
* refactor: Encapsulate the function of changing the page language * fix(lang): Fix the problem that the `lang` attribute of `HTML` will not be set when it is first loaded
This commit is contained in:
parent
e1123a2ccb
commit
eca8907a11
|
|
@ -3,7 +3,7 @@ import type { I18n, I18nOptions } from 'vue-i18n';
|
||||||
|
|
||||||
import { createI18n } from 'vue-i18n';
|
import { createI18n } from 'vue-i18n';
|
||||||
|
|
||||||
import { setLoadLocalePool } from './useLocale';
|
import { setLoadLocalePool, setHtmlPageLang } from './useLocale';
|
||||||
import { localeSetting } from '/@/settings/localeSetting';
|
import { localeSetting } from '/@/settings/localeSetting';
|
||||||
import { useLocaleStoreWithOut } from '/@/store/modules/locale';
|
import { useLocaleStoreWithOut } from '/@/store/modules/locale';
|
||||||
|
|
||||||
|
|
@ -16,7 +16,8 @@ async function createI18nOptions(): Promise<I18nOptions> {
|
||||||
const locale = localeStore.getLocale;
|
const locale = localeStore.getLocale;
|
||||||
const defaultLocal = await import(`./lang/${locale}.ts`);
|
const defaultLocal = await import(`./lang/${locale}.ts`);
|
||||||
const message = defaultLocal.default?.message ?? {};
|
const message = defaultLocal.default?.message ?? {};
|
||||||
|
|
||||||
|
setHtmlPageLang(locale)
|
||||||
setLoadLocalePool((loadLocalePool) => {
|
setLoadLocalePool((loadLocalePool) => {
|
||||||
loadLocalePool.push(locale);
|
loadLocalePool.push(locale);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,10 @@ interface LangModule {
|
||||||
|
|
||||||
const loadLocalePool: LocaleType[] = [];
|
const loadLocalePool: LocaleType[] = [];
|
||||||
|
|
||||||
|
export function setHtmlPageLang(locale: LocaleType) {
|
||||||
|
document.querySelector('html')?.setAttribute('lang', locale);
|
||||||
|
}
|
||||||
|
|
||||||
export function setLoadLocalePool(cb: (loadLocalePool: LocaleType[]) => void) {
|
export function setLoadLocalePool(cb: (loadLocalePool: LocaleType[]) => void) {
|
||||||
cb(loadLocalePool);
|
cb(loadLocalePool);
|
||||||
}
|
}
|
||||||
|
|
@ -30,7 +34,7 @@ function setI18nLanguage(locale: LocaleType) {
|
||||||
(i18n.global.locale as any).value = locale;
|
(i18n.global.locale as any).value = locale;
|
||||||
}
|
}
|
||||||
localeStore.setLocaleInfo({ locale });
|
localeStore.setLocaleInfo({ locale });
|
||||||
document.querySelector('html')?.setAttribute('lang', locale);
|
setHtmlPageLang(locale)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useLocale() {
|
export function useLocale() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue