12345678910111213141516171819202122232425262728293031323334353637383940 |
- import { createI18n } from 'vue-i18n';
- import zhCN from './zh-CN/index.json';
- import enUS from './en-US/index.json';
- const messages = {
- 'zh-CN': zhCN,
- 'en-US': enUS,
- };
- const defaultLocale = localStorage.getItem('locale') || 'zh-CN';
- const i18n = createI18n({
- legacy: false,
- locale: defaultLocale,
- fallbackLocale: 'zh-CN',
- messages,
- globalInjection: true,
- });
- // 初始化时设置 data-locale 属性
- if (typeof document !== 'undefined') {
- document.querySelector('html')?.setAttribute('lang', defaultLocale);
- document.querySelector('html')?.setAttribute('data-locale', defaultLocale);
- }
- export default i18n;
- // 导出语言切换函数
- export function setLanguage(locale) {
- i18n.global.locale.value = locale;
- localStorage.setItem('locale', locale);
- document.querySelector('html').setAttribute('lang', locale);
- // 设置 data-locale 属性用于样式适配
- document.querySelector('html').setAttribute('data-locale', locale);
- }
- // 导出当前语言
- export function getCurrentLanguage() {
- return i18n.global.locale.value;
- }
|