forked from rubenv/angular-gettext
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcatalog.js
427 lines (407 loc) · 16.1 KB
/
catalog.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
* @ngdoc service
* @module gettext
* @name gettextCatalog
* @requires gettextPlurals
* @requires gettextFallbackLanguage
* @requires https://docs.angularjs.org/api/ng/service/$http $http
* @requires https://docs.angularjs.org/api/ng/service/$cacheFactory $cacheFactory
* @requires https://docs.angularjs.org/api/ng/service/$interpolate $interpolate
* @requires https://docs.angularjs.org/api/ng/service/$rootScope $rootScope
* @description Provides set of method to translate stings
* @example
* This example shows translations behaviour with different settings.
<example module="gettextExample" name="gettextExample">
<file name="index.html">
<div ng-controller="GettextController as gtx">
<section class="left">
<label>
<span>Translation:</span>
<select
aria-label="current language"
ng-model="gtx.options.currentLanguage"
ng-change="gtx.options.setCurrentLanguage(gtx.options.currentLanguage)">
<option ng-repeat="(k, lang) in gtx.options.strings" value="{{k}}">{{k}}</option>
</select>
</label>
<label>
<span>Debug mode:</span>
<input type="checkbox"
ng-model="gtx.options.debug"
ng-change="gtx.update()"
aria-label="debug mode"/></label>
<label>
<span>Debug prefix:</span>
<input type="text"
ng-model="gtx.options.debugPrefix"
ng-change="gtx.update()"
placeholder="non-translated string prefix"
aria-label="debug prefix for untranslated strings" /></label>
<label>
<span>Show translated markers:</span>
<input type="checkbox"
ng-model="gtx.options.showTranslatedMarkers"
ng-change="gtx.update()"
aria-label="debug mode" /></label>
<label>
<span>Translated marker prefix:</span>
<input type="text"
ng-model="gtx.options.translatedMarkerPrefix"
ng-change="gtx.update()"
placeholder="translated string prefix"
aria-label="translated strings prefix for debug mode" /></label>
<label>
<span>Translated marker suffix:</span>
<input type="text"
ng-model="gtx.options.translatedMarkerSuffix"
ng-change="gtx.update()"
placeholder="translated string suffix"
aria-label="translated strings prefix for debug mode" /></label>
</section>
<section class="right">
<label>
<span>Your name:</span>
<input type="text" ng-model="gtx.name" placeholder="enter your name" aria-label="your name"/></label>
<label>
<span>"Hello" string</span>
<input type="text"
ng-change="gtx.update()"
ng-model="gtx.options.strings[gtx.options.currentLanguage]['Hi, \{\{gtx.name\}\}!'].$$noContext[0]" placeholder="{{'Hi, {{gtx.name}\}!'}}" aria-label="Hello string"/></label>
<label>
<span>"Bye" string</span>
<input type="text"
ng-change="gtx.update()"
ng-model="gtx.options.strings[gtx.options.currentLanguage]['Bye, \{\{gtx.name\}\}!'].$$noContext[0]" placeholder="{{'Bye, {{gtx.name}\}!'}}" aria-label="Bye string"/></label>
</section>
<ul>
<li class="translation">
<span>Translation: {{gtx.options.currentLanguage}}</span>
<ul>
<li>
<span translate>Hi, {{gtx.name}}!</span>
</li>
<li>
<span translate>Bye, {{gtx.name}}!</span>
</li>
</ul>
</li>
</ul>
</div>
</file>
<file name="script.js">
angular.module('gettextExample', ['gettext']).controller('GettextController', function($scope, gettextCatalog) {
gettextCatalog.debug = true;
gettextCatalog.showTranslatedMarkers = true;
gettextCatalog.translatedMarkerPrefix = '->';
gettextCatalog.translatedMarkerSuffix = '<-';
gettextCatalog.currentLanguage = 'nl';
gettextCatalog.setStrings('en-US', {
'Hi, {{gtx.name}}!': 'Hello, {{gtx.name}}!',
'Bye, {{gtx.name}}!': '',
});
gettextCatalog.setStrings('es-ES', {
'Hi, {{gtx.name}}!': '',
'Bye, {{gtx.name}}!': '¡Adious, {{gtx.name}}!',
})
gettextCatalog.setStrings('nl', {
'Hi, {{gtx.name}}!': 'Halo, {{gtx.name}}!',
'Bye, {{gtx.name}}!': 'Doei, {{gtx.name}}!',
});
this.name = 'Ruben';
this.options = gettextCatalog;
this.update = function () {
// a kind of hack, service settings are not tracked for real time changes
$scope.$root.$broadcast('gettextLanguageChanged');
}
});
</file>
<file name="style.css">
select,
input[type=text] {
width: 200px;
}
.translation {
display: inline-block;
list-style: none;
}
label {
display: block;
}
label > span {
display: inline-block;
width: 250px;
}
section {
display: inline-block;
padding: 5px;
}
section.right > label > span {
width: 120px;
}
</file>
</example>
*/
angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, gettextFallbackLanguage, $http, $cacheFactory, $interpolate, $rootScope) {
var catalog;
var noContext = '$$noContext';
// IE8 returns UPPER CASE tags, even though the source is lower case.
// This can causes the (key) string in the DOM to have a different case to
// the string in the `po` files.
// IE9, IE10 and IE11 reorders the attributes of tags.
var test = '<span id="test" title="test" class="tested">test</span>';
var isHTMLModified = (angular.element('<span>' + test + '</span>').html() !== test);
var prefixDebug = function (string) {
if (catalog.debug && catalog.currentLanguage !== catalog.baseLanguage) {
return catalog.debugPrefix + string;
} else {
return string;
}
};
var addTranslatedMarkers = function (string) {
if (catalog.showTranslatedMarkers) {
return catalog.translatedMarkerPrefix + string + catalog.translatedMarkerSuffix;
} else {
return string;
}
};
function broadcastUpdated() {
/**
* @ngdoc event
* @name gettextCatalog#gettextLanguageChanged
* @eventType broadcast on $rootScope
* @description Fires language change notification without any additional parameters.
*/
$rootScope.$broadcast('gettextLanguageChanged');
}
catalog = {
/**
* @ngdoc property
* @name gettextCatalog#debug
* @public
* @type {Boolean} false
* @see gettextCatalog#debug
* @description Whether or not to prefix untranslated strings with `[MISSING]:` or a custom prefix.
*/
debug: false,
/**
* @ngdoc property
* @name gettextCatalog#debugPrefix
* @public
* @type {String} [MISSING]:
* @description Custom prefix for untranslated strings when {@link gettextCatalog#debug gettextCatalog#debug} set to `true`.
*/
debugPrefix: '[MISSING]: ',
/**
* @ngdoc property
* @name gettextCatalog#showTranslatedMarkers
* @public
* @type {Boolean} false
* @description Whether or not to wrap all processed text with markers.
*
* Example output: `[Welcome]`
*/
showTranslatedMarkers: false,
/**
* @ngdoc property
* @name gettextCatalog#translatedMarkerPrefix
* @public
* @type {String} [
* @description Custom prefix to mark strings that have been run through {@link module:gettext angular-gettext}.
*/
translatedMarkerPrefix: '[',
/**
* @ngdoc property
* @name gettextCatalog#translatedMarkerSuffix
* @public
* @type {String} ]
* @description Custom suffix to mark strings that have been run through {@link module:gettext angular-gettext}.
*/
translatedMarkerSuffix: ']',
/**
* @ngdoc property
* @name gettextCatalog#strings
* @private
* @type {Object}
* @description An object of loaded translation strings. Shouldn't be used directly.
*/
strings: {},
/**
* @ngdoc property
* @name gettextCatalog#baseLanguage
* @protected
* @deprecated
* @since 2.0
* @type {String} en
* @description The default language, in which you're application is written.
*
* This defaults to English and it's generally a bad idea to use anything else:
* if your language has different pluralization rules you'll end up with incorrect translations.
*/
baseLanguage: 'en',
/**
* @ngdoc property
* @name gettextCatalog#currentLanguage
* @public
* @type {String}
* @description Active language.
*/
currentLanguage: 'en',
/**
* @ngdoc property
* @name gettextCatalog#cache
* @public
* @type {String} en
* @description Language cache for lazy load
*/
cache: $cacheFactory('strings'),
/**
* @ngdoc method
* @name gettextCatalog#setCurrentLanguage
* @public
* @param {String} lang language name
* @description Sets the current language and makes sure that all translations get updated correctly.
*/
setCurrentLanguage: function (lang) {
this.currentLanguage = lang;
broadcastUpdated();
},
/**
* @ngdoc method
* @name gettextCatalog#getCurrentLanguage
* @public
* @returns {String} current language
* @description Returns the current language.
*/
getCurrentLanguage: function () {
return this.currentLanguage;
},
/**
* @ngdoc method
* @name gettextCatalog#setStrings
* @public
* @param {String} language language name
* @param {Object.<String>} strings set of strings where the key is the translation `key` and `value` is the translated text
* @description Processes an object of string definitions. {@link guide:manual-setstrings More details here}.
*/
setStrings: function (language, strings) {
if (!this.strings[language]) {
this.strings[language] = {};
}
for (var key in strings) {
var val = strings[key];
if (isHTMLModified) {
// Use the DOM engine to render any HTML in the key (#131).
key = angular.element('<span>' + key + '</span>').html();
}
if (angular.isString(val) || angular.isArray(val)) {
// No context, wrap it in $$noContext.
var obj = {};
obj[noContext] = val;
val = obj;
}
// Expand single strings for each context.
for (var context in val) {
var str = val[context];
val[context] = angular.isArray(str) ? str : [str];
}
this.strings[language][key] = val;
}
broadcastUpdated();
},
/**
* @ngdoc method
* @name gettextCatalog#getStringFormFor
* @protected
* @param {String} language language name
* @param {String} string translation key
* @param {Number=} n number to build sting form for
* @param {String=} context translation key context, e.g. {@link docs:context Verb, Noun}
* @returns {String|Null} translated or annotated string or null if language is not set
* @description Translate a string with the given language, count and context.
*/
getStringFormFor: function (language, string, n, context) {
if (!language) {
return null;
}
var stringTable = this.strings[language] || {};
var contexts = stringTable[string] || {};
var plurals = contexts[context || noContext] || [];
return plurals[gettextPlurals(language, n)];
},
/**
* @ngdoc method
* @name gettextCatalog#getString
* @public
* @param {String} string translation key
* @param {$rootScope.Scope=} scope scope to do interpolation against
* @param {String=} context translation key context, e.g. {@link docs:context Verb, Noun}
* @returns {String} translated or annotated string
* @description Translate a string with the given scope and context.
*
* First it tries {@link gettextCatalog#currentLanguage gettextCatalog#currentLanguage} (e.g. `en-US`) then {@link gettextFallbackLanguage fallback} (e.g. `en`).
*
* When `scope` is supplied it uses Angular.JS interpolation, so something like this will do what you expect:
* ```js
* var hello = gettextCatalog.getString("Hello {{name}}!", { name: "Ruben" });
* // var hello will be "Hallo Ruben!" in Dutch.
* ```
* Avoid using scopes - this skips interpolation and is a lot faster.
*/
getString: function (string, scope, context) {
var fallbackLanguage = gettextFallbackLanguage(this.currentLanguage);
string = this.getStringFormFor(this.currentLanguage, string, 1, context) ||
this.getStringFormFor(fallbackLanguage, string, 1, context) ||
prefixDebug(string);
string = scope ? $interpolate(string)(scope) : string;
return addTranslatedMarkers(string);
},
/**
* @ngdoc method
* @name gettextCatalog#getPlural
* @public
* @param {Number} n number to build sting form for
* @param {String} string translation key
* @param {String} stringPlural plural translation key
* @param {$rootScope.Scope=} scope scope to do interpolation against
* @param {String=} context translation key context, e.g. {@link docs:context Verb, Noun}
* @returns {String} translated or annotated string
* @see {@link gettextCatalog#getString gettextCatalog#getString} for details
* @description Translate a plural string with the given context.
*/
getPlural: function (n, string, stringPlural, scope, context) {
var fallbackLanguage = gettextFallbackLanguage(this.currentLanguage);
string = this.getStringFormFor(this.currentLanguage, string, n, context) ||
this.getStringFormFor(fallbackLanguage, string, n, context) ||
prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
scope.$count = n;
string = $interpolate(string)(scope);
}
return addTranslatedMarkers(string);
},
/**
* @ngdoc method
* @name gettextCatalog#loadRemote
* @public
* @param {String} url location of the translations
* @description Load a set of translation strings from a given URL.
*
* This should be a JSON catalog generated with [angular-gettext-tools](https://github.com/rubenv/angular-gettext-tools).
* {@link guide:lazy-loading More details here}.
*/
loadRemote: function (url) {
return $http({
method: 'GET',
url: url,
cache: catalog.cache
}).then(function (response) {
var data = response.data;
for (var lang in data) {
catalog.setStrings(lang, data[lang]);
}
return response;
});
}
};
return catalog;
});