/
translator.ts
68 lines (56 loc) · 2.24 KB
/
translator.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
/// <reference path='../../../node_modules/i18next-client/typescript/i18next.d.ts' />
/// <reference path='../../../third_party/typings/index.d.ts' />
import * as i18next from 'i18next-client';
import * as xregexp from 'xregexp';
// Example usage of these tests:
// isRightToLeft('hi') -> false
// isRightToLeft('لك الوص') -> true
function isRightToLeft(lang :string): boolean {
return xregexp.XRegExp('[\\p{Arabic}\\p{Hebrew}]').test(lang);
}
declare const window :I18nWindow;
declare const require :(path: string) => MessageResource;
interface I18nWindow extends Window {
i18nResources: any;
}
interface MessageResource { [key: string]: Message }
interface Message {
description: string;
message: string;
}
var english_source = require('../locales/en/messages.json');
var arabic_source = require('../locales/ar/messages.json');
var farsi_source = require('../locales/fa/messages.json');
var chinese_source = require('../locales/zh/messages.json');
function createI18nDictionary(sourceFile :MessageResource): IResourceStoreKey {
let i18nDictionary :IResourceStoreKey = {};
for (let key in sourceFile) {
i18nDictionary[key] = sourceFile[key]['message'];
}
return i18nDictionary;
}
window.i18nResources = {};
i18next.init({
resStore: window.i18nResources,
fallbackLng: 'en'
});
i18next.addResources('en', 'translation', createI18nDictionary(english_source));
i18next.addResources('zh', 'translation', createI18nDictionary(chinese_source));
i18next.addResources('ar', 'translation', createI18nDictionary(arabic_source));
i18next.addResources('fa', 'translation', createI18nDictionary(farsi_source));
export const i18n_t = (placeholder :string, params ?:any): string => {
for (let p in params) {
if (isRightToLeft(params[p])) {
params[p] = '\u200F' + params[p] + '\u200F';
} else {
params[p] = '\u200E' + params[p] + '\u200E';
}
}
return i18next.t(placeholder, params);
};
export const i18n_setLng = i18next.setLng;
export const i18nSanitizeHtml = (i18nMessage: string) => {
// Remove all HTML other than supported tags like strong, a, p, etc.
return i18nMessage.replace(/<((?!(\/?(strong|a|p|br|uproxy-faq-link)))[^>]+)>/g, '');
}
export const i18n_languagesAvailable :string[] = Object.keys(window.i18nResources);