@modern-js/plugin-i18n 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/src/.eslintrc.json +0 -3
- package/src/index.ts +0 -99
- package/src/utils/index.ts +0 -20
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/.eslintrc.json
DELETED
package/src/index.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { get } from 'lodash';
|
|
2
|
-
import { getObjKeyMap } from './utils';
|
|
3
|
-
|
|
4
|
-
type Language = string;
|
|
5
|
-
|
|
6
|
-
interface LanguageModel {
|
|
7
|
-
[Key: string]: string | LanguageModel;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type LanguageModelMap = Record<Language, LanguageModel>;
|
|
11
|
-
|
|
12
|
-
type TFunc = (
|
|
13
|
-
key: string,
|
|
14
|
-
vars?: { [key: string]: string },
|
|
15
|
-
fallbackText?: string,
|
|
16
|
-
) => string;
|
|
17
|
-
type TI18n = {
|
|
18
|
-
t: TFunc;
|
|
19
|
-
changeLanguage: (config: ChangeLanguageConfig) => void;
|
|
20
|
-
lang: (lang: Language) => { t: TFunc };
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export interface ChangeLanguageConfig {
|
|
24
|
-
locale?: Language;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
class I18n implements TI18n {
|
|
28
|
-
private language: Language = 'en';
|
|
29
|
-
|
|
30
|
-
private languageMap: LanguageModelMap = {};
|
|
31
|
-
|
|
32
|
-
private format(msg: string, vars: { [key: string]: string }) {
|
|
33
|
-
return msg.replace(/\{([^}]+)\}/gm, (_match, capture: string) =>
|
|
34
|
-
Object.prototype.hasOwnProperty.call(vars, capture)
|
|
35
|
-
? vars[capture]
|
|
36
|
-
: capture,
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private getMessage(
|
|
41
|
-
lang: string,
|
|
42
|
-
key: string,
|
|
43
|
-
vars?: { [key: string]: string },
|
|
44
|
-
fallbackText?: string,
|
|
45
|
-
) {
|
|
46
|
-
// 判断语言当前语料库是否存在,不存在使用 en 作为默认语言
|
|
47
|
-
const languages = Object.keys(this.languageMap);
|
|
48
|
-
const resultLang = languages.find(l => l === lang);
|
|
49
|
-
if (!resultLang && languages.length === 0) {
|
|
50
|
-
return fallbackText || key;
|
|
51
|
-
}
|
|
52
|
-
const model: LanguageModel =
|
|
53
|
-
this.languageMap[resultLang || 'en' || languages[0]];
|
|
54
|
-
if (!model) {
|
|
55
|
-
return fallbackText || key;
|
|
56
|
-
}
|
|
57
|
-
const message = get(model, key);
|
|
58
|
-
const value = message || fallbackText || key;
|
|
59
|
-
if (typeof value === 'string') {
|
|
60
|
-
return this.format(value, vars || {});
|
|
61
|
-
}
|
|
62
|
-
throw new Error('key is not a string');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public init<T extends LanguageModel>(
|
|
66
|
-
language?: Language,
|
|
67
|
-
languageMap?: Record<Language, T>,
|
|
68
|
-
) {
|
|
69
|
-
this.language = language || 'en';
|
|
70
|
-
if (languageMap) {
|
|
71
|
-
this.languageMap = languageMap;
|
|
72
|
-
}
|
|
73
|
-
return getObjKeyMap(this.languageMap[this.language]) as T;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
public changeLanguage(config: ChangeLanguageConfig) {
|
|
77
|
-
this.language = config.locale || 'en';
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public t(
|
|
81
|
-
key: string,
|
|
82
|
-
vars?: { [key: string]: string },
|
|
83
|
-
fallbackText?: string,
|
|
84
|
-
) {
|
|
85
|
-
return this.getMessage(this.language, key, vars, fallbackText);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public lang(lang: string) {
|
|
89
|
-
return {
|
|
90
|
-
t: (
|
|
91
|
-
key: string,
|
|
92
|
-
vars?: { [key: string]: string },
|
|
93
|
-
fallbackText?: string,
|
|
94
|
-
): string => this.getMessage(lang, key, vars, fallbackText),
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export { I18n };
|
package/src/utils/index.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { isObject, isString } from 'lodash';
|
|
2
|
-
|
|
3
|
-
interface ITem {
|
|
4
|
-
[key: string]: string | ITem;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function getObjKeyMap(obj: ITem, prefix = '') {
|
|
8
|
-
const result: ITem = {};
|
|
9
|
-
Object.keys(obj).forEach(key => {
|
|
10
|
-
if (isString(obj[key])) {
|
|
11
|
-
result[key] = prefix ? `${prefix}.${key}` : key;
|
|
12
|
-
} else if (isObject(obj[key])) {
|
|
13
|
-
result[key] = getObjKeyMap(
|
|
14
|
-
obj[key] as ITem,
|
|
15
|
-
prefix ? `${prefix}.${key}` : key,
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
return result;
|
|
20
|
-
}
|