/
index.ts
82 lines (73 loc) · 1.98 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { ref, reactive, inject, computed } from 'vue';
import { isString } from '../_utils/is';
import { configProviderInjectionKey } from '../config-provider/context';
import type { ArcoI18nMessages, ArcoLang } from './interface';
import zhCN from './lang/zh-cn';
const LOCALE = ref('zh-CN');
const I18N_MESSAGES = reactive<ArcoI18nMessages>({
'zh-CN': zhCN,
});
/**
* 添加地区语言包。添加过后的语言包可以通过 `useLocale` 使用
* @param messages 需要添加的地区语言数据
* @param options
*/
export const addI18nMessages = (
messages: ArcoI18nMessages,
options?: {
overwrite?: boolean;
}
) => {
for (const key of Object.keys(messages)) {
if (!I18N_MESSAGES[key] || options?.overwrite) {
I18N_MESSAGES[key] = messages[key];
}
}
};
/**
* 切换地区语言。仅在未提供ConfigProvider时生效。
* @param locale
*/
export const useLocale = (locale: string) => {
if (!I18N_MESSAGES[locale]) {
// eslint-disable-next-line no-console
console.warn(`use ${locale} failed! Please add ${locale} first`);
return;
}
LOCALE.value = locale;
};
/**
* 获取当前的地区语言
*/
export const getLocale = () => {
return LOCALE.value;
};
// 仅内部使用
export const useI18n = () => {
const configProvider = inject(configProviderInjectionKey, undefined);
const i18nMessage = computed<ArcoLang>(
() => configProvider?.locale ?? I18N_MESSAGES[LOCALE.value]
);
const locale = computed(() => i18nMessage.value.locale);
const transform = (key: string, ...args: any[]): string => {
const keyArray = key.split('.');
let temp: any = i18nMessage.value;
for (const keyItem of keyArray) {
if (!temp[keyItem]) {
return key;
}
temp = temp[keyItem];
}
if (isString(temp)) {
if (args.length > 0) {
return temp.replace(/{(\d+)}/g, (sub, index) => args[index] ?? sub);
}
return temp;
}
return temp;
};
return {
locale,
t: transform,
};
};