-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathindex.ts
92 lines (80 loc) · 2.81 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
83
84
85
86
87
88
89
90
91
92
import defaultDictionary from './locales/en/messages.json';
import { I18nDictionary, Dictionary } from '../../../types/configs';
import { LeavesDictKeys } from '../../types-internal/i18n-internal-namespace';
/**
* Type for all available internal dictionary strings
*/
type DictKeys = LeavesDictKeys<typeof defaultDictionary>;
/**
* This class will responsible for the translation through the language dictionary
*/
export default class I18n {
/**
* Property that stores messages dictionary
*/
private static currentDictionary: I18nDictionary = defaultDictionary;
/**
* Type-safe translation for internal UI texts:
* Perform translation of the string by namespace and a key
*
* @example I18n.ui(I18nInternalNS.ui.blockTunes.toggler, 'Click to tune')
*
* @param internalNamespace - path to translated string in dictionary
* @param dictKey - dictionary key. Better to use default locale original text
*/
public static ui(internalNamespace: string, dictKey: DictKeys): string {
return I18n._t(internalNamespace, dictKey);
}
/**
* Translate for external strings that is not presented in default dictionary.
* For example, for user-specified tool names
*
* @param namespace - path to translated string in dictionary
* @param dictKey - dictionary key. Better to use default locale original text
*/
public static t(namespace: string, dictKey: string): string {
return I18n._t(namespace, dictKey);
}
/**
* Adjust module for using external dictionary
*
* @param dictionary - new messages list to override default
*/
public static setDictionary(dictionary: I18nDictionary): void {
I18n.currentDictionary = dictionary;
}
/**
* Perform translation both for internal and external namespaces
* If there is no translation found, returns passed key as a translated message
*
* @param namespace - path to translated string in dictionary
* @param dictKey - dictionary key. Better to use default locale original text
*/
private static _t(namespace: string, dictKey: string): string {
const section = I18n.getNamespace(namespace);
/**
* For Console Message to Check Section is defined or not
* if (section === undefined) {
* _.logLabeled('I18n: section %o was not found in current dictionary', 'log', namespace);
* }
*/
if (!section || !section[dictKey]) {
return dictKey;
}
return section[dictKey] as string;
}
/**
* Find messages section by namespace path
*
* @param namespace - path to section
*/
private static getNamespace(namespace: string): Dictionary {
const parts = namespace.split('.');
return parts.reduce((section, part) => {
if (!section || !Object.keys(section).length) {
return {};
}
return section[part];
}, I18n.currentDictionary);
}
}