-
Notifications
You must be signed in to change notification settings - Fork 32
/
i18n-template.js
46 lines (38 loc) · 1.27 KB
/
i18n-template.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
if ('undefined' === typeof hatch) var hatch = {};
hatch.__i18n = {};
hatch.__locale = 'en';
/**
* Translate term
*
* @param {String} path - term to translate, e.g. "common.error.NotFound".
* @param {Array} substitute - strings to insert into result instead of "%".
* @param {String} defaultValue - this value will be used when translation missing.
*/
function t(path, substitute, defaultValue) {
var translation = hatch.__i18n[hatch.__locale];
if (arguments.length === 2) {
if ('object' === typeof substitute || !path.match(/%/)) {
defaultValue = substitute;
substitute = null;
}
}
function nextPathItem(token) {
return (translation = translation[token]);
}
if (!translation || !path.split('.').every(nextPathItem)) {
translation = defaultValue || 'translation missing for ' + hatch.__locale + '.' + path;
}
if (translation && substitute && substitute.length) {
substitute.forEach(function(substitution) {
translation = translation.replace(/%/, substitution.toString().replace(/%/g, ''));
});
}
return translation;
}
$(function() {
var locale = $('html').attr('lang');
if (locale) {
hatch.__locale = locale;
}
});
hatch.__i18n = {{ i18n }};