This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtranslate.js
860 lines (774 loc) · 26 KB
/
translate.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/**
* @ngdoc object
* @name pascalprecht.translate.$translateProvider
* @description
*
* $tranlateProvider allows developers to register translation-tables, asynchronous loaders
* and similar to configure translation behavior directly inside of a module.
*
*/
angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY', function ($STORAGE_KEY) {
var $translationTable = {},
$preferredLanguage,
$fallbackLanguage,
$uses,
$nextLang,
$storageFactory,
$storageKey = $STORAGE_KEY,
$storagePrefix,
$missingTranslationHandlerFactory,
$interpolationFactory,
$interpolatorFactories = [],
$loaderFactory,
$loaderOptions,
$notFoundIndicatorLeft,
$notFoundIndicatorRight,
NESTED_OBJECT_DELIMITER = '.';
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#translations
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Registers a new translation table either in general or for specific language key.
*
* You can register a translation table just by passing an object hash where a key
* represents a translation id and a value the concrete translation. Here is an
* example:
*
* <pre>
* // register translation table
* $translateProvider.translations({
* 'HEADLINE_TEXT':'Hey Guys, this is a headline!',
* 'SOME_TEXT': 'A text anywhere in the app.'
* });
* </pre>
*
* In the example above there are two registered translations,
* HEADLINE_TEXT and SOME_TEXT.
*
* To register a translation table for specific language, pass a defined language
* key as first parameter.
*
* <pre>
* // register translation table for language: 'de_DE'
* $translateProvider.translations('de_DE', {
* 'GREETING': 'Hallo Welt!'
* });
*
* // register another one
* $translateProvider.translations('en_US', {
* 'GREETING': 'Hello world!'
* });
* </pre>
*
* When registering multiple translation tables for for the same language key,
* the actual translation table gets extended. This allows you to define module
* specific translation which only get added, once a specific module is loaded in
* your app.
*
* Invoking this method with no arguments returns the translation table which was
* registered with no language key. Invoking it with a language key returns the
* related translation table.
*
* @param {string} key A language key.
* @param {object} translationTable A plain old JavaScript object that represents a translation table.
*
*/
var translations = function (langKey, translationTable) {
if (!langKey && !translationTable) {
return $translationTable;
}
if (langKey && !translationTable) {
if (angular.isString(langKey)) {
return $translationTable[langKey];
} else {
angular.extend($translationTable, flatObject(langKey));
}
} else {
if (!angular.isObject($translationTable[langKey])) {
$translationTable[langKey] = {};
}
angular.extend($translationTable[langKey], flatObject(translationTable));
}
return this;
};
var flatObject = function (data, path, result) {
var key, keyWithPath, val;
if (!path) {
path = [];
}
if (!result) {
result = {};
}
for (key in data) {
if (!data.hasOwnProperty(key)) continue;
val = data[key];
if (angular.isObject(val)) {
flatObject(val, path.concat(key), result);
} else {
keyWithPath = path.length ? ("" + path.join(NESTED_OBJECT_DELIMITER) + NESTED_OBJECT_DELIMITER + key) : key;
result[keyWithPath] = val;
}
}
return result;
};
this.translations = translations;
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#addInterpolation
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Adds interpolation services to angular-translate, so it can manage them.
*
* @param {object} factory Interpolation service factory
*/
this.addInterpolation = function (factory) {
$interpolatorFactories.push(factory);
return this;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useMessageFormatInterpolation
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to use interpolation functionality of messageformat.js.
* This is useful when having high level pluralization and gender selection.
*
*/
this.useMessageFormatInterpolation = function () {
return this.useInterpolation('$translateMessageFormatInterpolation');
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useInterpolation
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate which interpolation style to use as default, application-wide.
* Simply pass a factory/service name. The interpolation service has to implement
* the correct interface.
*
* @param {string} factory Interpolation service name.
*/
this.useInterpolation = function (factory) {
$interpolationFactory = factory;
return this;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#preferredLanguage
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells the module which of the registered translation tables to use for translation
* at initial startup by passing a language key. Similar to `$translateProvider#uses`
* only that it says which language to **prefer**.
*
* @param {string} langKey A language key.
*
*/
this.preferredLanguage = function(langKey) {
if (langKey) {
$preferredLanguage = langKey;
return this;
} else {
return $preferredLanguage;
}
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#translationNotFoundIndicator
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Sets an indicator which is used when a translation isn't found. E.g. when
* setting the indicator as 'X' and one tries to translate a translation id
* called `NOT_FOUND`, this will result in `X NOT_FOUND X`.
*
* Internally this methods sets a left indicator and a right indicator using
* `$translateProvider.translationNotFoundIndicatorLeft()` and
* `$translateProvider.translationNotFoundIndicatorRight()`.
*
* **Note**: These methods automatically add a whitespace between the indicators
* and the translation id.
*
* @param {string} indicator An indicator, could be any string.
*/
this.translationNotFoundIndicator = function (indicator) {
this.translationNotFoundIndicatorLeft(indicator);
this.translationNotFoundIndicatorRight(indicator);
return this;
};
/**
* ngdoc function
* @name pascalprecht.translate.$translateProvider#translationNotFoundIndicatorLeft
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Sets an indicator which is used when a translation isn't found left to the
* translation id.
*
* @param {string} indicator An indicator.
*/
this.translationNotFoundIndicatorLeft = function (indicator) {
if (!indicator) {
return $notFoundIndicatorLeft;
}
$notFoundIndicatorLeft = indicator;
return this;
};
/**
* ngdoc function
* @name pascalprecht.translate.$translateProvider#translationNotFoundIndicatorLeft
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Sets an indicator which is used when a translation isn't found right to the
* translation id.
*
* @param {string} indicator An indicator.
*/
this.translationNotFoundIndicatorRight = function (indicator) {
if (!indicator) {
return $notFoundIndicatorRight;
}
$notFoundIndicatorRight = indicator;
return this;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#fallbackLanguage
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells the module which of the registered translation tables to use when missing translations
* at initial startup by passing a language key. Similar to `$translateProvider#uses`
* only that it says which language to **fallback**.
*
* @param {string} langKey A language key.
*
*/
this.fallbackLanguage = function(langKey) {
if (langKey) {
$fallbackLanguage = langKey;
return this;
} else {
return $fallbackLanguage;
}
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#uses
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Set which translation table to use for translation by given language key. When
* trying to 'use' a language which isn't provided, it'll throw an error.
*
* You actually don't have to use this method since `$translateProvider#preferredLanguage`
* does the job too.
*
* @param {string} langKey A language key.
*
*/
this.uses = function (langKey) {
if (langKey) {
if (!$translationTable[langKey] && (!$loaderFactory)) {
// only throw an error, when not loading translation data asynchronously
throw new Error("$translateProvider couldn't find translationTable for langKey: '" + langKey + "'");
}
$uses = langKey;
return this;
} else {
return $uses;
}
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#storageKey
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells the module which key must represent the choosed language by a user in the storage.
*
* @param {string} key A key for the storage.
*
*/
var storageKey = function(key) {
if (!key) {
if ($storagePrefix) {
return $storagePrefix + $storageKey;
}
return $storageKey;
}
$storageKey = key;
};
this.storageKey = storageKey;
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useUrlLoader
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to use `$translateUrlLoader` extension service as loader.
*
* @param {string} url Url
*/
this.useUrlLoader = function (url) {
return this.useLoader('$translateUrlLoader', { url: url });
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useStaticFilesLoader
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to use `$translateStaticFilesLoader` extension service as loader.
*
* @param {Object=} options Optional configuration object
*/
this.useStaticFilesLoader = function (options) {
return this.useLoader('$translateStaticFilesLoader', options);
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useLoader
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to use any other service as loader.
*
* @param {string} loaderFactory Factory name to use
* @param {Object=} options Optional configuration object
*/
this.useLoader = function (loaderFactory, options) {
$loaderFactory = loaderFactory;
$loaderOptions = options || {};
return this;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useLocalStorage
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to use `$translateLocalStorage` service as storage layer.
*
*/
this.useLocalStorage = function () {
return this.useStorage('$translateLocalStorage');
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useCookieStorage
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to use `$translateCookieStorage` service as storage layer.
*
*/
this.useCookieStorage = function () {
return this.useStorage('$translateCookieStorage');
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useStorage
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to use custom service as storage layer.
*
*/
this.useStorage = function (storageFactory) {
$storageFactory = storageFactory;
return this;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#storagePrefix
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Sets prefix for storage key.
*
* @param {string} prefix Storage key prefix
*/
this.storagePrefix = function (prefix) {
if (!prefix) {
return prefix;
}
$storagePrefix = prefix;
return this;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useMissingTranslationHandlerLog
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to use built-in log handler when trying to translate
* a translation Id which doesn't exist.
*
* This is actually a shortcut method for `useMissingTranslationHandler()`.
*
*/
this.useMissingTranslationHandlerLog = function () {
return this.useMissingTranslationHandler('$translateMissingTranslationHandlerLog');
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useMissingTranslationHandler
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Expects a factory name which later gets instantiated with `$injector`.
* This method can be used to tell angular-translate to use a custom
* missingTranslationHandler. Just build a factory which returns a function
* and expects a translation id as argument.
*
* Example:
* <pre>
* app.config(function ($translateProvider) {
* $translateProvider.useMissingTranslationHandler('customHandler');
* });
*
* app.factory('customHandler', function (dep1, dep2) {
* return function (translationId) {
* // something with translationId and dep1 and dep2
* };
* });
* </pre>
*
* @param {string} factory Factory name
*/
this.useMissingTranslationHandler = function (factory) {
$missingTranslationHandlerFactory = factory;
return this;
};
/**
* @ngdoc object
* @name pascalprecht.translate.$translate
* @requires $interpolate
* @requires $log
* @requires $rootScope
* @requires $q
*
* @description
* The `$translate` service is the actual core of angular-translate. It excepts a translation id
* and optional interpolate parameters to translate contents.
*
* <pre>
* $scope.translatedText = $translate('HEADLINE_TEXT');
* </pre>
*
* @param {string} translationId A token which represents a translation id
* @param {object=} interpolateParams An object hash for dynamic values
*/
this.$get = [
'$log',
'$injector',
'$rootScope',
'$q',
function ($log, $injector, $rootScope, $q) {
var Storage,
defaultInterpolator = $injector.get($interpolationFactory || '$translateDefaultInterpolation'),
pendingLoader = false,
interpolatorHashMap = {};
var loadAsync = function (key) {
if (!key) {
throw "No language key specified for loading.";
}
var deferred = $q.defer();
$rootScope.$broadcast('$translateLoadingStart');
pendingLoader = true;
$nextLang = key;
$injector.get($loaderFactory)(angular.extend($loaderOptions, {
key: key
})).then(function (data) {
$rootScope.$broadcast('$translateLoadingSuccess');
var translationTable = {};
if (angular.isArray(data)) {
angular.forEach(data, function (table) {
angular.extend(translationTable, table);
});
} else {
angular.extend(translationTable, data);
}
translations(key, translationTable);
pendingLoader = false;
$nextLang = undefined;
deferred.resolve(key);
$rootScope.$broadcast('$translateLoadingEnd');
}, function (key) {
$rootScope.$broadcast('$translateLoadingError');
deferred.reject(key);
$nextLang = undefined;
$rootScope.$broadcast('$translateLoadingEnd');
});
return deferred.promise;
};
if ($storageFactory) {
Storage = $injector.get($storageFactory);
if (!Storage.get || !Storage.set) {
throw new Error('Couldn\'t use storage \'' + $storageFactory + '\', missing get() or set() method!');
}
}
// if we have additional interpolations that were added via
// $translateProvider.addInterpolation(), we have to map'em
if ($interpolatorFactories.length > 0) {
angular.forEach($interpolatorFactories, function (interpolatorFactory) {
var interpolator = $injector.get(interpolatorFactory);
// setting initial locale for each interpolation service
interpolator.setLocale($preferredLanguage || $uses);
// make'em recognizable through id
interpolatorHashMap[interpolator.getInterpolationIdentifier()] = interpolator;
});
}
var $translate = function (translationId, interpolateParams, interpolationId) {
// determine translation table and current Interpolator
var table = $uses ? $translationTable[$uses] : $translationTable,
Interpolator = (interpolationId) ? interpolatorHashMap[interpolationId] : defaultInterpolator;
// if the translation id exists, we can just interpolate it
if (table && table.hasOwnProperty(translationId)) {
return Interpolator.interpolate(table[translationId], interpolateParams);
}
// looks like the requested translation id doesn't exists.
// Now, if there is a registered handler for missing translations and no
// asyncLoader is pending, we execute the handler
if ($missingTranslationHandlerFactory && !pendingLoader) {
$injector.get($missingTranslationHandlerFactory)(translationId, $uses);
}
// since we couldn't translate the inital requested translation id,
// we try it now with a fallback language, if a fallback language is
// configured.
if ($uses && $fallbackLanguage && $uses !== $fallbackLanguage){
var translation = $translationTable[$fallbackLanguage][translationId];
// check if a translation for the fallback language exists
if (translation) {
var returnVal;
// temporarly letting Interpolator know we're using fallback language now.
Interpolator.setLocale($fallbackLanguage);
returnVal = Interpolator.interpolate(translation, interpolateParams);
// after we've interpolated the translation, we reset Interpolator to proper locale.
Interpolator.setLocale($uses);
return returnVal;
}
}
// applying notFoundIndicators
if ($notFoundIndicatorLeft) {
translationId = [$notFoundIndicatorLeft, translationId].join(' ');
}
if ($notFoundIndicatorRight) {
translationId = [translationId, $notFoundIndicatorRight].join(' ');
}
return translationId;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translate#preferredLanguage
* @methodOf pascalprecht.translate.$translate
*
* @description
* Returns the language key for the preferred language.
*
* @return {string} preferred language key
*/
$translate.preferredLanguage = function () {
return $preferredLanguage;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translate#fallbackLanguage
* @methodOf pascalprecht.translate.$translate
*
* @description
* Returns the language key for the fallback language.
*
* @return {string} fallback language key
*/
$translate.fallbackLanguage = function () {
return $fallbackLanguage;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translate#proposedLanguage
* @methodOf pascalprecht.translate.$translate
*
* @description
* Returns the language key of language that is currently loaded asynchronously.
*
* @return {string} language key
*/
$translate.proposedLanguage = function () {
return $nextLang;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translate#storage
* @methodOf pascalprecht.translate.$translate
*
* @description
* Returns registered storage.
*
* @return {object} Storage
*/
$translate.storage = function () {
return Storage;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translate#uses
* @methodOf pascalprecht.translate.$translate
*
* @description
* Tells angular-translate which language to uses by given language key. This method is
* used to change language at runtime. It also takes care of storing the language
* key in a configured store to let your app remember the choosed language.
*
* When trying to 'use' a language which isn't available it tries to load it
* asynchronously with registered loaders.
*
* Returns promise object with loaded language file data
* @example
* $translate.uses("en_US").then(function(data){
* $scope.text = $translate("HELLO");
* });
*
* @param {string} key Language key
* @return {string} Language key
*/
$translate.uses = function (key) {
if (!key) {
return $uses;
}
var deferred = $q.defer();
$rootScope.$broadcast('$translateChangeStart');
function useLanguage(key) {
$uses = key;
$rootScope.$broadcast('$translateChangeSuccess');
if ($storageFactory) {
Storage.set($translate.storageKey(), $uses);
}
// inform default interpolator
defaultInterpolator.setLocale($uses);
// inform all others to!
angular.forEach(interpolatorHashMap, function (interpolator, id) {
interpolatorHashMap[id].setLocale($uses);
});
deferred.resolve(key);
$rootScope.$broadcast('$translateChangeEnd');
}
// if there isn't a translation table for the language we've requested,
// we load it asynchronously
if (!$translationTable[key] && $loaderFactory) {
loadAsync(key).then(useLanguage, function (key) {
$rootScope.$broadcast('$translateChangeError');
deferred.reject(key);
$rootScope.$broadcast('$translateChangeEnd');
});
} else {
useLanguage(key);
}
return deferred.promise;
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translate#storageKey
* @methodOf pascalprecht.translate.$translate
*
* @description
* Returns the key for the storage.
*
* @return {string} storage key
*/
$translate.storageKey = function() {
return storageKey();
};
/**
* @ngdoc function
* @name pascalprecht.translate.$translate#refresh
* @methodOf pascalprecht.translate.$translate
*
* @description
* Refreshes a translation table pointed by the given langKey. If langKey is not specified,
* the module will drop all existent translation tables and load new version of those which
* are currently in use.
*
* Refresh means that the module will drop target translation table and try to load it again.
*
* In case there are no loaders registered the refresh() method will throw an Error.
*
* If the module is able to refresh translation tables refresh() method will broadcast
* $translateRefreshStart and $translateRefreshEnd events.
*
* @example
* // this will drop all currently existent translation tables and reload those which are
* // currently in use
* $translate.refresh();
* // this will refresh a translation table for the en_US language
* $translate.refresh('en_US');
*
* @param {string} lankKey A language key of the table, which has to be refreshed
*
* @return {promise} Promise, which will be resolved in case a translation tables refreshing
* process is finished successfully, and reject if not.
*/
$translate.refresh = function(langKey) {
var deferred = $q.defer();
function onLoadSuccess() {
deferred.resolve();
$rootScope.$broadcast('$translateRefreshEnd');
}
function onLoadFailure() {
deferred.reject();
$rootScope.$broadcast('$translateRefreshEnd');
}
if (!$loaderFactory) {
throw new Error('Couldn\'t refresh translation table, no loader registered!');
}
if (!langKey) {
$rootScope.$broadcast('$translateRefreshStart');
for (var lang in $translationTable) {
if ($translationTable.hasOwnProperty(lang)) {
delete $translationTable[lang];
}
}
var loaders = [];
if ($fallbackLanguage) {
loaders.push(loadAsync($fallbackLanguage));
}
if ($uses) {
loaders.push($translate.uses($uses));
}
if (loaders.length > 0) {
$q.all(loaders).then(onLoadSuccess, onLoadFailure);
} else onLoadSuccess();
} else if ($translationTable.hasOwnProperty(langKey)) {
$rootScope.$broadcast('$translateRefreshStart');
delete $translationTable[langKey];
var loader = null;
if (langKey === $uses) {
loader = $translate.uses($uses);
} else {
loader = loadAsync(langKey);
}
loader.then(onLoadSuccess, onLoadFailure);
} else deferred.reject();
return deferred.promise;
};
// If at least one async loader is defined and there are no (default) translations available
// we should try to load them.
if ($loaderFactory) {
if (angular.equals($translationTable, {})) {
$translate.uses($translate.uses());
}
if ($fallbackLanguage && !$translationTable[$fallbackLanguage]) {
loadAsync($fallbackLanguage);
}
}
return $translate;
}];
}]);