-
Notifications
You must be signed in to change notification settings - Fork 0
/
I18n.js
194 lines (173 loc) · 5.69 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* global define */
;(function(){
'use strict';
var regex = /\$\{\s*([a-zA-Z0-9\-\_]+)\s*\}/g;
var globals;
var activeLocale;
var translations;
var defaultLocale;
var defaultTranslations;
var markMissing;
var preProcessor;
var postProcessor;
var suffixFunction;
var keepPlaceholder;
var findTranslation;
var suffixSeparator;
var nestingSeparator;
var I18n = {
't': translate,
'add': addTranslation,
'init': init,
'has': has,
set 'locale'(locale) { setLocale(locale); },
get 'locale'() { return activeLocale; },
get 'globals'() { return globals; }
};
var hasDefine = typeof define === 'function';
var hasExports = typeof module !== 'undefined' && module['exports'];
var root = (typeof window === 'undefined') ? global : /* istanbul ignore next */ window;
/* istanbul ignore next */
if (hasDefine) { // AMD Module
define([], function() {
return I18n;
});
} else if (hasExports) { // Node.js Module
module['exports'] = I18n;
} else { // Assign to the global object
root['I18n'] = I18n;
}
function defaultPreProcessorFn(key) {
return key;
}
function defaultPostProcessorFn(str) {
return str.replace(/\n/g, '<br />');
};
function defaultSuffixFn(count) {
return suffixSeparator +(count === 1 ? 'one' : 'other');
};
function defaultFindTranslation(key, translations) {
if (translations[key]) {
return translations[key];
}
var combined = key.split(nestingSeparator);
var values = translations;
while(combined.length && values) {
var subkey = combined.shift();
values = values[subkey];
}
return values;
}
function translate(key, args, locale) {
args = args || {};
var currentLocale = locale || activeLocale;
if (!currentLocale) {
throw new Error('Active locale is not set');
}
var processedKey = preProcessor(key, args, locale);
var t = getTranslation(processedKey, args, false, currentLocale);
if (t) {
var str = interpolate(t, prepareArgs(args, currentLocale));
if (postProcessor) {
str = postProcessor(str, processedKey, args);
}
return str;
} else if (markMissing) {
return currentLocale + ': ' + processedKey;
} else {
return processedKey;
}
}
function has(key, args, includeDefault, locale) {
args = args || {};
var currentLocale = locale || activeLocale;
if (!currentLocale) {
throw new Error('Active locale is not set');
}
var processedKey = preProcessor(key, args, locale);
return typeof getTranslation(processedKey, args, !includeDefault, currentLocale) !== 'undefined';
}
function getSuffix(key, args, locale) {
if ('count' in args && suffixFunction) {
return suffixFunction(args.count, key, args, locale);
}
return '';
}
function getTranslation(key, args, ignoreDefault, currentLocale) {
var currentTranslations = translations[currentLocale] || {};
var current = findTranslation(key + getSuffix(key, args, currentLocale), currentTranslations)
|| findTranslation(key, currentTranslations);
if (!current && defaultTranslations && !ignoreDefault) {
current = findTranslation(key + getSuffix(key, args, defaultLocale), defaultTranslations)
|| findTranslation(key, defaultTranslations);
}
return current;
}
function interpolate(t, args) {
var match = regex.exec(t);
var lastIndex = 0;
while(match) {
var arg = args[match[1]];
if (arg === undefined) {
arg = keepPlaceholder ? match[0] : '';
lastIndex = keepPlaceholder ? regex.lastIndex : lastIndex;
}
t = t.replace(match[0], arg);
regex.lastIndex = lastIndex; // Bug #1 - Need to reset the position in case the placeholder is longer than the value
match = regex.exec(t);
}
return t;
}
function prepareArgs(args, currentLocale) {
var prepared = {};
extend(prepared, globals.all);
extend(prepared, globals[currentLocale]);
extend(prepared, args);
return prepared;
}
function extend(original, additional) {
additional = additional || {};
for (var key in additional) {
/* istanbul ignore else */
if (additional.hasOwnProperty(key)) {
original[key] = additional[key];
}
}
return original;
}
function setLocale(locale) {
activeLocale = locale;
}
function addTranslation() {
var translationObj, locale;
if (typeof arguments[0] === 'object') {
translationObj = arguments[0];
locale = arguments[1];
} else {
translationObj = {};
translationObj[arguments[0]] = arguments[1];
locale = arguments[2];
}
locale = locale || activeLocale;
translations[locale] = translations[locale] || {};
extend(translations[locale], translationObj);
}
function setOption(option, defaultOption) {
return option === false ? null : (option || defaultOption);
}
function init(options) {
keepPlaceholder = !!options['keepPlaceholder'];
globals = setOption(options['globals'], {});
markMissing = setOption(options['markMissing'], true);
translations = setOption(options['translations'], {});
preProcessor = setOption(options['preProcessor'], defaultPreProcessorFn);
postProcessor = setOption(options['postProcessor'], defaultPostProcessorFn);
suffixFunction = setOption(options['suffixFunction'], defaultSuffixFn);
findTranslation = setOption(options['findTranslation'], defaultFindTranslation);
defaultLocale = options['default'] || null;
suffixSeparator = options['suffixSeparator'] || '_';
nestingSeparator = options['nestingSeparator'] || '.';
defaultTranslations = translations[options['default']];
setLocale(options['active']);
}
})();