-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
76 lines (67 loc) · 2.33 KB
/
scripts.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
+function () {
'use strict';
$(init);
function init() {
new Internationalizer().init();
/* Experimetal
new LayoutLoader().ajaxifyNavLinks();
*/
}
function Internationalizer() {
this.init = init;
////////////////////////////////
var translate = null;
function init() {
var options = {
lng: 'ru',
fallbackLng: ["en"],
backend: {
loadPath: "/locales/{{lng}}/{{ns}}.json"
},
ns: [
"common",
"welcome"
],
defaultNS: "common"
};
i18next
.use(i18nextXHRBackend)
.init(options, onI18nInitialized);
$('.lang-list .lang-list-item').click(function () {
var key = $(this).data('key'),
name = $(this).text(),
flagClass = $(this).find('.lang-list-item_flag').attr('class').replace('lang-list-item_flag', '');
$('.selected-lang .selected-lang_name').text(name);
$('.selected-lang .selected-lang_flag')
.removeClass()
.addClass('selected-lang_flag' + flagClass);
i18next.changeLanguage(key, onI18nInitialized);
$('.lang-list li.active').removeClass('active');
$(this).parent().addClass('active');
});
}
function onI18nInitialized(err, t) {
if (err) {
return console.error('Failed to initialize i18next.', err);
}
translate = t;
$('[data-i18n]').each(translateElement);
}
function translateElement() {
var $e = $(this),
expr = $e.data('i18n'),
ns = expr.indexOf(':') === -1
? null
: expr.split(':')[0],
key = expr.indexOf(':') === -1
? expr
: expr.split(':')[1].replace(/\s*/g, ''),
value = ns === null
? translate(key)
: translate(key, { ns: ns });
if (key !== value) { // translation successful
$e.text(value);
}
}
}
} ();