-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
PluralResolver.js
205 lines (172 loc) · 7.54 KB
/
PluralResolver.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
import baseLogger from './logger.js';
import { getCleanedCode } from './utils.js'
// definition http://translate.sourceforge.net/wiki/l10n/pluralforms
/* eslint-disable */
let sets = [
{ lngs: ['ach','ak','am','arn','br','fil','gun','ln','mfe','mg','mi','oc', 'pt', 'pt-BR',
'tg', 'tl', 'ti','tr','uz','wa'], nr: [1,2], fc: 1 },
{ lngs: ['af','an','ast','az','bg','bn','ca','da','de','dev','el','en',
'eo','es','et','eu','fi','fo','fur','fy','gl','gu','ha','hi',
'hu','hy','ia','it','kk','kn','ku','lb','mai','ml','mn','mr','nah','nap','nb',
'ne','nl','nn','no','nso','pa','pap','pms','ps','pt-PT','rm','sco',
'se','si','so','son','sq','sv','sw','ta','te','tk','ur','yo'], nr: [1,2], fc: 2 },
{ lngs: ['ay','bo','cgg','fa','ht','id','ja','jbo','ka','km','ko','ky','lo',
'ms','sah','su','th','tt','ug','vi','wo','zh'], nr: [1], fc: 3 },
{ lngs: ['be','bs', 'cnr', 'dz','hr','ru','sr','uk'], nr: [1,2,5], fc: 4 },
{ lngs: ['ar'], nr: [0,1,2,3,11,100], fc: 5 },
{ lngs: ['cs','sk'], nr: [1,2,5], fc: 6 },
{ lngs: ['csb','pl'], nr: [1,2,5], fc: 7 },
{ lngs: ['cy'], nr: [1,2,3,8], fc: 8 },
{ lngs: ['fr'], nr: [1,2], fc: 9 },
{ lngs: ['ga'], nr: [1,2,3,7,11], fc: 10 },
{ lngs: ['gd'], nr: [1,2,3,20], fc: 11 },
{ lngs: ['is'], nr: [1,2], fc: 12 },
{ lngs: ['jv'], nr: [0,1], fc: 13 },
{ lngs: ['kw'], nr: [1,2,3,4], fc: 14 },
{ lngs: ['lt'], nr: [1,2,10], fc: 15 },
{ lngs: ['lv'], nr: [1,2,0], fc: 16 },
{ lngs: ['mk'], nr: [1,2], fc: 17 },
{ lngs: ['mnk'], nr: [0,1,2], fc: 18 },
{ lngs: ['mt'], nr: [1,2,11,20], fc: 19 },
{ lngs: ['or'], nr: [2,1], fc: 2 },
{ lngs: ['ro'], nr: [1,2,20], fc: 20 },
{ lngs: ['sl'], nr: [5,1,2,3], fc: 21 },
{ lngs: ['he','iw'], nr: [1,2,20,21], fc: 22 }
]
let _rulesPluralsTypes = {
1: function(n) {return Number(n > 1);},
2: function(n) {return Number(n != 1);},
3: function(n) {return 0;},
4: function(n) {return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);},
5: function(n) {return Number(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5);},
6: function(n) {return Number((n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2);},
7: function(n) {return Number(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);},
8: function(n) {return Number((n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3);},
9: function(n) {return Number(n >= 2);},
10: function(n) {return Number(n==1 ? 0 : n==2 ? 1 : n<7 ? 2 : n<11 ? 3 : 4) ;},
11: function(n) {return Number((n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3);},
12: function(n) {return Number(n%10!=1 || n%100==11);},
13: function(n) {return Number(n !== 0);},
14: function(n) {return Number((n==1) ? 0 : (n==2) ? 1 : (n == 3) ? 2 : 3);},
15: function(n) {return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2);},
16: function(n) {return Number(n%10==1 && n%100!=11 ? 0 : n !== 0 ? 1 : 2);},
17: function(n) {return Number(n==1 || n%10==1 && n%100!=11 ? 0 : 1);},
18: function(n) {return Number(n==0 ? 0 : n==1 ? 1 : 2);},
19: function(n) {return Number(n==1 ? 0 : n==0 || ( n%100>1 && n%100<11) ? 1 : (n%100>10 && n%100<20 ) ? 2 : 3);},
20: function(n) {return Number(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);},
21: function(n) {return Number(n%100==1 ? 1 : n%100==2 ? 2 : n%100==3 || n%100==4 ? 3 : 0); },
22: function(n) {return Number(n==1 ? 0 : n==2 ? 1 : (n<0 || n>10) && n%10==0 ? 2 : 3); }
};
/* eslint-enable */
const nonIntlVersions = ['v1', 'v2', 'v3'];
const intlVersions = ['v4'];
const suffixesOrder = {
zero: 0,
one: 1,
two: 2,
few: 3,
many: 4,
other: 5,
};
function createRules() {
const rules = {};
sets.forEach((set) => {
set.lngs.forEach((l) => {
rules[l] = {
numbers: set.nr,
plurals: _rulesPluralsTypes[set.fc]
};
});
});
return rules;
}
class PluralResolver {
constructor(languageUtils, options = {}) {
this.languageUtils = languageUtils;
this.options = options;
this.logger = baseLogger.create('pluralResolver');
if ((!this.options.compatibilityJSON || intlVersions.includes(this.options.compatibilityJSON)) && (typeof Intl === 'undefined' || !Intl.PluralRules)) {
this.options.compatibilityJSON = 'v3';
this.logger.error('Your environment seems not to be Intl API compatible, use an Intl.PluralRules polyfill. Will fallback to the compatibilityJSON v3 format handling.');
}
this.rules = createRules();
}
addRule(lng, obj) {
this.rules[lng] = obj;
}
getRule(code, options = {}) {
if (this.shouldUseIntlApi()) {
try {
return new Intl.PluralRules(getCleanedCode(code === 'dev' ? 'en' : code), { type: options.ordinal ? 'ordinal' : 'cardinal' });
} catch (err) {
return;
}
}
return this.rules[code] || this.rules[this.languageUtils.getLanguagePartFromCode(code)];
}
needsPlural(code, options = {}) {
const rule = this.getRule(code, options);
if (this.shouldUseIntlApi()) {
return rule && rule.resolvedOptions().pluralCategories.length > 1;
}
return rule && rule.numbers.length > 1;
}
getPluralFormsOfKey(code, key, options = {}) {
return this.getSuffixes(code, options).map((suffix) => `${key}${suffix}`);
}
getSuffixes(code, options = {}) {
const rule = this.getRule(code, options);
if (!rule) {
return [];
}
if (this.shouldUseIntlApi()) {
return rule.resolvedOptions().pluralCategories
.sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2])
.map(pluralCategory => `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${pluralCategory}`);
}
return rule.numbers.map((number) => this.getSuffix(code, number, options));
}
getSuffix(code, count, options = {}) {
const rule = this.getRule(code, options);
if (rule) {
if (this.shouldUseIntlApi()) {
return `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${rule.select(count)}`;
}
return this.getSuffixRetroCompatible(rule, count);
}
this.logger.warn(`no plural rule found for: ${code}`);
return '';
}
getSuffixRetroCompatible(rule, count) {
const idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count));
let suffix = rule.numbers[idx];
// special treatment for lngs only having singular and plural
if (this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
if (suffix === 2) {
suffix = 'plural';
} else if (suffix === 1) {
suffix = '';
}
}
const returnSuffix = () => (
this.options.prepend && suffix.toString() ? this.options.prepend + suffix.toString() : suffix.toString()
);
// COMPATIBILITY JSON
// v1
if (this.options.compatibilityJSON === 'v1') {
if (suffix === 1) return '';
if (typeof suffix === 'number') return `_plural_${suffix.toString()}`;
return returnSuffix();
// eslint-disable-next-line no-else-return
} else if (/* v2 */ this.options.compatibilityJSON === 'v2') {
return returnSuffix();
} else if (/* v3 - gettext index */ this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
return returnSuffix();
}
return this.options.prepend && idx.toString() ? this.options.prepend + idx.toString() : idx.toString();
}
shouldUseIntlApi() {
return !nonIntlVersions.includes(this.options.compatibilityJSON);
}
}
export default PluralResolver;