-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpo.js
858 lines (754 loc) · 29.9 KB
/
po.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
/*jslint regexp: true, eval: true */
/*global require: false, XMLHttpRequest: false, ActiveXObject: false,
define: false, window: false, process: false, Packages: false,
java: false, location: false */
define([
'module'
//>>excludeStart('excludePo', pragmas.excludePo)
,'messageformat'
//>>excludeEnd('excludePo')
], function (
module
//>>excludeStart('excludePo', pragmas.excludePo)
,MessageFormat
//>>excludeEnd('excludePo')
) {
//>>excludeStart('excludePo', pragmas.excludePo)
'use strict';
var sharedFuncs = {};
/**
* Parses a header string into an object of key-value pairs
*
* @param {String} str Header string
* @return {Object} An object of key-value pairs
*/
sharedFuncs.parseHeader = function (str){
var lines = (str || "").split("\n"),
headers = {};
lines.forEach(function(line){
var parts = line.trim().split(":"),
key = (parts.shift() || "").trim().toLowerCase(),
value = parts.join(":").trim();
if(!key){
return;
}
headers[key] = value;
});
return headers;
};
sharedFuncs.convert = function (po) {
var parser = new Parser(po, 'utf-8');
var translations = parser.parse();
var translationMap = {};
Object.keys(translations.translations['']).forEach(function (key) {
var msgstr = null;
if (key === '') {
return;
}
try {
msgstr = translations.translations[''][key].msgstr;
} catch (e) {}
if (msgstr !== null) {
msgstr.forEach(function (data) {
if (data !== null) {
translationMap[key] = data;
}
});
}
});
return translationMap;
};
/**
* Creates a PO parser object. If PO object is a string,
* UTF-8 will be used as the charset
*
* @constructor
* @param {Buffer|String} fileContents PO object
* @param {String} [defaultCharset] Default charset to use
*/
function Parser(fileContents, defaultCharset){
this._charset = defaultCharset || "iso-8859-1";
if(typeof fileContents == "string"){
this._charset = "utf-8";
this._fileContents = fileContents;
}else{
this._handleCharset(fileContents);
}
}
/**
* Detects charset for PO strings from the header
*
* @param {Buffer} headers Header value
*/
Parser.prototype._handleCharset = function(buf){
var str = (buf || "").toString(),
pos, headers = "", match;
if((pos = str.search(/^\s*msgid/im))>=0){
if((pos = pos + str.substr(pos + 5).search(/^\s*(msgid|msgctxt)/im))){
headers = str.substr(0, pos);
}
}
if((match = headers.match(/[; ]charset\s*=\s*([\w\-]+)(?:[\s;]|\\n)*"\s*$/mi))){
this._charset = sharedFuncs.formatCharset(match[1], this._charset);
}
if(this._charset == "utf-8"){
this._fileContents = str;
}else{
this._fileContents = encoding.convert(buf, "utf-8", this._charset).toString("utf-8");
}
};
/**
* State constants for parsing FSM
*/
Parser.prototype.states = {
none: 0x01,
comments: 0x02,
key: 0x03,
string: 0x04
};
/**
* Value types for lexer
*/
Parser.prototype.types = {
comments: 0x01,
key: 0x02,
string: 0x03
};
/**
* String matches for lexer
*/
Parser.prototype.symbols = {
quotes: /['"]/,
comments: /\#/,
whitespace: /\s/,
key: /[\w\-\[\]]/
};
/**
* Token parser
*
* @return {Object} Parsed tokens
*/
Parser.prototype._lexer = function(){
var chr,
escaped = false,
lex = [],
node,
state = this.states.none;
for(var i=0, len = this._fileContents.length; i<len; i++){
chr = this._fileContents.charAt(i);
switch(state){
case this.states.none:
if(chr.match(this.symbols.quotes)){
node = {
type: this.types.string,
value: "",
quote: chr
};
lex.push(node);
state = this.states.string;
}else if(chr.match(this.symbols.comments)){
node = {
type: this.types.comments,
value: ""
};
lex.push(node);
state = this.states.comments;
}else if(!chr.match(this.symbols.whitespace)){
node = {
type: this.types.key,
value: chr
};
lex.push(node);
state = this.states.key;
}
break;
case this.states.comments:
if(chr == "\n"){
state = this.states.none;
}else if(chr != "\r"){
node.value += chr;
}
break;
case this.states.string:
if(escaped){
switch(chr){
case "t": node.value += "\t"; break;
case "n": node.value += "\n"; break;
case "r": node.value += "\r"; break;
default:
node.value += chr;
}
escaped = false;
}else{
if(chr == node.quote){
state = this.states.none;
}else if(chr == "\\"){
escaped = true;
break;
}else{
node.value += chr;
}
escaped = false;
}
break;
case this.states.key:
if(!chr.match(this.symbols.key)){
state = this.states.none;
i--;
}else{
node.value += chr;
}
break;
}
}
return lex;
};
/**
* Join multi line strings
*
* @param {Object} lex Parsed tokens
* @return {Object} Parsed tokens, with multi line strings joined into one
*/
Parser.prototype._joinStringValues = function(lex){
var lastNode, response = [];
for(var i=0, len = lex.length; i<len; i++){
if(lastNode && lex[i].type == this.types.string && lastNode.type == this.types.string){
lastNode.value += lex[i].value;
}else if(lastNode && lex[i].type == this.types.comments && lastNode.type == this.types.comments){
lastNode.value += "\n" + lex[i].value;
}else{
response.push(lex[i]);
lastNode = lex[i];
}
}
return response;
};
/**
* Parse comments into separate comment blocks
*
* @param {Object} lex Parsed tokens
*/
Parser.prototype._parseComments = function(lex){
// parse comments
lex.forEach((function(node){
var comment, lines;
if(node && node.type == this.types.comments){
comment = {translator: [], extracted: [], reference: [], flag: [], previous: []};
lines = (node.value || "").split(/\n/);
lines.forEach(function(line){
switch(line.charAt(0) || ""){
case ":":
comment.reference.push(line.substr(1).trim());
break;
case ".":
comment.extracted.push(line.substr(1).replace(/^\s+/, ""));
break;
case ",":
comment.flag.push(line.substr(1).replace(/^\s+/, ""));
break;
case "|":
comment.previous.push(line.substr(1).replace(/^\s+/, ""));
break;
default:
comment.translator.push(line.replace(/^\s+/, ""));
}
});
node.value = {};
Object.keys(comment).forEach(function(key){
if(comment[key] && comment[key].length){
node.value[key] = comment[key].join("\n");
}
});
}
}).bind(this));
};
/**
* Join gettext keys with values
*
* @param {Object} lex Parsed tokens
* @return {Object} Tokens
*/
Parser.prototype._handleKeys = function(lex){
var response = [], lastNode;
for(var i=0, len = lex.length; i<len; i++){
if(lex[i].type == this.types.key){
lastNode = {
key: lex[i].value
};
if(i && lex[i-1].type == this.types.comments){
lastNode.comments = lex[i-1].value;
}
lastNode.value = "";
response.push(lastNode);
}else if(lex[i].type == this.types.string && lastNode){
lastNode.value += lex[i].value;
}
}
return response;
};
/**
* Separate different values into individual translation objects
*
* @param {Object} lex Parsed tokens
* @return {Object} Tokens
*/
Parser.prototype._handleValues = function(lex){
var response = [], lastNode, curContext, curComments;
for(var i=0, len = lex.length; i<len; i++){
if(lex[i].key.toLowerCase() == "msgctxt"){
curContext = lex[i].value;
curComments = lex[i].comments;
}else if(lex[i].key.toLowerCase() == "msgid"){
lastNode = {
msgid: lex[i].value
};
if(curContext){
lastNode.msgctxt = curContext;
}
if(curComments){
lastNode.comments = curComments;
}
if(lex[i].comments && !lastNode.comments){
lastNode.comments = lex[i].comments;
}
curContext = false;
curComments = false;
response.push(lastNode);
}else if(lex[i].key.toLowerCase() == "msgid_plural"){
if(lastNode){
lastNode.msgid_plural = lex[i].value;
}
if(lex[i].comments && !lastNode.comments){
lastNode.comments = lex[i].comments;
}
curContext = false;
curComments = false;
}else if(lex[i].key.substr(0, 6).toLowerCase() == "msgstr"){
if(lastNode){
lastNode.msgstr = (lastNode.msgstr || []).concat(lex[i].value);
}
if(lex[i].comments && !lastNode.comments){
lastNode.comments = lex[i].comments;
}
curContext = false;
curComments = false;
}
}
return response;
};
/**
* Compose a translation table from tokens object
*
* @param {Object} lex Parsed tokens
* @return {Object} Translation table
*/
Parser.prototype._normalize = function(lex){
var msgctxt,
table = {
charset: this._charset,
headers: undefined,
translations: {}
};
for(var i=0, len = lex.length; i < len; i++){
msgctxt = lex[i].msgctxt || "";
if(!table.translations[msgctxt]){
table.translations[msgctxt] = {};
}
if(!table.headers && !msgctxt && !lex[i].msgid){
table.headers = sharedFuncs.parseHeader(lex[i].msgstr[0]);
}
table.translations[msgctxt][lex[i].msgid] = lex[i];
}
return table;
};
/**
* Parses the PO object and returns translation table
*
* @return {Object} Translation table
*/
Parser.prototype.parse = function(){
var lex = this._lexer();
lex = this._joinStringValues(lex);
this._parseComments(lex);
lex = this._handleKeys(lex);
lex = this._handleValues(lex);
return this._normalize(lex);
};
var localizeFile = function (config, name) {
var localizedFilename = 'foo',
root;
try {
root = window;
} catch (e) {
root = config;
}
var locale = root.locale || root._lang,
_locale = locale,
_shortLocale = locale.split('_')[0];
if (typeof locale === 'function') {
_locale = locale(root);
}
localizedFilename = name.replace('{{locale}}', _locale);
return localizedFilename;
};
var parseOutLocale = function (text, options) {
options = options || {};
var localeFunc;
var metaMarker = 'Plural-Forms:';
var metaMarkerLength = metaMarker.length;
var pos = text.search(metaMarker);
var startFromText = text.substr(pos + metaMarkerLength);
var inlineMarker = 'plurals=';
var inlineMarkerLength = inlineMarker.length;
var inlinePos = inlineMarker.search(inlineMarker);
var startFromInlinedText = startFromText.substr(inlinePos + inlineMarkerLength);
var nextSemicolon = startFromInlinedText.indexOf(';');
startFromInlinedText = startFromInlinedText.substr(nextSemicolon + 1);
nextSemicolon = startFromInlinedText.search(';');
startFromInlinedText = startFromInlinedText.substr(0, nextSemicolon);
startFromInlinedText = startFromInlinedText.replace('plural=', '');
startFromInlinedText = startFromInlinedText.replace(/'/g, '"');
startFromInlinedText = startFromInlinedText.replace(/"\n"/g, ' ');
if (options.useDefine) {
localeFunc = 'define(function defineInternationalization_' + globalConfig.locale + '_Locale () {\n' +
' return function (n) { return ' + startFromInlinedText + '; }\n' +
'});';
} else {
localeFunc = '(function defineInternationalization_' + globalConfig.locale + '_Locale () {\n' +
' return function (n) { return ' + startFromInlinedText + '; }\n' +
'}());';
};
return localeFunc;
};
var globalConfig;
var localeEmitter;
//>>excludeEnd('excludePo')
var text, fs,
progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],
xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im,
bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im,
hasLocation = typeof location !== 'undefined' && location.href,
defaultProtocol = hasLocation && location.protocol && location.protocol.replace(/\:/, ''),
defaultHostName = hasLocation && location.hostname,
defaultPort = hasLocation && (location.port || undefined),
buildMap = [],
masterConfig = (module.config && module.config()) || {};
text = {
version: '0.1.0',
strip: function (content) {
//>>excludeStart('excludePo', pragmas.excludePo)
//Strips <?xml ...?> declarations so that external SVG and XML
//documents can be added to a document without worry. Also, if the string
//is an HTML document, only the part inside the body tag is returned.
if (content) {
content = content.replace(xmlRegExp, "");
var matches = content.match(bodyRegExp);
if (matches) {
content = matches[1];
}
} else {
content = "";
}
//>>excludeEnd('excludePo')
return content;
},
//>>excludeStart('excludePo', pragmas.excludePo)
jsEscape: function (content) {
return content.replace(/(['\\])/g, '\\$1')
.replace(/[\f]/g, "\\f")
.replace(/[\b]/g, "\\b")
.replace(/[\n]/g, "\\n")
.replace(/[\t]/g, "\\t")
.replace(/[\r]/g, "\\r")
.replace(/[\u2028]/g, "\\u2028")
.replace(/[\u2029]/g, "\\u2029");
},
createXhr: masterConfig.createXhr || function () {
//Would love to dump the ActiveX crap in here. Need IE 6 to die first.
var xhr, i, progId;
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else if (typeof ActiveXObject !== "undefined") {
for (i = 0; i < 3; i += 1) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
} catch (e) {}
if (xhr) {
progIds = [progId]; // so faster next time
break;
}
}
}
return xhr;
},
/**
* Parses a resource name into its component parts. Resource names
* look like: module/name.ext!strip, where the !strip part is
* optional.
* @param {String} name the resource name
* @returns {Object} with properties "moduleName", "ext" and "strip"
* where strip is a boolean.
*/
parseName: function (name) {
name += '.po';
var strip = false, index = name.lastIndexOf("."),
modName = name.substring(0, index),
ext = name.substring(index + 1, name.length);
index = ext.indexOf("!");
if (index !== -1) {
//Pull off the strip arg.
strip = ext.substring(index + 1, ext.length);
strip = strip === "strip";
ext = ext.substring(0, index);
}
if (ext === 'po.po') {
ext = 'po';
}
return {
moduleName: modName,
ext: ext,
strip: strip
};
},
xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/,
/**
* Is an URL on another domain. Only works for browser use, returns
* false in non-browser environments. Only used to know if an
* optimized .js version of a text resource should be loaded
* instead.
* @param {String} url
* @returns Boolean
*/
useXhr: function (url, protocol, hostname, port) {
var uProtocol, uHostName, uPort,
match = text.xdRegExp.exec(url);
if (!match) {
return true;
}
uProtocol = match[2];
uHostName = match[3];
uHostName = uHostName.split(':');
uPort = uHostName[1];
uHostName = uHostName[0];
return (!uProtocol || uProtocol === protocol) &&
(!uHostName || uHostName.toLowerCase() === hostname.toLowerCase()) &&
((!uPort && !uHostName) || uPort === port);
},
finishLoad: function (name, strip, content, onLoad) {
content = strip ? text.strip(content) : content;
if (masterConfig.isBuild) {
buildMap[name] = content;
}
onLoad(content);
},
load: function (name, req, onLoad, config) {
globalConfig = config;
//Name has format: some.module.filext!strip
//The strip part is optional.
//if strip is present, then that means only get the string contents
//inside a body tag in an HTML string. For XML/SVG content it means
//removing the <?xml ...?> declarations so the content can be inserted
//into the current doc without problems.
// Do not bother with the work if a build and text will
// not be inlined.
if (config.isBuild && !config.inlineText) {
onLoad();
return;
}
if (typeof config.locale === 'function' && !config.isBuild) {
var root;
try {
root = window;
} catch (e) {
root = {};
}
config.locale = config.locale(root);
}
masterConfig.isBuild = config.isBuild;
var parsed = text.parseName(name);
// use `i18nLocation` configuration as url default
var nonStripName = config.po.i18nLocation + '/' + config.locale + '/' + parsed.moduleName + '.' + parsed.ext;
// check if we need to modify the location of the file, this is the case if the locale placeholder has been set
if (parsed.moduleName.search('{{locale}}') !== -1) {
nonStripName = localizeFile(config, parsed.moduleName) + '.' + parsed.ext;
}
var url = req.toUrl(nonStripName);
var useXhr = (masterConfig.useXhr) || text.useXhr;
localeEmitter = req.toUrl(config.po.i18nLocation + '/' + '{{locale}}/locale.js');
//Load the text. Use XHR if possible and in a browser.
if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) {
text.get(url, function (content) {
text.finishLoad(name, parsed.strip, content, onLoad);
}, function (err) {
if (onLoad.error) {
onLoad.error(err);
}
});
} else {
//Need to fetch the resource across domains. Assume
//the resource has been optimized into a JS module. Fetch
//by the module name + extension, but do not include the
//!strip part to avoid file system issues.
req([nonStripName], function (content) {
text.finishLoad(parsed.moduleName + '.' + parsed.ext,
parsed.strip, content, onLoad);
});
}
},
write: function (pluginName, moduleName, write, config) {
if (buildMap.hasOwnProperty(moduleName)) {
var content = buildMap[moduleName];
write.asModule(pluginName + "!" + moduleName,
"define(function () {" +
content +
";});");
}
},
writeFile: function (pluginName, moduleName, req, write, config) {
var parsed = text.parseName(moduleName),
nonStripName = parsed.moduleName + '.' + parsed.ext,
//Use a '.js' file name so that it indicates it is a
//script that can be loaded across domains.
fileName = req.toUrl(parsed.moduleName + '.' +
parsed.ext) + '.js';
//Leverage own load() method to load plugin value, but only
//write out values that do not have the strip argument,
//to avoid any potential issues with ! in file names.
text.load(nonStripName, req, function (value) {
//Use own write() method to construct full module value.
//But need to create shell that translates writeFile's
//write() to the right interface.
var textWrite = function (contents) {
return write(fileName, contents);
};
textWrite.asModule = function (moduleName, contents) {
return write.asModule(moduleName, fileName, contents);
};
text.write(pluginName, nonStripName, textWrite, config);
}, config);
}
};
if (masterConfig.env === 'node' || (!masterConfig.env && typeof process !== "undefined" && process.versions && !!process.versions.node)) {
// Using special require.nodeRequire, something added by r.js.
fs = require.nodeRequire('fs');
text.get = function (url, callback) {
var file = fs.readFileSync(url, 'utf8');
// Remove BOM (Byte Mark Order) from utf8 files if it is there.
if (file.indexOf('\uFEFF') === 0) {
file = file.substring(1);
}
var mf;
if (globalConfig.po.useMessageformatPlurals === true) {
mf = new MessageFormat(globalConfig.locale);
} else {
var localeThingy = null;
var define = function (fn) {
localeThingy = fn;
};
// okay, this is so evil...
if (globalConfig.po.usePluralFromPo !== true) {
var loacalething = fs.readFileSync(localeEmitter.replace('{{locale}}', globalConfig.locale));
eval(String(loacalething));
} else if (file.search('Plural-Forms:') !== -1) {
// check for inlined localization plural forms
var loacalething = parseOutLocale(file, {useDefine: true});
eval(String(loacalething));
}
mf = new MessageFormat(globalConfig.locale, localeThingy);
}
var pluralFuncName = mf.runtime.pluralFuncs[globalConfig.locale].name || ('_defaultPluralFunc_' + globalConfig.locale);
var compiledMessageFormat = ['returnee = {};' + 'var ' + pluralFuncName + ' = ' + mf.runtime.pluralFuncs[globalConfig.locale].toString() + ';'];
compiledMessageFormat.push(" " + pluralFuncName + ".__masterGlobalVars = function (d) {"
+ " var globs = require.s.contexts._.config.po.globals || {};"
+ " d = d === Object(d) ? d : {};"
+ " Object.keys(globs).forEach(function (name) {"
+ " if (!d[name]) d[name] = globs[name];"
+ " });"
+ " return d;"
+ "};"
);
var translations = sharedFuncs.convert(file);
var funcs = mf.compile(translations).toString();
var start = funcs.indexOf('function anonymous() {') + 'function anonymous() {'.length;
var end = funcs.indexOf('return {');
compiledMessageFormat.push(funcs.substring(start, end));
Object.keys(translations).forEach(function(key){
var ostr = mf.compile(translations[key]);
var str = "function (d) { return " + ostr + "(" + pluralFuncName + ".__masterGlobalVars(d)); };";
var retString = 'returnee["' + key + '"] = ' + str.replace(/\\"/g, '\\"') + ';';
compiledMessageFormat.push(retString.replace(/\n/g, ' '));
});
compiledMessageFormat.push('return returnee');
callback(compiledMessageFormat.join(' '));
};
} else if (masterConfig.env === 'xhr' || (!masterConfig.env && text.createXhr())) {
text.get = function (url, callback, errback) {
var xhr = text.createXhr();
xhr.open('GET', url, true);
// Allow overrides specified in config
if (masterConfig.onXhr) {
masterConfig.onXhr(xhr, url);
}
xhr.onreadystatechange = function (evt) {
var status, err;
// Do not explicitly handle errors, those should be
// visible via console output in the browser.
if (xhr.readyState === 4) {
status = xhr.status;
if (status > 399 && status < 600) {
// An http 4xx or 5xx error. Signal an error.
err = new Error(url + ' HTTP status: ' + status);
err.xhr = xhr;
errback(err);
} else {
var createMessageFormat = function(locale) {
if (!MessageFormat.locale) MessageFormat.locale = {};
MessageFormat.locale[globalConfig.locale] = locale;
var mf = new MessageFormat(globalConfig.locale);
var returnee = {};
var translations = sharedFuncs.convert(xhr.responseText);
if (!require.i18n) {
require.i18n = {};
}
var fileName = url.split('/').pop().split('.')[0];
if (!require.i18n[fileName]) {
require.i18n[fileName] = {};
}
Object.keys(translations).forEach(function (key) {
var tmp = mf.compile(translations[key]);
require.i18n[fileName][key] = returnee[key] = function (d) {
var globs = globalConfig.po.globals || {};
d = typeof d === 'object' ? d : {};
Object.keys(globs).forEach(function (name) {
if (!d[name]) d[name] = globs[name];
});
return tmp(d);
};
});
callback(returnee);
};
if (globalConfig.po.useMessageformatPlurals === true) {
createMessageFormat(globalConfig.locale)
} else if (globalConfig.po.usePluralFromPo !== true) {
require([localeEmitter.replace('{{locale}}', globalConfig.locale)], function (locale) {
createMessageFormat(locale)
});
} else {
// check for inlined localization plural forms
var localeFunc;
if (xhr.responseText.search('Plural-Forms:') !== -1) {
localeFunc = parseOutLocale(xhr.responseText);
}
var locale = eval(localeFunc);
createMessageFormat(locale)
}
}
}
};
xhr.send(null);
};
//>>excludeEnd('excludePo')
}
return text;
});