-
Notifications
You must be signed in to change notification settings - Fork 16
/
i18n.js
95 lines (87 loc) · 2.48 KB
/
i18n.js
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
// Process the i18n initialization.
//
// If 'hl=' is set in URL, use that value. If not, use a trick to get user agent's preference.
//
// Callback is provided to update the i18n message after we know the language setting of the
// user agent.
//
function load_i18n(callback) {
let lang = PARAMS.get("hl");
let i18n = $.i18n();
const supportedLanguages = ["en-US", "zh-TW", "ko", "he"];
do {
if (supportedLanguages.indexOf(lang) !== -1) break;
if (supportedLanguages.indexOf(navigator.language) !== -1) {
console.log('using navigator.language');
lang = navigator.language;
break;
}
for (let l of navigator.languages) {
console.log(`checking ${l}`);
if (supportedLanguages.indexOf(l) !== -1) {
console.log('using navigator.languages');
lang = l;
break;
}
}
if (lang) break;
console.log('fallback to en-US');
lang = "en-US";
} while (0);
console.log(`selected language: ${lang}`);
HTML_LANG = lang;
i18n.locale = lang;
i18n.load( `locales/${lang}/text.json`, i18n.locale ).done(
() => {
callback();
}
);
}
// Called after i18n .json file is loaded.
//
// This function iterates all elements starting with "HTML_" prefix. Then replace the
// innerHTML with i18n string (by its ID).
//
// It also does similar things for IMG.
//
function update_i18n_UI() {
$("[id^=HTML_]").each(function(idx) {
$(this).html($.i18n($(this)[0].id));
});
$("[id^=IMG_]").each(function(idx) {
$(this).attr("src", $.i18n($(this)[0].id));
});
$("template").each(function(idx) {
$(this.content).find('[id^=HTML_]').each(function(_) {
$(this).html($.i18n($(this)[0].id));
})
});
window.document.title = $.i18n("HTML_APP_NAME");
}
// Handle RTL UI.
//
// This must be run after the Materialize init
//
function apply_RTL_UI() {
// Handle Right-to-Left languages
let rtl = ['he', 'iw'];
if (HTML_LANG && rtl.includes(HTML_LANG)) {
// RTL
$('html').children().css('direction', 'rtl');
$('#nav-mobile').addClass('left');
$('.btn_next_step').addClass('left');
$('.select-dropdown').css('text-align', 'left');
// Reverse the order of tabs.
const tabs = $(".tabs")[0];
const children = [...tabs.children].reverse();
tabs.innerText = "";
for (let c of children) {
tabs.appendChild(c);
}
} else {
// LTR
$('html').children().css('direction', 'ltr');
$('#nav-mobile').addClass('right');
$('.btn_next_step').addClass('right');
}
}