/
i19.js
285 lines (267 loc) · 8.85 KB
/
i19.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* @fileoverview Angular module i19
* @name i19
*
*/
angular.module('i19', ['i19dict']).
/**
* @class $i19 service
* @name i19.$i19
* @requires i19dict
* @returns {function} i19.$i19.translate
*/
factory('$i19', ['$rootScope', 'i19dict', '$http', '$q',
function($scope, i19dict, $http, $q) {
/**
* @private
* @description Current language
*/
var language = 'en';
/**
* @private
* @description Convert an amount into an index into the pluralization list
*/
function count2plural(number) {
return 0 + eval('(function(n){ return ' +
i19dict[language].__pluralization_expr__ +
'})')(number);
}
/**
* @name i19.$i19.translate
* @class
*
* @description
* Translation function
*
* @param {string} input i18n ID of translation string
* @param {number} plural number to use to pluralize this message
* @returns {string} translation for i18n ID in current language
*
* @example $i19('Welcome') == 'Willkommen'
*/
function translate(input, plural) {
var output = i19dict[language][input];
if (!angular.isUndefined(plural))
output = output[count2plural(plural)];
if (!output && translate.warn_on_missing_strings)
console.warn('i19: ' + language + ' missing "' + input + '"');
return output;
}
/**
* @property {bool} i19.$i19.translate.warn_on_missing_strings
*
* @description emit console warning for each missing translation string
*/
translate.warn_on_missing_strings = true;
/**
* @property {bool} i19.$i19.translate.fallback_default
*
* @description return default string in case no translation
* is available; if false, return i18n ID
*/
translate.fallback_default = true;
/**
* @name i19.$i19.translate._extract_plural
* @function
*
* @description
*/
translate._extract_plural = function(i19id) {
var pl_start = i19id.indexOf('(') + 1;
if (pl_start > 0)
return i19id.slice(pl_start, -1);
}
/**
* @name i19.$i19.translate._sanitize
* @function
*
* @description convert default translation string into acceptable i19 ID
*/
translate._sanitize = function(default_value) {
return default_value.replace(/[^A-Za-z0-9\-_]/gi,'');
}
/**
* @name i19.$i19.translate.get_lang
* @function
*
* @description
* Get current language
*
* @returns {string} 2 character locale id
*/
translate.get_lang = function() {
return language;
}
/**
* @name i19.$i19.translate.set_lang
* @function
*
* @description
* Set current language
*
* @param {string} new_lang locale id for new language
* @returns {deferred}
*
* @example $i19.set_lang('de')
*/
translate.set_lang = function (new_lang) {
var def = $q.defer();
function do_set_lang() {
language = new_lang;
$scope.$broadcast('language_changed');
def.resolve(language);
}
// invalid language
if (angular.isUndefined(i19dict[new_lang])) {
console.error("No i19 data for language " + new_lang);
def.reject();
}
// language data available through separate JSON file
else if (angular.isString(i19dict[new_lang])) {
$http.get(i19dict[new_lang]).
success(function(d) {
i19dict[new_lang] = d[new_lang];
do_set_lang();
}).
error(function() {
def.reject();
});
}
// language data already available
else do_set_lang();
return def;
}
// inject i19 into rootScope so we can access it in partials
$scope.i19 = function(a, b) { return translate(a, b) || a; };
return translate;
}]).
/**
* @name i19.i19
* @class i19 directive
*
* @description
* Internationalization of HTML tag *content*
*/
directive('i19', ['$i19', '$compile', function($i19, $compile) {
return {
priority: 99,
compile: function(elem, attrs, transclude) {
var i19id = attrs.i19 || $i19._sanitize(elem.html().trim()),
plural = $i19._extract_plural(attrs.i19 || ''),
deflt = elem.html(),
watcher;
return function($scope, elem) {
function re_compile() {
if (plural) {
watcher && watcher();
watcher = $scope.$watch(plural, function(count) {
elem.html($i19(i19id, count) ||
($i19.fallback_default ? deflt : i19id));
$compile(elem.contents())($scope)
});
} else {
elem.html($i19(i19id) ||
($i19.fallback_default ? deflt : i19id));
$compile(elem.contents())($scope)
}
}
$scope.$on('language_changed', re_compile);
re_compile();
}
}
}
}]).
/**
* @name i19.i19-attr
* @class i19-attr directive
*
* @description Internationalization of HTML *attributes*
*
* @example
* <img alt="Image description" i19-attr="alt">
*
* @example
* <img alt="Image description" i19-attr="alt img-desc-signup">
*/
directive('i19Attr', ['$i19', '$interpolate', function($i19, $interpolate) {
return {
restrict: 'A',
priority: 98,
compile: function(elem, attrs, transclude) {
var attr_map = {};
// build mapping {attribute_name: [i19id, pluralization expr]}
// and clear current attribute values
angular.forEach(attrs.i19Attr.split(','), function(attspec) {
var spec = attspec.trim().split(' '),
aname = spec[0].trim(),
i19id = (spec[1] || '').trim(),
plural = $i19._extract_plural(i19id);
this[aname] = [i19id || $i19._sanitize(attrs[aname]),
plural, attrs[aname]];
elem.attr(aname, '');
}, attr_map);
return function link($scope, elem) {
var watcher = {};
// Translate and interpolate one attribute
function transpolate(id_pl, aname) {
var i19id = id_pl[0],
plural = id_pl[1],
deflt = id_pl[2],
translated = $i19(i19id,
plural && $scope.$eval(plural)) ||
($i19.fallback_default ? deflt : i19id);
transpolated = $interpolate(translated, true);
// re-initialize watchers after every language or
// pluralization change to deal with changing variable
// names between translation strings
(watcher[aname] || []).map(function(unregister) { unregister(); });
watcher[aname] = [];
if (plural) {
watcher[aname].push(
$scope.$watch(plural, function(nv, ov) {
if (nv !== ov) transpolate(id_pl, aname);
}));
}
if (transpolated) {
// if the translation string is interpolated,
// we need to watch for changes
watcher[aname].push(
$scope.$watch(transpolated, function(nv) {
elem.attr(aname, nv);
}));
} else {
elem.attr(aname, translated);
}
}
// render all attributes
function re_compile() {
angular.forEach(attr_map, transpolate);
}
$scope.$on('language_changed', re_compile);
re_compile();
}
}
}
}]).
/**
* @name i19.i19-name
* @class i19-name directive
*
* @description
* Marker for HTML elements inside i19 directives
*
* @example
* <i19="outer"> Click <a i19-name="link"> here </a> to continue </i19>
*/
directive('i19-name', ['$i19', '$compile', function($i19, $compile) {
return {
restrict: 'A',
priority: 100,
compile: function(elem, attrs, transclude) {
// the i19 directive on the outer element will load this HTML from
// the translation string later, so we remove it here to prevent any binding
// to happen in the meantime
elem.remove();
}
}
}]);