-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
ui-i18n.js
304 lines (267 loc) · 8.9 KB
/
ui-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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* ui.i18n Created by Tim Sweet on 2/1/14.
* https://github.com/timothyswt
* MIT License
*/
/**
* @ngdoc overview
* @name ui.grid.i18n
* @description
*
* # ui.grid.i18n
* This module provides i18n functions to ui.grid and any application that wants to use it
*
* <div doc-module-components="ui.grid.i18n"></div>
*/
(function () {
var DIRECTIVE_ALIASES = ['uiT', 'uiTranslate'];
var FILTER_ALIASES = ['t', 'uiTranslate'];
var module = angular.module('ui.grid.i18n');
/**
* @ngdoc object
* @name ui.grid.i18n.constant:i18nConstants
*
* @description constants available in i18n module
*/
module.constant('i18nConstants', {
MISSING: '[MISSING]',
UPDATE_EVENT: '$uiI18n',
LOCALE_DIRECTIVE_ALIAS: 'uiI18n',
// default to english
DEFAULT_LANG: 'en'
});
// module.config(['$provide', function($provide) {
// $provide.decorator('i18nService', ['$delegate', function($delegate) {}])}]);
/**
* @ngdoc service
* @name ui.grid.i18n.service:i18nService
*
* @description Services for i18n
*/
module.service('i18nService', ['$log', 'i18nConstants', '$rootScope',
function ($log, i18nConstants, $rootScope) {
var langCache = {
_langs: {},
current: null,
get: function (lang) {
return this._langs[lang.toLowerCase()];
},
add: function (lang, strings) {
var lower = lang.toLowerCase();
if (!this._langs[lower]) {
this._langs[lower] = {};
}
angular.extend(this._langs[lower], strings);
},
getAllLangs: function () {
var langs = [];
if (!this._langs) {
return langs;
}
for (var key in this._langs) {
langs.push(key);
}
return langs;
},
setCurrent: function (lang) {
this.current = lang.toLowerCase();
},
getCurrentLang: function () {
return this.current;
}
};
var service = {
/**
* @ngdoc service
* @name add
* @methodOf ui.grid.i18n.service:i18nService
* @description Adds the languages and strings to the cache. Decorate this service to
* add more translation strings
* @param {string} lang language to add
* @param {object} stringMaps of strings to add grouped by property names
* @example
* <pre>
* i18nService.add('en', {
* aggregate: {
* label1: 'items',
* label2: 'some more items'
* }
* },
* groupPanel: {
* description: 'Drag a column header here and drop it to group by that column.'
* }
* }
* </pre>
*/
add: function (langs, stringMaps) {
if (typeof(langs) === 'object') {
angular.forEach(langs, function (lang) {
if (lang) {
langCache.add(lang, stringMaps);
}
});
} else {
langCache.add(langs, stringMaps);
}
},
/**
* @ngdoc service
* @name getAllLangs
* @methodOf ui.grid.i18n.service:i18nService
* @description return all currently loaded languages
* @returns {array} string
*/
getAllLangs: function () {
return langCache.getAllLangs();
},
/**
* @ngdoc service
* @name get
* @methodOf ui.grid.i18n.service:i18nService
* @description return all currently loaded languages
* @param {string} lang to return. If not specified, returns current language
* @returns {object} the translation string maps for the language
*/
get: function (lang) {
var language = lang ? lang : service.getCurrentLang();
return langCache.get(language);
},
/**
* @ngdoc service
* @name getSafeText
* @methodOf ui.grid.i18n.service:i18nService
* @description returns the text specified in the path or a Missing text if text is not found
* @param {String} path property path to use for retrieving text from string map
* @param {String} [lang] to return. If not specified, returns current language
* @returns {object} the translation for the path
* @example
* <pre>
* i18nService.getSafeText('sort.ascending')
* </pre>
*/
getSafeText: function (path, lang) {
var language = lang || service.getCurrentLang();
var trans = langCache.get(language);
if (!trans) {
return i18nConstants.MISSING;
}
var paths = path.split('.');
var current = trans;
for (var i = 0; i < paths.length; ++i) {
if (current[paths[i]] === undefined || current[paths[i]] === null) {
return i18nConstants.MISSING;
} else {
current = current[paths[i]];
}
}
return current;
},
/**
* @ngdoc service
* @name setCurrentLang
* @methodOf ui.grid.i18n.service:i18nService
* @description sets the current language to use in the application
* $broadcasts the i18nConstants.UPDATE_EVENT on the $rootScope
* @param {string} lang to set
* @example
* <pre>
* i18nService.setCurrentLang('fr');
* </pre>
*/
setCurrentLang: function (lang) {
if (lang) {
langCache.setCurrent(lang);
$rootScope.$broadcast(i18nConstants.UPDATE_EVENT);
}
},
/**
* @ngdoc service
* @name getCurrentLang
* @methodOf ui.grid.i18n.service:i18nService
* @description returns the current language used in the application
*/
getCurrentLang: function () {
var lang = langCache.getCurrentLang();
if (!lang) {
lang = i18nConstants.DEFAULT_LANG;
langCache.setCurrent(lang);
}
return lang;
}
};
return service;
}]);
var localeDirective = function (i18nService, i18nConstants) {
return {
compile: function () {
return {
pre: function ($scope, $elm, $attrs) {
var alias = i18nConstants.LOCALE_DIRECTIVE_ALIAS;
// check for watchable property
var lang = $scope.$eval($attrs[alias]);
if (lang) {
$scope.$watch($attrs[alias], function () {
i18nService.setCurrentLang(lang);
});
} else if ($attrs.$$observers) {
$attrs.$observe(alias, function () {
i18nService.setCurrentLang($attrs[alias] || i18nConstants.DEFAULT_LANG);
});
}
}
};
}
};
};
module.directive('uiI18n', ['i18nService', 'i18nConstants', localeDirective]);
// directive syntax
var uitDirective = function ($parse, i18nService, i18nConstants) {
return {
restrict: 'EA',
compile: function () {
return {
pre: function ($scope, $elm, $attrs) {
var alias1 = DIRECTIVE_ALIASES[0],
alias2 = DIRECTIVE_ALIASES[1];
var token = $attrs[alias1] || $attrs[alias2] || $elm.html();
var missing = i18nConstants.MISSING + token;
var observer;
if ($attrs.$$observers) {
var prop = $attrs[alias1] ? alias1 : alias2;
observer = $attrs.$observe(prop, function (result) {
if (result) {
$elm.html($parse(result)(i18nService.getCurrentLang()) || missing);
}
});
}
var getter = $parse(token);
var listener = $scope.$on(i18nConstants.UPDATE_EVENT, function (evt) {
if (observer) {
observer($attrs[alias1] || $attrs[alias2]);
} else {
// set text based on i18n current language
$elm.html(getter(i18nService.get()) || missing);
}
});
$scope.$on('$destroy', listener);
$elm.html(getter(i18nService.get()) || missing);
}
};
}
};
};
angular.forEach( DIRECTIVE_ALIASES, function ( alias ) {
module.directive( alias, ['$parse', 'i18nService', 'i18nConstants', uitDirective] );
} );
// optional filter syntax
var uitFilter = function ($parse, i18nService, i18nConstants) {
return function (data) {
var getter = $parse(data);
// set text based on i18n current language
return getter(i18nService.get()) || i18nConstants.MISSING + data;
};
};
angular.forEach( FILTER_ALIASES, function ( alias ) {
module.filter( alias, ['$parse', 'i18nService', 'i18nConstants', uitFilter] );
} );
})();