-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathcatalog.js
More file actions
294 lines (276 loc) · 11.3 KB
/
catalog.js
File metadata and controls
294 lines (276 loc) · 11.3 KB
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
/**
* @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
*/
angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, gettextFallbackLanguage, $cacheFactory, $interpolate, $rootScope, $injector) {
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 angular-gettext angular-gettext}.
*/
translatedMarkerPrefix: '[',
/**
* @ngdoc property
* @name gettextCatalog#translatedMarkerSuffix
* @public
* @type {String} ]
* @description Custom suffix to mark strings that have been run through {@link angular-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 doc: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 doc: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 doc: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 $injector.get('$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;
});