1 |
|
2 |
|
3 |
|
4 | export const defaultTranslateConfig = {
|
5 | loader: () => Promise.resolve({}),
|
6 | emptyPlaceholder: key => `[${key}]`,
|
7 | getTranslation: getTranslation,
|
8 | interpolate: interpolate,
|
9 | translationCache: {},
|
10 | languageCache: {},
|
11 | lang: null,
|
12 | translations: null
|
13 | };
|
14 |
|
15 | let currentConfig = defaultTranslateConfig;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export function registerTranslateConfig(config) {
|
21 | return (currentConfig = Object.assign({}, currentConfig, config));
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | export async function loadTranslations(lang, config = currentConfig) {
|
29 | return config.languageCache[lang] != null ? config.languageCache[lang] : await currentConfig.loader(lang, config);
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | export function dispatchLangChanged(detail) {
|
36 | window.dispatchEvent(new CustomEvent("langChanged" , { detail }));
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | export function updateConfig(config, newLang, newTranslations) {
|
45 | dispatchLangChanged({
|
46 | previousTranslations: config.translations,
|
47 | previousLang: config.lang,
|
48 | lang: (config.lang = newLang),
|
49 | translations: (config.translations = newTranslations)
|
50 | });
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export function listenForLangChanged(callback, options) {
|
59 | const handler = (e) => callback(e.detail);
|
60 | window.addEventListener("langChanged" , handler, options);
|
61 | return () => window.removeEventListener("langChanged" , handler);
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | export async function use(lang, config = currentConfig) {
|
69 |
|
70 | const translations = await loadTranslations(lang);
|
71 | config.languageCache[lang] = translations;
|
72 | config.translationCache = {};
|
73 |
|
74 | updateConfig(config, lang, translations);
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | export function interpolate(text, values) {
|
82 | return Object.entries(extract(values)).reduce((text, [key, value]) => text.replace(new RegExp(`{{[ ]*${key}[ ]*}}`), String(extract(value))), text);
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | export function getTranslation(key, config = currentConfig) {
|
90 |
|
91 | const parts = key.split(".");
|
92 |
|
93 | let translation = config.translations || {};
|
94 | while (parts.length > 0) {
|
95 | translation = translation[parts.shift()];
|
96 |
|
97 | if (translation == null)
|
98 | return config.emptyPlaceholder(key, config);
|
99 | }
|
100 |
|
101 | return translation.toString();
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | export function get(key, values, config = currentConfig) {
|
111 |
|
112 | let translation = config.translationCache[key]
|
113 | || (config.translationCache[key] = config.getTranslation(key, config));
|
114 |
|
115 | values = values != null ? extract(values) : null;
|
116 |
|
117 | return values != null ? currentConfig.interpolate(translation, values, config) : translation;
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | export function extract(obj) {
|
124 | return (typeof obj === "function") ? obj() : obj;
|
125 | }
|
126 |
|
\ | No newline at end of file |