UNPKG

3.85 kBJavaScriptView Raw
1import get from 'lodash/get';
2
3import { localeId } from '../';
4import locales from './locales';
5
6export let l = (() => {
7 if (__SERVER__) {
8 if (__DEV__ && typeof global.__KOOT_SSR__ === 'object') {
9 if (typeof global.__KOOT_SSR__.locales === 'object')
10 return global.__KOOT_SSR__.locales[localeId];
11 return {};
12 }
13 // console.log({ locales })
14 if (typeof locales === 'object') return locales[localeId];
15 return {};
16 }
17 if (JSON.parse(process.env.KOOT_I18N_TYPE) === 'store') return locales;
18 return false;
19})();
20
21/**
22 * 翻译文本
23 * 语言包中源文本中的 ${replaceKey} 表示此处需要替换,replaceKey 就是传入的 obj 中对应的值
24 *
25 * @param {string} key 要翻译的文本 Key
26 * @param {*object} obj 文本内对应的替换内容
27 *
28 * @returns {string} 翻译的文本;如果语言包中没有对应的项,返回 key
29 */
30const translate = (...args) => {
31 let key = '';
32 let str;
33 let options = {};
34 const keys = [];
35
36 if (__SERVER__ && __DEV__) l = locales[global.__KOOT_LOCALEID__];
37
38 args.forEach((value, index) => {
39 // 如果最后一个参数是 Object,表示为选项
40 if (
41 index === args.length - 1 &&
42 typeof value === 'object' &&
43 !Array.isArray(value)
44 ) {
45 options = value;
46 return;
47 }
48 if (typeof value === 'string' && value.includes('.')) {
49 value.split('.').forEach(value => keys.push(value));
50 return;
51 }
52 keys.push(value);
53 });
54
55 const length = keys.length;
56
57 if (args.length === 1 && typeof args[0] === 'object') {
58 /**
59 * ! 如果只有一个 arg 且为 Object,直接返回该 Object
60 */
61 return args[0];
62 }
63
64 if (typeof keys[0] === 'object') {
65 /**
66 * 第一个值为 Object,通常是客户端情况,后续值依次取前一个 Object 内对应的值
67 */
68 key = keys[0];
69 let hasUnmatched = false;
70 for (let i = 1; i < length; i++) {
71 // const value = get(key, keys[i]);
72 const value = key[keys[i]];
73 // console.log(key, value);
74 if (typeof value === 'undefined') {
75 hasUnmatched = true;
76 break;
77 } else {
78 key = value;
79 }
80 // if (typeof key[keys[i]] !== 'undefined') key = key[keys[i]];
81 }
82 if (hasUnmatched) key = keys[length - 1];
83 } else if (length === 1) {
84 key = keys[0];
85 } else {
86 for (let i = 0; i < length; i++) {
87 if (typeof l === 'object') {
88 if (keys[i] !== '') key += `[${JSON.stringify(keys[i])}]`;
89 } else {
90 key += (i ? '.' : '') + keys[i];
91 }
92 }
93 }
94
95 // console.log(key);
96 // if (__CLIENT__) {
97 // // console.log(localeId)
98 // console.log(localeId, keys, length, key, l);
99 // }
100
101 if (typeof l === 'undefined') {
102 str = key;
103 } else if (typeof l === 'object') {
104 // str = l && typeof l[key] !== 'undefined' ? l[key] : undefined;
105 str = get(l, key);
106 }
107 // const localeId = _self.curLocaleId
108
109 // if (typeof str === 'undefined' && typeof l === 'object') {
110 // try {
111 // str = get(l, key);
112 // // str = eval('l.' + key);
113 // } catch (e) {}
114 // }
115
116 if (typeof str === 'undefined') str = key;
117
118 if (typeof str === 'string')
119 return str.replace(/\$\{([^}]+)\}/g, (match, p) =>
120 typeof options[p] === 'undefined' ? p : options[p]
121 );
122
123 return str;
124};
125export default translate;