-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.ts
92 lines (79 loc) · 2.2 KB
/
i18n.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
/**
* i18n library for Dota Coach
*
* Format to be used: <div id="i18n_#ENGLISH TEXT#" class="i18n">${i18n.t(#ENGLISH TEXT)}</div>`
*/
import { i18n } from './i18n-data'
import * as DotaLogger from '../../src/utility/log'
declare namespace window {
let language: string
}
window.language = 'en'
/**
* Function returns the text in the right language
*
* @param code
* @returns
*/
export function t(code: string) {
if (!i18n.text.hasOwnProperty(code)) {
DotaLogger.log(`localization.t(): Invalid code ${code}`)
var err = new Error();
console.warn(err.stack);
code = "ERROR"
}
if (i18n.text[code].hasOwnProperty(window.language)) {
return i18n.text[code][window.language]
}
else if (window.language=='en') {
return code
}
else {
return code
}
}
/**
* This function initiates the library.
* It needs to be called before the library can be used.
*
* @param language Supported languages: 'en', 'ru', 'zh', 'id'
*/
export function setLanguage(language: string) {
const lang = findLanguage(language)
window.language = lang
}
export function findLanguage(language: string): string {
const ls = i18n.config.languages
for (const l of ls) {
if (l.code==language) {
return language
}
}
return "en" // Default language
}
export function getLanguage(): string {
return window.language
}
export function getLanguageName(): string {
DotaLogger.log(`localization.getLanguageName(): window.language = ${window.language}`)
return i18n.config.languages[window.language]
}
/**
*
* @returns Array of { <language code>: <language name> }
*/
export function getLanguages(): any[] {
return i18n.config.languages
}
/**
* Function update all static HTML elements declared as 'i18n' (class needs to be set to 'i18n' and html id equlas to i18n_<JSON code>)
*/
export function updateHTML() {
const elements = document.getElementsByClassName('i18n')
for (const element of elements) {
element.innerHTML = t(element.id.replace('i18n_', '').replace('i18x_', ''))
}
}
export function div(code: string) {
return `<div id="i18n_${code}" class="i18n">${t(code)}</div>`
}