This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
l10n.js
1749 lines (1528 loc) · 43 KB
/
l10n.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
(function(window, undefined) {
'use strict';
/* jshint validthis:true */
function L10nError(message, id, loc) {
this.name = 'L10nError';
this.message = message;
this.id = id;
this.loc = loc;
}
L10nError.prototype = Object.create(Error.prototype);
L10nError.prototype.constructor = L10nError;
/* jshint browser:true */
var io = {
load: function load(url, callback, sync) {
var xhr = new XMLHttpRequest();
if (xhr.overrideMimeType) {
xhr.overrideMimeType('text/plain');
}
xhr.open('GET', url, !sync);
xhr.addEventListener('load', function io_load(e) {
if (e.target.status === 200 || e.target.status === 0) {
callback(null, e.target.responseText);
} else {
callback(new L10nError('Not found: ' + url));
}
});
xhr.addEventListener('error', callback);
xhr.addEventListener('timeout', callback);
// the app: protocol throws on 404, see https://bugzil.la/827243
try {
xhr.send(null);
} catch (e) {
callback(new L10nError('Not found: ' + url));
}
},
loadJSON: function loadJSON(url, callback) {
var xhr = new XMLHttpRequest();
if (xhr.overrideMimeType) {
xhr.overrideMimeType('application/json');
}
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.addEventListener('load', function io_loadjson(e) {
if (e.target.status === 200 || e.target.status === 0) {
callback(null, e.target.response);
} else {
callback(new L10nError('Not found: ' + url));
}
});
xhr.addEventListener('error', callback);
xhr.addEventListener('timeout', callback);
// the app: protocol throws on 404, see https://bugzil.la/827243
try {
xhr.send(null);
} catch (e) {
callback(new L10nError('Not found: ' + url));
}
}
};
function EventEmitter() {}
EventEmitter.prototype.emit = function ee_emit() {
if (!this._listeners) {
return;
}
var args = Array.prototype.slice.call(arguments);
var type = args.shift();
if (!this._listeners[type]) {
return;
}
var typeListeners = this._listeners[type].slice();
for (var i = 0; i < typeListeners.length; i++) {
typeListeners[i].apply(this, args);
}
};
EventEmitter.prototype.addEventListener = function ee_add(type, listener) {
if (!this._listeners) {
this._listeners = {};
}
if (!(type in this._listeners)) {
this._listeners[type] = [];
}
this._listeners[type].push(listener);
};
EventEmitter.prototype.removeEventListener = function ee_rm(type, listener) {
if (!this._listeners) {
return;
}
var typeListeners = this._listeners[type];
var pos = typeListeners.indexOf(listener);
if (pos === -1) {
return;
}
typeListeners.splice(pos, 1);
};
function getPluralRule(lang) {
var locales2rules = {
'af': 3,
'ak': 4,
'am': 4,
'ar': 1,
'asa': 3,
'az': 0,
'be': 11,
'bem': 3,
'bez': 3,
'bg': 3,
'bh': 4,
'bm': 0,
'bn': 3,
'bo': 0,
'br': 20,
'brx': 3,
'bs': 11,
'ca': 3,
'cgg': 3,
'chr': 3,
'cs': 12,
'cy': 17,
'da': 3,
'de': 3,
'dv': 3,
'dz': 0,
'ee': 3,
'el': 3,
'en': 3,
'eo': 3,
'es': 3,
'et': 3,
'eu': 3,
'fa': 0,
'ff': 5,
'fi': 3,
'fil': 4,
'fo': 3,
'fr': 5,
'fur': 3,
'fy': 3,
'ga': 8,
'gd': 24,
'gl': 3,
'gsw': 3,
'gu': 3,
'guw': 4,
'gv': 23,
'ha': 3,
'haw': 3,
'he': 2,
'hi': 4,
'hr': 11,
'hu': 0,
'id': 0,
'ig': 0,
'ii': 0,
'is': 3,
'it': 3,
'iu': 7,
'ja': 0,
'jmc': 3,
'jv': 0,
'ka': 0,
'kab': 5,
'kaj': 3,
'kcg': 3,
'kde': 0,
'kea': 0,
'kk': 3,
'kl': 3,
'km': 0,
'kn': 0,
'ko': 0,
'ksb': 3,
'ksh': 21,
'ku': 3,
'kw': 7,
'lag': 18,
'lb': 3,
'lg': 3,
'ln': 4,
'lo': 0,
'lt': 10,
'lv': 6,
'mas': 3,
'mg': 4,
'mk': 16,
'ml': 3,
'mn': 3,
'mo': 9,
'mr': 3,
'ms': 0,
'mt': 15,
'my': 0,
'nah': 3,
'naq': 7,
'nb': 3,
'nd': 3,
'ne': 3,
'nl': 3,
'nn': 3,
'no': 3,
'nr': 3,
'nso': 4,
'ny': 3,
'nyn': 3,
'om': 3,
'or': 3,
'pa': 3,
'pap': 3,
'pl': 13,
'ps': 3,
'pt': 3,
'rm': 3,
'ro': 9,
'rof': 3,
'ru': 11,
'rwk': 3,
'sah': 0,
'saq': 3,
'se': 7,
'seh': 3,
'ses': 0,
'sg': 0,
'sh': 11,
'shi': 19,
'sk': 12,
'sl': 14,
'sma': 7,
'smi': 7,
'smj': 7,
'smn': 7,
'sms': 7,
'sn': 3,
'so': 3,
'sq': 3,
'sr': 11,
'ss': 3,
'ssy': 3,
'st': 3,
'sv': 3,
'sw': 3,
'syr': 3,
'ta': 3,
'te': 3,
'teo': 3,
'th': 0,
'ti': 4,
'tig': 3,
'tk': 3,
'tl': 4,
'tn': 3,
'to': 0,
'tr': 0,
'ts': 3,
'tzm': 22,
'uk': 11,
'ur': 3,
've': 3,
'vi': 0,
'vun': 3,
'wa': 4,
'wae': 3,
'wo': 0,
'xh': 3,
'xog': 3,
'yo': 0,
'zh': 0,
'zu': 3
};
// utility functions for plural rules methods
function isIn(n, list) {
return list.indexOf(n) !== -1;
}
function isBetween(n, start, end) {
return typeof n === typeof start && start <= n && n <= end;
}
// list of all plural rules methods:
// map an integer to the plural form name to use
var pluralRules = {
'0': function() {
return 'other';
},
'1': function(n) {
if ((isBetween((n % 100), 3, 10))) {
return 'few';
}
if (n === 0) {
return 'zero';
}
if ((isBetween((n % 100), 11, 99))) {
return 'many';
}
if (n === 2) {
return 'two';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'2': function(n) {
if (n !== 0 && (n % 10) === 0) {
return 'many';
}
if (n === 2) {
return 'two';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'3': function(n) {
if (n === 1) {
return 'one';
}
return 'other';
},
'4': function(n) {
if ((isBetween(n, 0, 1))) {
return 'one';
}
return 'other';
},
'5': function(n) {
if ((isBetween(n, 0, 2)) && n !== 2) {
return 'one';
}
return 'other';
},
'6': function(n) {
if (n === 0) {
return 'zero';
}
if ((n % 10) === 1 && (n % 100) !== 11) {
return 'one';
}
return 'other';
},
'7': function(n) {
if (n === 2) {
return 'two';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'8': function(n) {
if ((isBetween(n, 3, 6))) {
return 'few';
}
if ((isBetween(n, 7, 10))) {
return 'many';
}
if (n === 2) {
return 'two';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'9': function(n) {
if (n === 0 || n !== 1 && (isBetween((n % 100), 1, 19))) {
return 'few';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'10': function(n) {
if ((isBetween((n % 10), 2, 9)) && !(isBetween((n % 100), 11, 19))) {
return 'few';
}
if ((n % 10) === 1 && !(isBetween((n % 100), 11, 19))) {
return 'one';
}
return 'other';
},
'11': function(n) {
if ((isBetween((n % 10), 2, 4)) && !(isBetween((n % 100), 12, 14))) {
return 'few';
}
if ((n % 10) === 0 ||
(isBetween((n % 10), 5, 9)) ||
(isBetween((n % 100), 11, 14))) {
return 'many';
}
if ((n % 10) === 1 && (n % 100) !== 11) {
return 'one';
}
return 'other';
},
'12': function(n) {
if ((isBetween(n, 2, 4))) {
return 'few';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'13': function(n) {
if ((isBetween((n % 10), 2, 4)) && !(isBetween((n % 100), 12, 14))) {
return 'few';
}
if (n !== 1 && (isBetween((n % 10), 0, 1)) ||
(isBetween((n % 10), 5, 9)) ||
(isBetween((n % 100), 12, 14))) {
return 'many';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'14': function(n) {
if ((isBetween((n % 100), 3, 4))) {
return 'few';
}
if ((n % 100) === 2) {
return 'two';
}
if ((n % 100) === 1) {
return 'one';
}
return 'other';
},
'15': function(n) {
if (n === 0 || (isBetween((n % 100), 2, 10))) {
return 'few';
}
if ((isBetween((n % 100), 11, 19))) {
return 'many';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'16': function(n) {
if ((n % 10) === 1 && n !== 11) {
return 'one';
}
return 'other';
},
'17': function(n) {
if (n === 3) {
return 'few';
}
if (n === 0) {
return 'zero';
}
if (n === 6) {
return 'many';
}
if (n === 2) {
return 'two';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'18': function(n) {
if (n === 0) {
return 'zero';
}
if ((isBetween(n, 0, 2)) && n !== 0 && n !== 2) {
return 'one';
}
return 'other';
},
'19': function(n) {
if ((isBetween(n, 2, 10))) {
return 'few';
}
if ((isBetween(n, 0, 1))) {
return 'one';
}
return 'other';
},
'20': function(n) {
if ((isBetween((n % 10), 3, 4) || ((n % 10) === 9)) && !(
isBetween((n % 100), 10, 19) ||
isBetween((n % 100), 70, 79) ||
isBetween((n % 100), 90, 99)
)) {
return 'few';
}
if ((n % 1000000) === 0 && n !== 0) {
return 'many';
}
if ((n % 10) === 2 && !isIn((n % 100), [12, 72, 92])) {
return 'two';
}
if ((n % 10) === 1 && !isIn((n % 100), [11, 71, 91])) {
return 'one';
}
return 'other';
},
'21': function(n) {
if (n === 0) {
return 'zero';
}
if (n === 1) {
return 'one';
}
return 'other';
},
'22': function(n) {
if ((isBetween(n, 0, 1)) || (isBetween(n, 11, 99))) {
return 'one';
}
return 'other';
},
'23': function(n) {
if ((isBetween((n % 10), 1, 2)) || (n % 20) === 0) {
return 'one';
}
return 'other';
},
'24': function(n) {
if ((isBetween(n, 3, 10) || isBetween(n, 13, 19))) {
return 'few';
}
if (isIn(n, [2, 12])) {
return 'two';
}
if (isIn(n, [1, 11])) {
return 'one';
}
return 'other';
}
};
// return a function that gives the plural form name for a given integer
var index = locales2rules[lang.replace(/-.*$/, '')];
if (!(index in pluralRules)) {
return function() { return 'other'; };
}
return pluralRules[index];
}
function PropertiesParser() {
var parsePatterns = {
comment: /^\s*#|^\s*$/,
entity: /^([^=\s]+)\s*=\s*(.+)$/,
multiline: /[^\\]\\$/,
macro: /\{\[\s*(\w+)\(([^\)]*)\)\s*\]\}/i,
unicode: /\\u([0-9a-fA-F]{1,4})/g,
entries: /[\r\n]+/,
controlChars: /\\([\\\n\r\t\b\f\{\}\"\'])/g
};
this.parse = function (ctx, source) {
var ast = Object.create(null);
var entries = source.split(parsePatterns.entries);
for (var i = 0; i < entries.length; i++) {
var line = entries[i];
if (parsePatterns.comment.test(line)) {
continue;
}
while (parsePatterns.multiline.test(line) && i < entries.length) {
line = line.slice(0, -1) + entries[++i].trim();
}
var entityMatch = line.match(parsePatterns.entity);
if (entityMatch) {
try {
parseEntity(entityMatch[1], entityMatch[2], ast);
} catch (e) {
if (ctx) {
ctx._emitter.emit('error', e);
} else {
throw e;
}
}
}
}
return ast;
};
function setEntityValue(id, attr, key, value, ast) {
var obj = ast;
var prop = id;
if (attr) {
if (!(id in obj)) {
obj[id] = {};
}
if (typeof(obj[id]) === 'string') {
obj[id] = {'_': obj[id]};
}
obj = obj[id];
prop = attr;
}
if (!key) {
obj[prop] = value;
return;
}
if (!(prop in obj)) {
obj[prop] = {'_': {}};
} else if (typeof(obj[prop]) === 'string') {
obj[prop] = {'_index': parseMacro(obj[prop]), '_': {}};
}
obj[prop]._[key] = value;
}
function parseEntity(id, value, ast) {
var name, key;
var pos = id.indexOf('[');
if (pos !== -1) {
name = id.substr(0, pos);
key = id.substring(pos + 1, id.length - 1);
} else {
name = id;
key = null;
}
var nameElements = name.split('.');
if (nameElements.length > 2) {
throw new Error('Error in ID: "' + name + '".' +
' Nested attributes are not supported.');
}
var attr;
if (nameElements.length > 1) {
name = nameElements[0];
attr = nameElements[1];
} else {
attr = null;
}
setEntityValue(name, attr, key, unescapeString(value), ast);
}
function unescapeControlCharacters(str) {
return str.replace(parsePatterns.controlChars, '$1');
}
function unescapeUnicode(str) {
return str.replace(parsePatterns.unicode, function(match, token) {
return unescape('%u' + '0000'.slice(token.length) + token);
});
}
function unescapeString(str) {
if (str.lastIndexOf('\\') !== -1) {
str = unescapeControlCharacters(str);
}
return unescapeUnicode(str);
}
function parseMacro(str) {
var match = str.match(parsePatterns.macro);
if (!match) {
throw new L10nError('Malformed macro');
}
return [match[1], match[2]];
}
}
var MAX_PLACEABLE_LENGTH = 2500;
var MAX_PLACEABLES = 100;
var rePlaceables = /\{\{\s*(.+?)\s*\}\}/g;
function Entity(id, node, env) {
this.id = id;
this.env = env;
// the dirty guard prevents cyclic or recursive references from other
// Entities; see Entity.prototype.resolve
this.dirty = false;
if (typeof node === 'string') {
this.value = node;
} else {
// it's either a hash or it has attrs, or both
var keys = Object.keys(node);
/* jshint -W084 */
for (var i = 0, key; key = keys[i]; i++) {
if (key[0] !== '_') {
if (!this.attributes) {
this.attributes = Object.create(null);
}
this.attributes[key] = new Entity(this.id + '.' + key, node[key],
env);
}
}
this.value = node._ || null;
this.index = node._index;
}
}
Entity.prototype.resolve = function E_resolve(ctxdata) {
if (this.dirty) {
return undefined;
}
this.dirty = true;
var val;
// if resolve fails, we want the exception to bubble up and stop the whole
// resolving process; however, we still need to clean up the dirty flag
try {
val = resolve(ctxdata, this.env, this.value, this.index);
} finally {
this.dirty = false;
}
return val;
};
Entity.prototype.toString = function E_toString(ctxdata) {
try {
return this.resolve(ctxdata);
} catch (e) {
return undefined;
}
};
Entity.prototype.valueOf = function E_valueOf(ctxdata) {
if (!this.attributes) {
return this.toString(ctxdata);
}
var entity = {
value: this.toString(ctxdata),
attributes: Object.create(null)
};
for (var key in this.attributes) {
/* jshint -W089 */
entity.attributes[key] = this.attributes[key].toString(ctxdata);
}
return entity;
};
function subPlaceable(ctxdata, env, match, id) {
if (ctxdata && ctxdata.hasOwnProperty(id) &&
(typeof ctxdata[id] === 'string' ||
(typeof ctxdata[id] === 'number' && !isNaN(ctxdata[id])))) {
return ctxdata[id];
}
// XXX: special case for Node.js where still:
// '__proto__' in Object.create(null) => true
if (id in env && id !== '__proto__') {
if (!(env[id] instanceof Entity)) {
env[id] = new Entity(id, env[id], env);
}
var value = env[id].resolve(ctxdata);
if (typeof value === 'string') {
// prevent Billion Laughs attacks
if (value.length >= MAX_PLACEABLE_LENGTH) {
throw new L10nError('Too many characters in placeable (' +
value.length + ', max allowed is ' +
MAX_PLACEABLE_LENGTH + ')');
}
return value;
}
}
return match;
}
function interpolate(ctxdata, env, str) {
var placeablesCount = 0;
var value = str.replace(rePlaceables, function(match, id) {
// prevent Quadratic Blowup attacks
if (placeablesCount++ >= MAX_PLACEABLES) {
throw new L10nError('Too many placeables (' + placeablesCount +
', max allowed is ' + MAX_PLACEABLES + ')');
}
return subPlaceable(ctxdata, env, match, id);
});
placeablesCount = 0;
return value;
}
function resolve(ctxdata, env, expr, index) {
if (typeof expr === 'string') {
return interpolate(ctxdata, env, expr);
}
if (typeof expr === 'boolean' ||
typeof expr === 'number' ||
!expr) {
return expr;
}
// otherwise, it's a dict
if (index && ctxdata && ctxdata.hasOwnProperty(index[1])) {
var argValue = ctxdata[index[1]];
// special cases for zero, one, two if they are defined on the hash
if (argValue === 0 && 'zero' in expr) {
return resolve(ctxdata, env, expr.zero);
}
if (argValue === 1 && 'one' in expr) {
return resolve(ctxdata, env, expr.one);
}
if (argValue === 2 && 'two' in expr) {
return resolve(ctxdata, env, expr.two);
}
var selector = env.__plural(argValue);
if (expr.hasOwnProperty(selector)) {
return resolve(ctxdata, env, expr[selector]);
}
}
// if there was no index or no selector was found, try 'other'
if ('other' in expr) {
return resolve(ctxdata, env, expr.other);
}
return undefined;
}
function compile(env, ast) {
/* jshint -W089 */
env = env || Object.create(null);
for (var id in ast) {
env[id] = new Entity(id, ast[id], env);
}
return env;
}
/* Utility functions */
// Recursively walk an AST node searching for content leaves
function walkContent(node, fn) {
if (typeof node === 'string') {
return fn(node);
}
var rv = {};
for (var key in node) {
if (key !== '_index' && (key in node)) {
rv[key] = walkContent(node[key], fn);
}
}
return rv;
}
/* Pseudolocalizations
*
* PSEUDO_STRATEGIES is a dict of strategies to be used to modify the English
* context in order to create pseudolocalizations. These can be used by
* developers to test the localizability of their code without having to
* actually speak a foreign language.
*
* Currently, the following pseudolocales are supported:
*
* qps-ploc - Ȧȧƈƈḗḗƞŧḗḗḓ Ḗḗƞɠŀīīşħ
*
* In Accented English all English letters are replaced by accented
* Unicode counterparts which don't impair the readability of the content.
* This allows developers to quickly test if any given string is being
* correctly displayed in its 'translated' form. Additionally, simple
* heuristics are used to make certain words longer to better simulate the
* experience of international users.
*
* qps-plocm - ɥsıʅƃuƎ pǝɹoɹɹıW
*
* Mirrored English is a fake RTL locale. All words are surrounded by
* Unicode formatting marks forcing the RTL directionality of characters.
* In addition, to make the reversed text easier to read, individual
* letters are flipped.
*
* Note: The name above is hardcoded to be RTL in case code editors have
* trouble with the RLO and PDF Unicode marks. In reality, it should be
* surrounded by those marks as well.
*
* See https://bugzil.la/900182 for more information.
*
*/
var reAlphas = /[a-zA-Z]/g;
var reVowels = /[aeiouAEIOU]/g;
// ȦƁƇḒḖƑƓĦĪĴĶĿḾȠǾƤɊŘŞŦŬṼẆẊẎẐ + [\\]^_` + ȧƀƈḓḗƒɠħīĵķŀḿƞǿƥɋřşŧŭṽẇẋẏẑ
var ACCENTED_MAP = '\u0226\u0181\u0187\u1E12\u1E16\u0191\u0193\u0126\u012A' +
'\u0134\u0136\u013F\u1E3E\u0220\u01FE\u01A4\u024A\u0158' +
'\u015E\u0166\u016C\u1E7C\u1E86\u1E8A\u1E8E\u1E90' +
'[\\]^_`' +
'\u0227\u0180\u0188\u1E13\u1E17\u0192\u0260\u0127\u012B' +
'\u0135\u0137\u0140\u1E3F\u019E\u01FF\u01A5\u024B\u0159' +
'\u015F\u0167\u016D\u1E7D\u1E87\u1E8B\u1E8F\u1E91';
// XXX Until https://bugzil.la/1007340 is fixed, ᗡℲ⅁⅂⅄ don't render correctly
// on the devices. For now, use the following replacements: pɟפ˥ʎ
// ∀ԐↃpƎɟפHIſӼ˥WNOԀÒᴚS⊥∩ɅMXʎZ + [\\]ᵥ_, + ɐqɔpǝɟƃɥıɾʞʅɯuodbɹsʇnʌʍxʎz
var FLIPPED_MAP = '\u2200\u0510\u2183p\u018E\u025F\u05E4HI\u017F' +
'\u04FC\u02E5WNO\u0500\xD2\u1D1AS\u22A5\u2229\u0245' +
'\uFF2DX\u028EZ' +
'[\\]\u1D65_,' +
'\u0250q\u0254p\u01DD\u025F\u0183\u0265\u0131\u027E' +
'\u029E\u0285\u026Fuodb\u0279s\u0287n\u028C\u028Dx\u028Ez';
function makeLonger(val) {
return val.replace(reVowels, function(match) {
return match + match.toLowerCase();
});
}
function makeAccented(map, val) {
// Replace each Latin letter with a Unicode character from map
return val.replace(reAlphas, function(match) {
return map.charAt(match.charCodeAt(0) - 65);
});
}
var reWords = /[^\W0-9_]+/g;
function makeRTL(val) {
// Surround each word with Unicode formatting codes, RLO and PDF:
// U+202E: RIGHT-TO-LEFT OVERRIDE (RLO)
// U+202C: POP DIRECTIONAL FORMATTING (PDF)
// See http://www.w3.org/International/questions/qa-bidi-controls
return val.replace(reWords, function(match) {
return '\u202e' + match + '\u202c';
});
}
// strftime tokens (%a, %Eb), {{ placeables }} and template {vars}
var reExcluded = /(%[EO]?\w|\{\{?\s*.+?\s*\}?\})/;
function mapContent(fn, val) {
if (!val) {
return val;
}
var parts = val.split(reExcluded);
var modified = parts.map(function(part) {
if (reExcluded.test(part)) {
return part;
}
return fn(part);
});
return modified.join('');
}
function Pseudo(id, name, charMap, modFn) {
this.id = id;
this.translate = mapContent.bind(null, function(val) {
return makeAccented(charMap, modFn(val));
});
this.name = this.translate(name);
}
var PSEUDO_STRATEGIES = {
'qps-ploc': new Pseudo('qps-ploc', 'Accented English',
ACCENTED_MAP, makeLonger),
'qps-plocm': new Pseudo('qps-plocm', 'Mirrored English',
FLIPPED_MAP, makeRTL)
};
var propertiesParser = null;