-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
jquery-i18next.js
128 lines (100 loc) · 3.66 KB
/
jquery-i18next.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.jqueryI18next = factory());
}(this, (function () { 'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var defaults = {
tName: 't',
i18nName: 'i18n',
handleName: 'localize',
selectorAttr: 'data-i18n',
targetAttr: 'i18n-target',
optionsAttr: 'i18n-options',
useOptionsAttr: false,
parseDefaultValueFromContent: true
};
function init(i18next, $) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
options = _extends({}, defaults, options);
function parse(ele, key, opts) {
if (key.length === 0) return;
var attr = 'text';
if (key.indexOf('[') === 0) {
var parts = key.split(']');
key = parts[1];
attr = parts[0].substr(1, parts[0].length - 1);
}
if (key.indexOf(';') === key.length - 1) {
key = key.substr(0, key.length - 2);
}
function extendDefault(o, val) {
if (!options.parseDefaultValueFromContent) return o;
return _extends({}, o, { defaultValue: val });
}
if (attr === 'html') {
ele.html(i18next.t(key, extendDefault(opts, ele.html())));
} else if (attr === 'text') {
ele.text(i18next.t(key, extendDefault(opts, ele.text())));
} else if (attr === 'prepend') {
ele.prepend(i18next.t(key, extendDefault(opts, ele.html())));
} else if (attr === 'append') {
ele.append(i18next.t(key, extendDefault(opts, ele.html())));
} else if (attr.indexOf('data-') === 0) {
var dataAttr = attr.substr('data-'.length);
var translated = i18next.t(key, extendDefault(opts, ele.data(dataAttr)));
// we change into the data cache
ele.data(dataAttr, translated);
// we change into the dom
ele.attr(attr, translated);
} else {
ele.attr(attr, i18next.t(key, extendDefault(opts, ele.attr(attr))));
}
}
function localize(ele, opts) {
var key = ele.attr(options.selectorAttr);
if (!key && typeof key !== 'undefined' && key !== false) key = ele.text() || ele.val();
if (!key) return;
var target = ele,
targetSelector = ele.data(options.targetAttr);
if (targetSelector) target = ele.find(targetSelector) || ele;
if (!opts && options.useOptionsAttr === true) opts = ele.data(options.optionsAttr);
opts = opts || {};
if (key.indexOf(';') >= 0) {
var keys = key.split(';');
$.each(keys, function (m, k) {
// .trim(): Trim the comma-separated parameters on the data-i18n attribute.
if (k !== '') parse(target, k.trim(), opts);
});
} else {
parse(target, key, opts);
}
if (options.useOptionsAttr === true) {
var clone = {};
clone = _extends({ clone: clone }, opts);
delete clone.lng;
ele.data(options.optionsAttr, clone);
}
}
function handle(opts) {
return this.each(function () {
// localize element itself
localize($(this), opts);
// localize children
var elements = $(this).find('[' + options.selectorAttr + ']');
elements.each(function () {
localize($(this), opts);
});
});
};
// $.t $.i18n shortcut
$[options.tName] = i18next.t.bind(i18next);
$[options.i18nName] = i18next;
// selector function $(mySelector).localize(opts);
$.fn[options.handleName] = handle;
}
var index = {
init: init
};
return index;
})));