forked from pencilblue/pencilblue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localization.js
executable file
·1138 lines (988 loc) · 37.6 KB
/
localization.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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2016 PencilBlue, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
//dependencies
var fs = require('fs');
var path = require('path');
var Locale = require('locale');
var util = require('./util.js');
module.exports = function LocalizationModule(pb) {
//pb dependencies
var config = pb.config;
/**
* Provides functions to translate items based on keys. Also
* assists in the determination of the best language for the given user.
*
* @module Services
* @class Localization
* @constructor
* @param {Request|string} request The request object
* @param {Object} [options]
* @param {String} [options.activeTheme]
* @param {Array} [options.supported] The languages that the localization
* instance should be limited to.
*/
function Localization(request, options){
if (!util.isObject(options)) {
options = {};
}
/**
* expected to be lowercase and of the form "en-us"
* @property language Really should be renamed to locale in the future
* @type {String}
*/
this.language = Localization.best(request, options.supported).toString();
/**
* @property localeObj
* @type {Object}
*/
this.localeObj = Localization.parseLocaleStr(this.language);
/**
* Stores the keys already retrieved for the instance to prevent duplicate retrieval.
* @property cache
* @type {Object}
*/
this.cache = {};
/**
* The currently active theme that should be prioritized when
* performing key lookup
* @property activeTheme
* @type {string}
*/
this.activeTheme = options.activeTheme;
}
/**
*
* @property JS_EXT
* @type {String}
*/
var JS_EXT = '.js';
/**
*
* @static
* @readonly
* @property SEP
* @type {String}
*/
Localization.SEP = '^';
/**
*
* @static
* @readonly
* @property PREFIX
* @type {String}
*/
Localization.PREFIX = Localization.SEP + 'loc_';
/**
*
* @static
* @readonly
* @property ACCEPT_LANG_HEADER
* @type {String}
*/
Localization.ACCEPT_LANG_HEADER = 'accept-language';
/**
*
* @static
* @readonly
* @property LOCALE_PART_SEP
* @type {String}
*/
Localization.LOCALE_PART_SEP = '-';
/**
*
* @static
* @readonly
* @property KEY_SEP
* @type {String}
*/
Localization.KEY_SEP = '.';
/**
*
* @static
* @readonly
* @property storage
* @type {Object}
*/
Localization.storage = {};
/**
*
* @static
* @readonly
* @property supported
* @type {Locales}
*/
Localization.supported = null;
/**
* @static
* @readonly
* @property supportedLookup
* @type {Object}
*/
Localization.supportedLookup = {};
/**
* @static
* @readonly
* @property PARAM_REPLACEMENT_REGEX
* @type {RegExp}
*/
Localization.PARAM_REPLACEMENT_REGEX = /{([^{}]*)}/g;
/**
* Localizes a string by searching for keys within the template and replacing
* them with the specified values.
* @deprecated
* @method localize
* @param {Array} sets The localizations sets to search in
* @param {string} text The text to localize
* @param {string} hostname The current hostname
* @return {string} The text where keys have been replaced with translated values
*/
Localization.prototype.localize = function(sets, text, hostname){
if (pb.log.isSilly()) {
pb.log.silly('Localization: Localizing text - Locale [%s] Sets %s', this.language, JSON.stringify(sets));
}
pb.log.warn('Localization: localize is deprecated and will be removed in 1.0. Place your localizations in templates');
//get i18n from storage
var loc = Localization.storage;
if (util.isNullOrUndefined(loc)) {
throw new Error("Failed to set a language. LANG="+util.inspect(this.language));
}
//to squeeze a little performance out of an outdated function we convert the sets to a quick lookup to reduce
// the number of comparisons necessary to see if we should even check for a localization
var self = this;
var superSets = ['generic'];
util.arrayPushAll(sets, superSets);
var setLookup = superSets.reduce(function(lookup, prefix) {
lookup[prefix] = true;
return lookup;
}, {});
text = Object.keys(Localization.storage).reduce(function(content, key) {
//look to see if the set even matches
var pieces = key.split('.');
//see if the set matches only when the set is in the lookup and when the key is nested (backward compatibility piece)
if (pieces.length === 1 || !setLookup[pieces[0]]) {
return content;
}
var setVal = self.g(key/*, {empty options}*/);
return content.split('^loc_' + pieces[1] + '^').join(setVal);
}, text);
// If the localization is for HTML output, load the localization into client side JS
if (text.indexOf('<body') >= 0) {
text = text.concat(pb.ClientJs.includeJS(pb.UrlService.createSystemUrl('api/localization/script?locale=' + this.language, { hostname: hostname })));
}
return text;
};
/**
* Translates a single key. The function accepts a variable number of
* parameters. The first must be the key to be localized. The rest are
* considered to be injectable values. The function will call "util.format" in
* situations where the key is found and the nuber of arguments passed to the
* function is greater than 1. See
* http://nodejs.org/api/util.html#util_util_format_format for details on
* supported formatting.
* @deprecated Since 0.5.0
* @method get
* @param {String} key
* @param {String|Integer|Number|Object} [args] The variable number of
* parameters to be injected into the localization value
* @return {string|null} The formatted and localized string
*/
Localization.prototype.get = function() {
var key = arguments[0];
if (pb.log.isSilly()) {
pb.log.silly('Localization: Localizing key [%s] - Locale [%s]', key, this.language);
}
//error checking
if (!pb.validation.isNonEmptyStr(key, true)) {
return null;
}
//create a key that can be converted over to what we need
var convertedKey = key;
if (util.isNullOrUndefined(Localization.storage[key])) {
//now we must loop over the keys to see if we were given only the second half of a localization key
//this is extremely inefficient and hence why the function is deprecated
var keys = Object.keys(Localization.storage);
for (var i = 0; i < keys.length; i++) {
//check to see if the the key is partitioned
var index = keys[i].indexOf('.');
if (index < 0) {
continue;
}
//now we check to see if we were only given the second half of the localization key
if (keys[i].substr(index + 1) === key) {
convertedKey = keys[i];
pb.log.debug('Localization: Using the localization key %s is non-performant and should be updated to %s', key, convertedKey);
break;
}
}
//just assign the key. Won't find anything though.
if (util.isNullOrUndefined(convertedKey)) {
convertedKey = key;
}
}
var val = this.g(convertedKey/*, {empty options}*/);
if (val !== null) {
arguments[0] = val;
val = util.format.apply(util, arguments);
}
return val;
};
/**
*
* @method g
* @param {String} key
* @param {Object} [options]
* @param {String} [options.site=global]
* @param {Object} [options.params={}]
* @param {Object} [options.plugin]
* @return {String}
*/
Localization.prototype.g = function(key, options) {
options = options || {
site: pb.SiteService.GLOBAL_SITE,
params: {}
};
//log operation
if (pb.log.isSilly()) {
pb.log.silly('Localization: Localizing key [%s] - Locale [%s]', key, this.language);
}
//error checking
if (!util.isString(key)) {
throw new Error('key parameter is required');
}
if (!util.isObject(options)) {
throw new Error('options parameter must be an object');
}
var params = options.params || {};
if (!util.isObject(params)) {
throw new Error('params parameter is required');
}
//TODO retrieve active plugins for site to narrow down which plugins should be examined during retrieval
//get the current local as object
var locale = this.localeObj;
//get theme to prioritize
var plugin = options.plugin || this.activeTheme;
//define convenience functions
var self = this;
var processValue = function(localization) {
//set cache
self.cache[key] = localization;
//finish processing the value
return localization.isParameterized ?
Localization.replaceParameters(localization.value, params, options.defaultParamVal) :
localization.value;
};
var processKeyBlock = function(keyBlock) {
//check for plugin specific
if (!util.isNullOrUndefined(keyBlock.__plugins)) {
var pluginsBlock = keyBlock.__plugins;
//check for active plugin first
if (!util.isNullOrUndefined(pluginsBlock[plugin])) {
//we found a plugin specific value
return processValue(pluginsBlock[plugin]);
}
//check to see if the other plugins support the key
var pluginsToInspect = Object.keys(pluginsBlock);
for (var j = 0; j < pluginsToInspect.length; j++) {
//skip the active plugin bc we've already checked it
if (plugin === pluginsToInspect[j]) {
continue;
}
//examine the plugin
if (!util.isNullOrUndefined(pluginsBlock[pluginsToInspect[j]])) {
//we found a country & plugin specific value
return processValue(pluginsBlock[pluginsToInspect[j]]);
}
}
}
//no plugin specific key was found. Now check the defaults
if (!util.isNullOrUndefined(keyBlock.__default)) {
return processValue(keyBlock.__default);
}
//couldn't find it in this block
return null;
};
var processLanguageBlock = function(langBlock) {
if (!util.isObject(langBlock)) {
return null;
}
//check for country specific
if (util.isString(locale.countryCode)) {
var countryKey = k(locale.countryCode);
if (!util.isNullOrUndefined(langBlock[countryKey])) {
var countryResult = processKeyBlock(langBlock[countryKey]);
if (util.isString(countryResult)) {
return countryResult;
}
}
}
//we failed to find the value in a country specific block. We need to
//move on to the language
var langResult = processKeyBlock(langBlock);
if (util.isString(langResult)) {
return langResult;
}
//we couldn't find it in this locale
return null;
};
var finalize = function(result) {
return util.isString(result) || options.defaultVal !== undefined ? result : key;
};
//verify that key even exists, if not we're done. Just send back the default val, if provided
if (!Localization.storage[key]) {
return finalize(options.defaultVal);
}
else if (this.cache[key]) {
//we have already processed this key once for this instance
return finalize(processValue(this.cache[key]));
}
//key create key path
var keyBlock = findKeyBlock(key, false);
if (!keyBlock) {
return null;
}
//we found the key. Now we need to dig around and figure out which
//value to pick
var langKey = k(locale.language);
var result = processLanguageBlock(keyBlock[langKey]);
if (!util.isString(result)) {
//check to see if we should fall back to the default locale
var defaultLocale = Localization.parseLocaleStr(Localization.getDefaultLocale());
if (defaultLocale.language !== this.localeObj.language || defaultLocale.countryCode !== this.localeObj.countryCode) {
locale = defaultLocale;
langKey = k(defaultLocale.language);
result = processLanguageBlock(keyBlock[langKey]);
}
else {
result = options.defaultVal;
}
}
//finally, if we have a string result return it otherwise settle on the key
return finalize(result);
};
/**
* Determines the best language to send a user based on the 'accept-language'
* header in the request
*
* @method best
* @param {Request|String} request The request object
* @param {Array} [supported] The array of supported locales
* @return {string} Locale for the request
*/
Localization.best = function(request, supported){
supported = util.isArray(supported) ?
new Locale.Locales(supported) : Localization.supported;
var locales;
var loc = Localization.getDefaultLocale();
if (request) {
var acceptLangStr = util.isString(request) ? request :
(request.headers[Localization.ACCEPT_LANG_HEADER] || loc);
locales = new Locale.Locales(acceptLangStr);
loc = locales.best(supported);
}
return loc;
};
/**
* Initializes the location. Loads all language packs into memory for fast
* retrieval and sets the supported locales for determining what language to
* send the user based on their list of acceptable languages.
*
* @method init
* @param {Function} cb
*/
Localization.init = function(cb) {
Localization.storage = {};
//create path to localization directory
var options = {
recursive: false,
filter: function(filePath) { return filePath.indexOf(JS_EXT) === filePath.length - JS_EXT.length; }
};
var localizationDir = path.join(pb.config.docRoot, 'public', 'localization');
util.getFiles(localizationDir, options, function(err, files) {
if (util.isError(err)) {
return cb(err);
}
var compoundedResult = true;
files.forEach(function(file) {
//parse the file
var obj = null;
try {
obj = require(file);
}
catch(e) {
pb.log.warn('Localization: Failed to load core localization file [%s]. %s', file, e.stack);
//we failed so skip this file
return;
}
//convert file name to locale
var localeObj = Localization.parseLocaleStr(file);
//register the localization as defaults (call private method)
compoundedResult = compoundedResult && Localization._registerLocale(localeObj, obj);
});
//set the supported locales
pb.log.debug("Localization: Supporting - " + JSON.stringify(Object.keys(Localization.supportedLookup)));
cb(null, compoundedResult);
});
};
/**
* Determines if there have been keys registered for the specified locale.
* An example locale string would be: en-US. Where the characters to the
* left of the dash are the language code and the characters to the right
* of the dash represent the country code.
* @static
* @method isSupported
* @param {String} locale
* @return {Boolean}
*/
Localization.isSupported = function(locale) {
if (!util.isString(locale) || locale.length === 0) {
return false;
}
//make sure the locale is properly formatted
var localeObj = Localization.parseLocaleStr(locale);
locale = Localization.formatLocale(localeObj.language, localeObj.countryCode);
return !!Localization.supportedLookup[locale];
};
/**
* Retrieves the localization package for the specified locale
* @static
* @method getLocalizationPackage
* @param {String} locale
* @param {Object} [options] See options for Localization.g
* @return {Object}
*/
Localization.getLocalizationPackage = function(locale, options) {
if (!pb.validation.isNonEmptyStr(locale, true)) {
return null;
}
var ls = new Localization(locale);
var packageObj = {};
Object.keys(Localization.storage).forEach(function(key) {
var result = ls.g(key, options);
var parts = key.split(Localization.KEY_SEP);
if (parts.length === 1) {
packageObj[key] = result;
return;
}
var block = packageObj;
for (var i = 0; i < parts.length; i++) {
if (i === parts.length - 1) {
block[parts[i]] = result;
break;
}
else if (util.isNullOrUndefined(block[parts[i]])) {
block[parts[i]] = {};
}
block = block[parts[i]];
}
});
return packageObj;
};
/**
* @deprecated since 0.5.0
* @static
* @method registerLocalizations
* @param {String} locale
* @param {Object} localizations
* @param {object} options
* @param {string} [options.plugin]
* @return {Boolean}
*/
Localization.registerLocalizations = function(locale, localizations, options) {
pb.log.warn('Localization: Localization.registerLocalizations is deprecated. Use Localization.registerLocale');
return Localization.registerLocale(locale, localizations, options);
};
/**
* Registers a localization package for the provided locale and plugin.
* @private
* @static
* @method _registerLocale
* @param {String|Object} locale
* @param {Object} localizations
* @param {Object} [options]
* @param {String} [options.plugin]
* @return {Boolean}
*/
Localization.registerLocale = function(locale, localizations, options) {
assertOptions(options);
return Localization._registerLocale(locale, localizations, options);
};
/**
* Registers a localization package for the provided locale. Optionally,
* the packaged can be scoped to a specific plugin.
* @private
* @static
* @method _registerLocale
* @param {String|Object} locale
* @param {string} [locale.language] Only required when passing locale as an object
* @param {string} [locale.countryCode]
* @param {Object} localizations
* @param {Object} [options]
* @param {string} [options.plugin=SYSTEM]
* @return {Boolean}
*/
Localization._registerLocale = function(locale, localizations, options) {
locale = parseLocale(locale);
if (!util.isObject(localizations)) {
throw new Error('localizations parameter is required');
}
if (!util.isObject(options)) {
options = {};
}
//log it
if (pb.log.isSilly()) {
pb.log.silly('Localization: Registering locale [%s] for plugin [%s]', Localization.formatLocale(locale.language, locale.countryCode), options.plugin || 'SYSTEM');
}
//load up top level keys into the queue
var queue = [];
var processObj = function(prefix, obj) {
util.forEach(obj, function(val, key) {
queue.push({
key: prefix ? prefix + Localization.KEY_SEP + key : key,
val: val
});
});
};
processObj(null, localizations);
var compoundedResult = true;
while(queue.length > 0) {
var item = queue.shift();
if (util.isObject(item.val)) {
processObj(item.key, item.val);
}
else if (util.isString(item.val)){
compoundedResult = compoundedResult && Localization._registerLocalization(locale, item.key, item.val, options);
}
else {
compoundedResult = false;
pb.log.warn('Localization: Locale [%s] key [%s] provided an invalid value: %s', Localization.formatLocale(locale.language, locale.countryCode), item.key, JSON.stringify(item.val));
}
}
//ensure that we add the supported localization
if (compoundedResult && !Localization.isSupported(locale)) {
Localization.supportedLookup[Localization.formatLocale(locale.language, locale.countryCode)] = locale;
Localization.supported = new Locale.Locales(Object.keys(Localization.supportedLookup));
}
return compoundedResult;
};
/**
* Registers a single localization key for the provided locale and plugin.
* @private
* @static
* @method _registerLocalization
* @param {String} locale
* @param {String} key
* @param {String} value
* @param {Object} options
* @param {String} options.plugin
* @return {Boolean}
*/
Localization.registerLocalization = function(locale, key, value, options) {
assertOptions(options);
return Localization._registerLocalization(locale, key, value, options);
};
/**
* Registers a single localization key for the provided locale. Optionally, the localization can be scoped to a single plugin.
* @private
* @static
* @method _registerLocalization
* @param {String|object} locale
* @param {string} [locale.language] Only required when passing locale as an object
* @param {string} [locale.countryCode]
* @param {String} key
* @param {String} value
* @param {Object} [options]
* @param {String} [options.plugin]
* @return {Boolean}
*/
Localization._registerLocalization = function(locale, key, value, options) {
locale = parseLocale(locale);
if (!util.isString(key)) {
throw new Error('key parameter is required');
}
if (!util.isString(value)) {
throw new Error('value parameter is required');
}
//set defaults
if (!util.isObject(options)) {
options = {};
}
//ensure that the key path exists and set a reference to the block that
//represents the key. We are essentially walking the tree to get to
//the key. When a child node does not exist we create it.
var keyBlock = findKeyBlock(key, true);
//ensure that the language block exists
var langKey = k(locale.language);
if (util.isNullOrUndefined(keyBlock[langKey])) {
keyBlock[langKey] = {};
}
var insertionBlock = keyBlock[langKey];
//check to see if we need to move to a country code block
if (util.isString(locale.countryCode)) {
var countryKey = k(locale.countryCode);
if (util.isNullOrUndefined(insertionBlock[countryKey])) {
insertionBlock[countryKey] = {};
}
insertionBlock = insertionBlock[countryKey];
}
//check to see if we are setting a default localization or a plugin specific one
var valueBlock = {
value: value,
isParameterized: Localization.containsParameters(value)
};
if (util.isString(options.plugin)) {
if (util.isNullOrUndefined(insertionBlock.__plugins)) {
insertionBlock.__plugins = {};
}
insertionBlock.__plugins[options.plugin] = valueBlock;
}
else { //we are inserting a system default
insertionBlock.__default = valueBlock;
}
return true;
};
/**
* Removes a locale and all keys associated with it. Optionally, the
* operation can be scoped to a single plugin.
* @static
* @method unregisterLocale
* @param {String|Object} locale
* @param {string} [locale.language] Only required when locale is passed as an object
* @param {string} [locale.countryCode]
* @param {Object} [options]
* @param {String} [options.plugin]
* @return {Boolean}
*/
Localization.unregisterLocale = function(locale, options) {
locale = parseLocale(locale);
//iterate over all of the keys
var keysRemoved = Object.keys(Localization.storage).reduce(function(prev, key) {
return prev + Localization.unregisterLocalization(locale, key, options);
}, 0);
//remove from quick lookup
delete Localization.supportedLookup[Localization.formatLocale(locale.language, locale.countryCode)];
Localization.supported = new Locale.Locales(Object.keys(Localization.supportedLookup));
return keysRemoved > 0;
};
/**
* Unregisters a single key for the given locale. The locale can be just
* the language or the combination of the language and country code.
* Additionally, the operation can be scoped to a single plugin.
* @static
* @method unregisterLocalization
* @param {String|Object} locale
* @param {string} [locale.language] Only required when passing locale as an object
* @param {string} [locale.countryCode]
* @param {String} key
* @param {Object} [options]
* @param {String} [options.plugin]
* @return {Boolean}
*/
Localization.unregisterLocalization = function(locale, key, options) {
locale = parseLocale(locale);
if (!util.isString(key)) {
throw new Error('key parameter is required');
}
if (!util.isObject(options)) {
options = {};
}
//ensure that the key even exists
if (!Localization.storage[key]) {
return false;
}
//walk the tree looking for the key
var keyBlock = findKeyBlock(key, false);
if (!keyBlock) {
return false;
}
var langKey = k(locale.language);
var langBlock = keyBlock[langKey];
if (util.isNullOrUndefined(langBlock)) {
//the language could not be found
return false;
}
//check for country
if (util.isString(locale.countryCode)) {
//the lang block contains a key for the country code
var countryKey = k(locale.countryCode);
if (!util.isNullOrUndefined(langBlock[countryKey])) {
// look to see if a plugin was specified
var countryBlock = langBlock[countryKey];//translate to plugin key
if (util.isString(options.plugin)) {
//the plugin was specified so we should check the country code block for a sub-section for the plugin
if (util.isString(countryBlock[options.plugin])) {
delete countryBlock[options.plugin];
return true;
}
//we were asked to target the plugin only
return false;
}
}
//no plugin so we should fall through to the default block
if (util.isObject(langBlock[countryKey].__default)) {
delete langBlock[countryKey].__default;
return true;
}
return false;
}
//check the plugin at the lang level
if (util.isString(options.plugin) && util.isObject(langBlock.__plugins)) {
if (util.isString(langBlock.__plugins[options.plugin])) {
delete langBlock.__plugins[options.plugin];
return true;
}
//we were asked to target the plugin only
return false;
}
//finally check the default
if (util.isString(langBlock.__default)) {
delete langBlock.__default;
return true;
}
return false;
};
/**
* Retrieves the default locale for the instance. It first inspects the
* Configuration property localization.defaultLocale. As a last resort it
* will fall back to english. The locale is expected to be of the form:
* [language code]_[country code]
* @static
* @method getDefaultLocale
* @return {String} The default locale
*/
Localization.getDefaultLocale = function() {
return config.localization.defaultLocale || 'en-US';
};
/**
* Retrieves the supported locales
* @static
* @method getSupported
* @return {Array}
*/
Localization.getSupported = function() {
return util.clone(Localization.supported);
};
/**
* Determines if a raw localization value contains named parameters
* @static
* @method containsParameters
* @param {String} localizationValue
* @return {Boolean}
*/
Localization.containsParameters = function(localizationValue) {
if (!util.isString(localizationValue)) {
throw new Error('localizationParameter is required');
}
return localizationValue.search(Localization.PARAM_REPLACEMENT_REGEX) >= 0;
};
/**
* Given a raw localization value and a set of parameters the function will
* attempt to replace the named parameters in the raw value with the values
* provided in the params. When a named parameter is found that was not
* provided a value the defaultVal parameter value is used.
* @static
* @method replaceParameters
* @param {String} value
* @param {Object} params
* @param {String} [defaultVal]
* @return {String}
*/
Localization.replaceParameters = function(value, params, defaultVal) {
if (!util.isString(value)) {
throw new Error('value parameter is required');
}
if (!util.isObject(params)) {
throw new Error('params parameter is required');
}
//We went with a homegrown solution here because it is ~4 times faster
//than the regex expression solution:
//http://stackoverflow.com/questions/1408289/how-can-i-do-string-interpolation-in-javascri
var prev = null;
var isOpen = false;
var variable = '';
var val = '';
for (var i = 0; i < value.length; i++) {
if (!isOpen && value[i] === '{' && prev !== '%') {
isOpen = true;
}
else if (isOpen && value[i] === '}') {
val += params[variable] || defaultVal || variable;
isOpen = false;
variable = '';
}
else if (isOpen) {
variable += value[i];
}
else {
val += value[i];
}
prev = value[i];
}
return val;
};
/**
* Retrieves the supported locales as an array where each item in the array
* contains a value (locale) and a name (locale specific representation of
* the locale).
* @static
* @method getSupportedWithDisplay
* @return {Array}
*/
Localization.getSupportedWithDisplay = function() {
var supported = Localization.getSupported();
return supported.map(function(locale) {
var localization = new Localization(locale);
return {
value: locale,
name: localization.g('generic.LOCALE_DISPLAY'/*, {empty options}*/)
};
});
};
/**
* Parses a locale or file path to a locale file and extracts the language
* and country code into an object.
* @static
* @method parseLocaleStr
* @param {String|object} filePath
* @param {string} [filePath.language] Only required when passing filePath as an object
* @param {string} [filePath.countryCode] Only required when passing filePath as an object
* @return {Object}