-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocaleProvider.tsx
325 lines (270 loc) · 10.8 KB
/
LocaleProvider.tsx
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import * as React from 'react';
// import { Router } from 'react-router-dom';
import { History } from 'history';
import { LocaleContext } from './LocaleContext';
import { getLanguageFromUrl } from './localizer';
import { ITranslations } from './types';
interface IProps {
defaultLanguage?: string;
languages: string[];
translations: ITranslations;
localizeUrls?: boolean; // * Manage languageCode in URL upon navigation, default=false
history?: History;
}
interface IState {
language: string;
}
type TranslationVariables = { [field: string]: string | number };
class LocaleProvider extends React.Component<IProps, IState> {
state = {
language: '',
};
componentDidMount() {
this.getIntialLanguage();
}
/**
* * Resolve the initiale language setting from hard setting or from the browser setting
*/
private getIntialLanguage(): string {
const { defaultLanguage, languages, localizeUrls = false } = this.props;
const urlLanguageSetting: string | null = this.getLanguageFromUrl();
const browserLanguageSetting = this.getBrowserLanguage(); // * language without region code
const resolvedLanguage: string =
this.getLanguageFromLocationState() || defaultLanguage || browserLanguageSetting || languages[0];
if (localizeUrls && urlLanguageSetting === null) {
this.insertLanguageToUrl(resolvedLanguage);
}
this.setLanguage(resolvedLanguage);
return resolvedLanguage;
}
private getBrowserLanguage(): string | null {
if (typeof window === 'undefined') {
return null;
}
return window.navigator.language.split(/[-_]/)[0];
}
private getLanguageFromLocationState(): string | null {
const { localizeUrls = false } = this.props;
if (typeof window === 'undefined') {
return null;
}
if (localizeUrls) {
window.localStorage.removeItem('k2-lang');
return this.getLanguageFromUrl();
} else {
return window.localStorage.getItem('k2-lang') || '';
}
}
private setLanguage = (language: string): void => {
const { languages, localizeUrls } = this.props;
const doesLanguageExist = languages.indexOf(language.toLowerCase()) > -1;
if (doesLanguageExist) {
this.setState(
{
language: language.toLowerCase(),
},
() => {
if (localizeUrls) {
this.updateUrl();
} else {
this.updateBrowserStorage();
}
}
);
} else {
console.error('⛔ Selected language does not exist in the initial <LocaleProvider/> configuration!');
}
};
private getLanguageFromUrl = (): string | null => {
const { languages } = this.props;
if (typeof window === 'undefined') {
return null;
}
return getLanguageFromUrl(languages, window.location.pathname);
};
private insertLanguageToUrl(language: string): void {
const { history } = this.props;
const pathName: string = window.location.pathname + window.location.search;
const url: string = `/${language + pathName}`;
history?.push(url);
}
private updateUrl = (): void => {
const { languages, defaultLanguage, history } = this.props;
const { language: currentLanguage } = this.state;
const currentUrl: string = location.pathname + location.search;
let searchString: string = defaultLanguage || languages[0];
// TODO Adapt to languages set via Provider props
for (const language of languages) {
if (currentUrl.includes(language)) {
searchString = language;
break;
}
}
const updatedUrl: string = currentUrl.replace(`/${searchString}/`, `/${currentLanguage}/`);
history?.push(updatedUrl);
};
private updateBrowserStorage(): void {
const { language } = this.state;
window.localStorage.setItem('k2-lang', language);
}
private handleError(
errorCode: 'NO_KEY' | 'NO_VAL' | 'NO_VAR' | 'NO_CONFIG',
language: string,
id: string,
varKey: string = ''
): void {
const errorMap = {
NO_CONFIG: '⛔ Translations not found!',
NO_KEY: `⛔ Missing Translation key: ${id.toUpperCase()}`,
NO_VAL: `⛔ Missing Translation to: ${id.toUpperCase()} => ${language.toUpperCase()}`,
NO_VAR: `⛔ Missing Variable { ${varKey} : string } in: ${id.toUpperCase()} => ${language.toUpperCase()}`,
};
const errorMessage: string = errorMap[errorCode];
// eslint-disable-next-line no-console
console.error(errorMessage);
}
/**
* Check if searchedString and replaced string as the same
* If the text is the same, then return true
* @param searchedString
* @param replacedString
*/
private compareSearchedAndReplacedString(searchedString: string, replacedString: string): boolean {
return searchedString.toLowerCase() === replacedString.toLowerCase();
}
/**
* * Perfroms variables search/replace of vars on the set translation
* @param textToReplaceVars
* @param searchValue
* @param replaceValue
* @param varKey
*/
private replaceVars(
textToReplaceVars: string,
searchValue: string,
replaceValue: string,
id: string,
varKey: string
): string {
const searchRegex = new RegExp(searchValue, 'gi');
const textWithReplacedVars: string = textToReplaceVars.replace(searchRegex, replaceValue);
// * Checks if variable is missing from the translation doc
const isVarReplacementSuccessful: boolean = !this.compareSearchedAndReplacedString(
textToReplaceVars,
textWithReplacedVars
);
if (isVarReplacementSuccessful === false) {
this.handleError('NO_VAR', varKey, id);
}
return textWithReplacedVars;
}
/**
* Process variables provided as props
* @param translatedText
*/
private processVars(id: string, translatedText: string, vars: TranslationVariables = {}): string {
const varKeys: string[] = Object.keys(vars) || [];
let parsedText: string = translatedText;
varKeys.forEach((varKey: string) => {
// * Trim key to allow for consistency when translating
const searchValue: string = `{${varKey.trim()}}`;
// * Needs to be converted into a string becauase input can either be a string/number
const replaceValue: string = vars[varKey].toString().trim();
parsedText = this.replaceVars(parsedText, searchValue, replaceValue, varKey, id);
});
return parsedText;
}
private performTranslation = (id: string | null | undefined | false, vars?: TranslationVariables): string => {
const { language } = this.state;
const { translations } = this.props;
if (!id) {
return `⛔ Missing translation key`;
}
if (!translations) {
this.handleError('NO_CONFIG', language, id);
return `⛔ ${id.toUpperCase()}`;
} else if (!translations[id]) {
this.handleError('NO_KEY', language, id);
return `⛔ ${id.toUpperCase()}`;
} else if (!translations[id][language]) {
this.handleError('NO_KEY', language, id);
return `⛔ ${id.toUpperCase()} - ${language.toUpperCase()}`;
} else {
const translatedText: string = translations[id][language];
if (vars) {
return this.processVars(id, translatedText, vars);
}
return translatedText;
}
};
private performTranslationAndParse = (id: string, vars?: TranslationVariables): React.ReactNode => {
const translatedText: string = this.performTranslation(id, vars);
// const isHtml: boolean = this.checkIfStringIsHtml(translatedText);
// * If parseText is false, the don't parseHTML. Otherwise parseHTML by default
// const parseTranslatedText: boolean = !parseHtml ? false : isHtml;
return <span dangerouslySetInnerHTML={{ __html: translatedText }} />;
};
/**
* * Checks if the translated text is HTML
* https://stackoverflow.com/questions/15458876/check-if-a-string-is-html-or-not/15458987
* @param text
*/
// private checkIfStringIsHtml(text: string): boolean {
// return /<[a-z/][\s\S]*>/i.test(text);
// }
// private async getComponent(): Promise<React.ReactNode> {
// const { children, localizeUrls = false, history } = this.props;
// if (localizeUrls && history) {
// // import { Router } from 'react-router-dom';
// const { Router } = await import('react-router-dom');
// return <Router history={history}>{children}</Router>;
// }
// return children;
// }
// private getLanguage = (): string => {
// return '';
// };
render() {
const { translations, localizeUrls = false, history, children } = this.props;
const { language } = this.state;
// const language = this.getLanguage();
if (localizeUrls && !history) {
throw new Error('Missing history prop from LocaleProvider');
}
//
if (!translations) {
throw new Error('Missing translations prop from LocaleProvider');
}
if (localizeUrls && history && language) {
const { Router } = require('react-router-dom');
return (
<LocaleContext.Provider
value={{
language,
translations,
changeLanguage: this.setLanguage,
performTranslation: this.performTranslation,
performTranslationAndParse: this.performTranslationAndParse,
}}>
<Router history={history}>{children}</Router>
</LocaleContext.Provider>
);
}
if (!localizeUrls) {
return (
<LocaleContext.Provider
value={{
language: language || this.getIntialLanguage(),
translations,
changeLanguage: this.setLanguage,
performTranslation: this.performTranslation,
performTranslationAndParse: this.performTranslationAndParse,
}}>
{children}
</LocaleContext.Provider>
);
}
return null;
}
}
export { LocaleProvider };