Skip to content

Commit 057436b

Browse files
authored
feat: 添加国际化支持 (#79)
1 parent 44e95d5 commit 057436b

7 files changed

Lines changed: 71 additions & 12 deletions

File tree

packages/vue3/source/mobile/src/App.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88

99
<script setup>
1010
import VErrorBoundary from './VErrorBoundary.vue';
11-
// TODO: 约定一种通用方式
12-
const locale = window.$libraries?.ElementPlus?.locale?.zhCn;
11+
import { getUserLanguage } from './i18n';
1312
13+
const { ConfigProvider, transformKeys = v => v } = window.lcapStandardUI;
14+
15+
const { appConfig } = window.appInfo;
16+
const { i18nInfo } = appConfig;
17+
// 获取当前语言
18+
const lang = getUserLanguage(appConfig, i18nInfo.messages);
19+
// 拿到messages
20+
const locale = i18nInfo.enabled ? transformKeys(i18nInfo.messages[lang]) : undefined;
1421
</script>

packages/vue3/source/mobile/src/common/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export function installDirectives(vm, directives) {
2525
}
2626

2727
export function installLibraries(vm, libraries) {
28-
window.$libraries = {};
28+
if (!window.$libraries) {
29+
window.$libraries = {};
30+
}
2931

3032
Object.keys(libraries).forEach((key) => {
3133
window.$libraries[key] = libraries[key];

packages/vue3/source/mobile/src/global-variables.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { useGlobalStore } from '@/store';
55
export const useGlobalVariables = () => {
66
const globalStore = useGlobalStore();
77

8-
const $frontendVariables = globalStore.frontendVariables;
9-
const $userInfo = globalStore.userInfo;
8+
const { frontendVariables: $frontendVariables, userInfo, i18nInfo, ...rest } = globalStore;
109

1110
onBeforeMount(() => {
1211
const variableSet = window.$localCacheVariableSet;
@@ -34,7 +33,9 @@ export const useGlobalVariables = () => {
3433
});
3534

3635
return {
37-
$frontendVariables,
38-
$userInfo,
36+
frontendVariables: $frontendVariables,
37+
userInfo,
38+
i18nInfo,
39+
...rest,
3940
};
4041
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as VueI18n from 'vue-i18n'
2+
3+
export function createI18nInstance(appConfig) {
4+
let locale = 'zh-CN';
5+
if (appConfig.i18nInfo) {
6+
const { I18nList, messages } = appConfig.i18nInfo;
7+
locale = getUserLanguage(appConfig, messages);
8+
// 重置当前生效语言
9+
appConfig.i18nInfo.locale = locale;
10+
appConfig.i18nInfo.currentLocale = locale;
11+
// 设置当前语言名称
12+
appConfig.i18nInfo.localeName = I18nList?.find((item) => item.id === locale)?.name;
13+
}
14+
const i18nInfo = appConfig.i18nInfo;
15+
16+
const i18n = VueI18n.createI18n({
17+
legacy: false,
18+
locale,
19+
messages: i18nInfo.messages || {},
20+
})
21+
22+
return i18n;
23+
}
24+
25+
export function getUserLanguage(appConfig, messages = {}) {
26+
let locale = localStorage.i18nLocale;
27+
// 如果local里没有就读主应用的默认语言
28+
if (!messages[locale]) {
29+
// 如果当前浏览器的设置也没有,就读取主应用的默认语言
30+
locale = navigator.language || navigator.userLanguage;
31+
32+
if (!messages[locale]) {
33+
// 如果不在列表中,获取语言代码的前两位
34+
const baseLang = locale.substring(0, 2);
35+
const languageList = Object.keys(messages);
36+
// 查找列表中是否有与基础语言代码相同的项
37+
const match = languageList.find((lang) => lang.startsWith(baseLang));
38+
// 如果存在前两位一样的就用这个
39+
if (match) {
40+
locale = match;
41+
} else {
42+
// 如果不存在,就用默认语言
43+
locale = appConfig.i18nInfo.locale || 'zh-CN';
44+
}
45+
}
46+
}
47+
return locale;
48+
}

packages/vue3/source/mobile/src/init.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { installComponents, installDirectives, installLibraries } from '@/common
2323
import { getTitleGuard } from '@/guards';
2424

2525
import App from './App.vue';
26+
import { createI18nInstance } from './i18n';
2627
import { setConfig } from './setConfig';
2728

2829
import './index.css';
@@ -120,6 +121,8 @@ const init = (appConfig, platformConfig, routes, metaData) => {
120121

121122
app.use(pinia);
122123

124+
app.use(createI18nInstance(appConfig));
125+
123126
// rendered 事件
124127
if (typeof window?.rendered === 'function') {
125128
window.rendered();
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { defineStore } from 'pinia';
22
export const useGlobalStore = defineStore('global', {
3-
state: () => ({
4-
userInfo: window.$global.userInfo,
5-
frontendVariables: window.$global.frontendVariables,
6-
}),
3+
state: () => window.$global,
74
});

packages/vue3/source/pc/src/App.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import VErrorBoundary from './VErrorBoundary.vue';
1111
import { getUserLanguage } from './i18n';
1212
13+
const { ConfigProvider, transformKeys = v => v } = window.lcapStandardUI;
14+
1315
const { appConfig } = window.appInfo;
1416
const { i18nInfo } = appConfig;
1517
// 获取当前语言
1618
const lang = getUserLanguage(appConfig, i18nInfo.messages);
1719
// 拿到messages
18-
const transformKeys = window.lcapStandardUI?.transformKeys || ((obj) => obj);
1920
const locale = i18nInfo.enabled ? transformKeys(i18nInfo.messages[lang]) : undefined;
2021
</script>

0 commit comments

Comments
 (0)