Skip to content

Commit

Permalink
feat: reinit i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
aisen60 committed Jan 19, 2024
1 parent 04e579f commit 4aa85e7
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 22 deletions.
11 changes: 6 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
"README.md": "README.md,README.zh-CN.md"
},

"i18n-ally.localesPaths": "src/locales",
"i18n-ally.localesPaths": ["src/locales/lang"],
"i18n-ally.enabledFrameworks": ["vue"],
"i18n-ally.sourceLanguage": "zh",
"i18n-ally.displayLanguage": "zh",
"i18n-ally.enabledParsers": ["ts", "json", "js"],
"i18n-ally.sourceLanguage": "zh-CN",
"i18n-ally.displayLanguage": "zh-CN",
"i18n-ally.enabledParsers": ["ts", "json"],
"i18n-ally.extract.keyMaxLength": 100,
"i18n-ally.namespace": true,
"i18n-ally.keystyle": "nested"
"i18n-ally.keystyle": "nested",
"i18n-ally.extract.keygenStyle": "camelCase"
}
6 changes: 5 additions & 1 deletion src/enums/localeEnum.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export enum localeEnum {
APP_LOCALE = 'APP_LOCALE'
APP_LOCALE = 'APP_LOCALE',
'ZH_CN' = 'zh-CN',
'EN_US' = 'en-US',
'ZH_CN_TEXT' = '中文',
'EN_US_TEXT' = 'English'
}
3 changes: 0 additions & 3 deletions src/locales/en.json

This file was deleted.

14 changes: 7 additions & 7 deletions src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createI18n } from 'vue-i18n';
import { localeEnum } from '@/enums';
import zh from './zh.json';
import en from './en.json';
import zhCN from './lang/zh-CN';
import enUS from './lang/en-US';

// 获取浏览器界面语言,默认语言
// https://developer.mozilla.org/zh-CN/docs/Web/API/Navigator/language
let currentLanguage = navigator.language.replace(/-(\S*)/, '');
let currentLanguage = navigator.language;

// 如果本地缓存记录了语言环境,则使用本地缓存
const lsLocale = localStorage.getItem(localeEnum.APP_LOCALE) || '';
Expand All @@ -14,16 +14,16 @@ if (lsLocale) {
}

export const langs = [
{ key: 'zh', title: '中文' },
{ key: 'en', title: 'English' }
{ key: localeEnum.ZH_CN, label: localeEnum.ZH_CN_TEXT },
{ key: localeEnum.EN_US, label: localeEnum.EN_US_TEXT }
];

export default createI18n({
locale: currentLanguage,
legacy: false, // 修复组件引入i18n时vite脚手架报错的问题
globalInjection: true, // 全局注册 $t
messages: {
zh,
en
'zh-CN': zhCN,
'en-US': enUS
}
});
3 changes: 3 additions & 0 deletions src/locales/lang/en-US.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
hello: 'Hello Vant Theme'
};
3 changes: 3 additions & 0 deletions src/locales/lang/zh-CN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
hello: '你好 Vant Theme'
};
3 changes: 0 additions & 3 deletions src/locales/zh.json

This file was deleted.

4 changes: 2 additions & 2 deletions src/stores/modules/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import type { NLocale, NDateLocale } from 'naive-ui';
export const useLocaleStore = defineStore('localeStore', () => {
const locale = ref(useStorage(localeEnum.APP_LOCALE, i18n.global.locale.value));

const naiveLocale = computed<NLocale | null>(() => (locale.value === 'zh' ? zhCN : null));
const naiveLocale = computed<NLocale | null>(() => (locale.value === 'zh-CN' ? zhCN : null));

const naiveDateLocale = computed<NDateLocale | null>(() =>
locale.value === 'zh' ? dateZhCN : null
locale.value === 'zh-CN' ? dateZhCN : null
);

const setLocale = (lang: Locale) => {
Expand Down
2 changes: 1 addition & 1 deletion src/types/locale.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type Locale = 'zh' | 'en';
export type Locale = 'zh-CN' | 'en-US';

0 comments on commit 4aa85e7

Please sign in to comment.