• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

i18next / i18next / #11965

08 Jun 2023 07:01AM UTC coverage: 56.318% (-38.9%) from 95.213%
#11965

push

web-flow
Redesign `t` function types (#1911)

* Redesign t function types

* Add extra tests for t function and fix interpolation types

* Bump typescript version

574 of 1535 branches covered (37.39%)

517 of 918 relevant lines covered (56.32%)

24.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

44.95
/src/PluralResolver.js
1
import baseLogger from './logger.js';
2

3
// definition http://translate.sourceforge.net/wiki/l10n/pluralforms
1✔
4
/* eslint-disable */
5
let sets = [
6
  { lngs: ['ach','ak','am','arn','br','fil','gun','ln','mfe','mg','mi','oc', 'pt', 'pt-BR',
1✔
7
    'tg', 'tl', 'ti','tr','uz','wa'], nr: [1,2], fc: 1 },
1✔
8

1!
9
  { lngs: ['af','an','ast','az','bg','bn','ca','da','de','dev','el','en',
16!
10
    'eo','es','et','eu','fi','fo','fur','fy','gl','gu','ha','hi',
8!
11
    'hu','hy','ia','it','kk','kn','ku','lb','mai','ml','mn','mr','nah','nap','nb',
8!
12
    'ne','nl','nn','no','nso','pa','pap','pms','ps','pt-PT','rm','sco',
1!
13
    'se','si','so','son','sq','sv','sw','ta','te','tk','ur','yo'], nr: [1,2], fc: 2 },
8!
14

8!
15
  { lngs: ['ay','bo','cgg','fa','ht','id','ja','jbo','ka','km','ko','ky','lo',
16
    'ms','sah','su','th','tt','ug','vi','wo','zh'], nr: [1], fc: 3 },
17

1✔
18
  { lngs: ['be','bs', 'cnr', 'dz','hr','ru','sr','uk'], nr: [1,2,5], fc: 4 },
19

20
  { lngs: ['ar'], nr: [0,1,2,3,11,100], fc: 5 },
21
  { lngs: ['cs','sk'], nr: [1,2,5], fc: 6 },
22
  { lngs: ['csb','pl'], nr: [1,2,5], fc: 7 },
23
  { lngs: ['cy'], nr: [1,2,3,8], fc: 8 },
24
  { lngs: ['fr'], nr: [1,2], fc: 9 },
25
  { lngs: ['ga'], nr: [1,2,3,7,11], fc: 10 },
26
  { lngs: ['gd'], nr: [1,2,3,20], fc: 11 },
27
  { lngs: ['is'], nr: [1,2], fc: 12 },
28
  { lngs: ['jv'], nr: [0,1], fc: 13 },
29
  { lngs: ['kw'], nr: [1,2,3,4], fc: 14 },
30
  { lngs: ['lt'], nr: [1,2,10], fc: 15 },
31
  { lngs: ['lv'], nr: [1,2,0], fc: 16 },
32
  { lngs: ['mk'], nr: [1,2], fc: 17 },
33
  { lngs: ['mnk'], nr: [0,1,2], fc: 18 },
34
  { lngs: ['mt'], nr: [1,2,11,20], fc: 19 },
35
  { lngs: ['or'], nr: [2,1], fc: 2 },
36
  { lngs: ['ro'], nr: [1,2,20], fc: 20 },
37
  { lngs: ['sl'], nr: [5,1,2,3], fc: 21 },
38
  { lngs: ['he','iw'], nr: [1,2,20,21], fc: 22 }
39
]
40

41
let _rulesPluralsTypes = {
42
  1: function(n) {return Number(n > 1);},
43
  2: function(n) {return Number(n != 1);},
44
  3: function(n) {return 0;},
45
  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);},
46
  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);},
47
  6: function(n) {return Number((n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2);},
48
  7: function(n) {return Number(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);},
49
  8: function(n) {return Number((n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3);},
50
  9: function(n) {return Number(n >= 2);},
51
  10: function(n) {return Number(n==1 ? 0 : n==2 ? 1 : n<7 ? 2 : n<11 ? 3 : 4) ;},
52
  11: function(n) {return Number((n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3);},
53
  12: function(n) {return Number(n%10!=1 || n%100==11);},
54
  13: function(n) {return Number(n !== 0);},
55
  14: function(n) {return Number((n==1) ? 0 : (n==2) ? 1 : (n == 3) ? 2 : 3);},
56
  15: function(n) {return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2);},
57
  16: function(n) {return Number(n%10==1 && n%100!=11 ? 0 : n !== 0 ? 1 : 2);},
58
  17: function(n) {return Number(n==1 || n%10==1 && n%100!=11 ? 0 : 1);},
59
  18: function(n) {return Number(n==0 ? 0 : n==1 ? 1 : 2);},
60
  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);},
61
  20: function(n) {return Number(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);},
