-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi18n.js
101 lines (95 loc) · 3.04 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
96
97
98
99
100
101
/*
* I18n.js
* =======
*
* Simple client-side i18n tool
* 1. Store your localized labels in json format: `localized-content.json`
* 2. Write your markup with key references using `data-i18n` attributes.
* 3. Explicitly invoke a traverse key resolver: `i18n.localize()`
* OR
* Change the language, and the contents will be refreshed: `i18n.lang('en')`
*
* This util relies on jQuery to perform some DOM manipulation. I would recommend
* using an updated version (1.12.x or 2.x), although this will probably run with
* any older version since it is only taking advantage of `$.getJSON()` and the
* jQuery selector function `$()`.
*
* © 2016 Diogo Simões - diogosimoes.com
*
*/
(function () {
this.I18n = function (defaultLang) {
var lang = defaultLang || 'en';
this.language = lang;
(function (i18n) {
$.getJSON('localized-content.json', function (json) {
i18n.contents = json;
i18n.contents.prop = function (key) {
var result = this;
var keyArr = key.split('.');
for (var index = 0; index < keyArr.length; index++) {
var prop = keyArr[index];
result = result[prop];
}
return result;
};
i18n.localize();
});
})(this);
};
this.I18n.prototype.hasCachedContents = function () {
return this.contents !== undefined;
};
this.I18n.prototype.lang = function (lang) {
if (typeof lang === 'string') {
this.language = lang;
}
this.localize();
return this.language;
};
this.I18n.prototype.localize = function () {
var contents = this.contents;
if (!this.hasCachedContents()) {
return;
}
var dfs = function (node, keys, results) {
var isLeaf = function (node) {
for (var prop in node) {
if (node.hasOwnProperty(prop)) {
if (typeof node[prop] === 'string') {
return true;
}
}
}
}
for (var prop in node) {
if (node.hasOwnProperty(prop) && typeof node[prop] === 'object') {
var myKey = keys.slice();
myKey.push(prop);
if (isLeaf(node[prop])) {
//results.push(myKey.reduce((prev, current) => prev + '.' + current)); //not supported in older mobile broweser
results.push(myKey.reduce( function (previousValue, currentValue, currentIndex, array) {
return previousValue + '.' + currentValue;
}));
} else {
dfs(node[prop], myKey, results);
}
}
}
return results;
};
var keys = dfs(contents, [], []);
for (var index = 0; index < keys.length; index++) {
var key = keys[index];
if (contents.prop(key).hasOwnProperty(this.language)) {
$('[data-i18n="'+key+'"]').text(contents.prop(key)[this.language]);
$('[data-i18n-placeholder="'+key+'"]').attr('placeholder', contents.prop(key)[this.language]);
$('[data-i18n-value="'+key+'"]').attr('value', contents.prop(key)[this.language]);
} else {
$('[data-i18n="'+key+'"]').text(contents.prop(key)['en']);
$('[data-i18n-placeholder="'+key+'"]').attr('placeholder', contents.prop(key)['en']);
$('[data-i18n-value="'+key+'"]').attr('value', contents.prop(key)['en']);
}
}
};
}).apply(window);