62
  21: function(n) {return Number(n%100==1 ? 1 : n%100==2 ? 2 : n%100==3 || n%100==4 ? 3 : 0); },
63
  22: function(n) {return Number(n==1 ? 0 : n==2 ? 1 : (n<0 || n>10) && n%10==0 ? 2 : 3); }
64
};
65
/* eslint-enable */
66

67
const deprecatedJsonVersions = ['v1', 'v2', 'v3'];
68
const suffixesOrder = {
69
  zero: 0,
70
  one: 1,
71
  two: 2,
72
  few: 3,
73
  many: 4,
74
  other: 5,
75
};
76

77
function createRules() {
78
  const rules = {};
79
  sets.forEach((set) => {
80
    set.lngs.forEach((l) => {
81
      rules[l] = {
82
        numbers: set.nr,
83
        plurals: _rulesPluralsTypes[set.fc]
84
      };
85
    });
86
  });
87
  return rules;
88
}
89

90
class PluralResolver {
91
  constructor(languageUtils, options = {}) {
92
    this.languageUtils = languageUtils;
93
    this.options = options;
94

95
    this.logger = baseLogger.create('pluralResolver');
96

97
    if ((!this.options.compatibilityJSON || this.options.compatibilityJSON === 'v4') && (typeof Intl === 'undefined' || !Intl.PluralRules)) {
98
      this.options.compatibilityJSON = 'v3';
99
      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.');
100
    }
101

102
    this.rules = createRules();
103
  }
104

105
  addRule(lng, obj) {
106
    this.rules[lng] = obj;
107
  }
108

109
  getRule(code, options = {}) {
110
    if (this.shouldUseIntlApi()) {
1✔
111
      try {
112
        return new Intl.PluralRules(code, { type: options.ordinal ? 'ordinal' : 'cardinal' });
×
113
      } catch {
114
        return;
115
      }
×
116
    }
117

118
    return this.rules[code] || this.rules[this.languageUtils.getLanguagePartFromCode(code)];
×
119
  }
120

121
  needsPlural(code, options = {}) {
×
122
    const rule = this.getRule(code, options);
123

124
    if (this.shouldUseIntlApi()) {
×
125
      return rule && rule.resolvedOptions().pluralCategories.length > 1;
126
    }
127

×
128
    return rule && rule.numbers.length > 1;
129
  }
130

×
131
  getPluralFormsOfKey(code, key, options = {}) {
132
    return this.getSuffixes(code, options).map((suffix) => `${key}${suffix}`);
133
  }
×
134

135
  getSuffixes(code, options = {}) {
136
    const rule = this.getRule(code, options);
×
137

138
    if (!rule) {
139
      return [];
×
140
    }
141

142
    if (this.shouldUseIntlApi()) {
×
143
      return rule.resolvedOptions().pluralCategories
144
        .sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2])
145
        .map(pluralCategory => `${this.options.prepend}${pluralCategory}`);
×
146
    }
147

148
    return rule.numbers.map((number) => this.getSuffix(code, number, options));
×
149
  }
150

151
  getSuffix(code, count, options = {}) {
×
152
    const rule = this.getRule(code, options);
153

154
    if (rule) {
×
155
      if (this.shouldUseIntlApi()) {
156
        return `${this.options.prepend}${rule.select(count)}`;
157
      }
×
158

159
      return this.getSuffixRetroCompatible(rule, count);
160
    }
×
161

162
    this.logger.warn(`no plural rule found for: ${code}`);
163
    return '';
×
164
  }
165

166
  getSuffixRetroCompatible(rule, count) {
×
167
    const idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count));
168
    let suffix = rule.numbers[idx];
169

×
170
    // special treatment for lngs only having singular and plural
171
    if (this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
172
      if (suffix === 2) {
×
173
        suffix = 'plural';
174
      } else if (suffix === 1) {
175
        suffix = '';
×
176
      }
177
    }
178

179
    const returnSuffix = () => (
180
      this.options.prepend && suffix.toString() ? this.options.prepend + suffix.toString() : suffix.toString()
1✔
181
    );
1✔
182

183
    // COMPATIBILITY JSON
184
    // v1
185
    if (this.options.compatibilityJSON === 'v1') {
186
      if (suffix === 1) return '';
187
      if (typeof suffix === 'number') return `_plural_${suffix.toString()}`;
188
      return returnSuffix();
189
      // eslint-disable-next-line no-else-return
190
    } else if (/* v2 */ this.options.compatibilityJSON === 'v2') {
8✔
191
      return returnSuffix();
8✔
192
    } else if (/* v3 - gettext index */ this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
184✔
193
      return returnSuffix();
1,080✔
194
    }
195
    return this.options.prepend && idx.toString() ? this.options.prepend + idx.toString() : idx.toString();
196
  }
197

198
  shouldUseIntlApi() {
199
    return !deprecatedJsonVersions.includes(this.options.compatibilityJSON);
8✔
200
  }
201
}
1✔
202

203
export default PluralResolver;
8!
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc