UNPKG

30.5 kBJavaScriptView Raw
1'use strict';
2
3var Polyglot = require('../');
4var expect = require('chai').expect;
5var forEach = require('array.prototype.foreach');
6
7describe('t', function () {
8 var phrases = {
9 hello: 'Hello',
10 hi_name_welcome_to_place: 'Hi, %{name}, welcome to %{place}!',
11 name_your_name_is_name: '%{name}, your name is %{name}!',
12 empty_string: ''
13 };
14
15 var polyglot;
16 beforeEach(function () {
17 polyglot = new Polyglot({ phrases: phrases });
18 });
19
20 it('translates a simple string', function () {
21 expect(polyglot.t('hello')).to.equal('Hello');
22 });
23
24 it('returns the key if translation not found', function () {
25 expect(polyglot.t('bogus_key')).to.equal('bogus_key');
26 });
27
28 it('interpolates', function () {
29 expect(polyglot.t('hi_name_welcome_to_place', {
30 name: 'Spike',
31 place: 'the webz'
32 })).to.equal('Hi, Spike, welcome to the webz!');
33 });
34
35 it('interpolates with missing substitutions', function () {
36 expect(polyglot.t('hi_name_welcome_to_place', {
37 place: undefined
38 })).to.equal('Hi, %{name}, welcome to %{place}!');
39 });
40
41 it('interpolates the same placeholder multiple times', function () {
42 expect(polyglot.t('name_your_name_is_name', {
43 name: 'Spike'
44 })).to.equal('Spike, your name is Spike!');
45 });
46
47 it('allows you to supply default values', function () {
48 expect(polyglot.t('can_i_call_you_name', {
49 _: 'Can I call you %{name}?',
50 name: 'Robert'
51 })).to.equal('Can I call you Robert?');
52 });
53
54 it('returns the non-interpolated key if not initialized with allowMissing and translation not found', function () {
55 expect(polyglot.t('Welcome %{name}', {
56 name: 'Robert'
57 })).to.equal('Welcome %{name}');
58 });
59
60 it('returns an interpolated key if initialized with allowMissing and translation not found', function () {
61 var instance = new Polyglot({ phrases: phrases, allowMissing: true });
62 expect(instance.t('Welcome %{name}', {
63 name: 'Robert'
64 })).to.equal('Welcome Robert');
65 });
66
67 describe('custom interpolation syntax', function () {
68 var createWithInterpolation = function (interpolation) {
69 return new Polyglot({ phrases: {}, allowMissing: true, interpolation: interpolation });
70 };
71
72 it('interpolates with the specified custom token syntax', function () {
73 var instance = createWithInterpolation({ prefix: '{{', suffix: '}}' });
74 expect(instance.t('Welcome {{name}}', {
75 name: 'Robert'
76 })).to.equal('Welcome Robert');
77 });
78
79 it('interpolates if the prefix and suffix are the same', function () {
80 var instance = createWithInterpolation({ prefix: '|', suffix: '|' });
81 expect(instance.t('Welcome |name|, how are you, |name|?', {
82 name: 'Robert'
83 })).to.equal('Welcome Robert, how are you, Robert?');
84 });
85
86 it('interpolates when using regular expression tokens', function () {
87 var instance = createWithInterpolation({ prefix: '\\s.*', suffix: '\\d.+' });
88 expect(instance.t('Welcome \\s.*name\\d.+', {
89 name: 'Robert'
90 })).to.equal('Welcome Robert');
91 });
92
93 it('throws an error when either prefix or suffix equals to pluralization delimiter', function () {
94 expect(function () { createWithInterpolation({ prefix: '||||', suffix: '}}' }); }).to.throw(RangeError);
95 expect(function () { createWithInterpolation({ prefix: '{{', suffix: '||||' }); }).to.throw(RangeError);
96 });
97 });
98
99 it('returns the translation even if it is an empty string', function () {
100 expect(polyglot.t('empty_string')).to.equal('');
101 });
102
103 it('returns the default value even if it is an empty string', function () {
104 expect(polyglot.t('bogus_key', { _: '' })).to.equal('');
105 });
106
107 it('handles dollar signs in the substitution value', function () {
108 expect(polyglot.t('hi_name_welcome_to_place', {
109 name: '$abc $0',
110 place: '$1 $&'
111 })).to.equal('Hi, $abc $0, welcome to $1 $&!');
112 });
113
114 it('supports nested phrase objects', function () {
115 var nestedPhrases = {
116 nav: {
117 presentations: 'Presentations',
118 hi_user: 'Hi, %{user}.',
119 cta: {
120 join_now: 'Join now!'
121 }
122 },
123 'header.sign_in': 'Sign In'
124 };
125 var instance = new Polyglot({ phrases: nestedPhrases });
126 expect(instance.t('nav.presentations')).to.equal('Presentations');
127 expect(instance.t('nav.hi_user', { user: 'Raph' })).to.equal('Hi, Raph.');
128 expect(instance.t('nav.cta.join_now')).to.equal('Join now!');
129 expect(instance.t('header.sign_in')).to.equal('Sign In');
130 });
131
132 describe('onMissingKey', function () {
133 it('calls the function when a key is missing', function () {
134 var expectedKey = 'some key';
135 var expectedOptions = {};
136 var expectedLocale = 'oz';
137 var returnValue = {};
138 var onMissingKey = function (key, options, locale) {
139 expect(key).to.equal(expectedKey);
140 expect(options).to.equal(expectedOptions);
141 expect(locale).to.equal(expectedLocale);
142 return returnValue;
143 };
144 var instance = new Polyglot({ onMissingKey: onMissingKey, locale: expectedLocale });
145 var result = instance.t(expectedKey, expectedOptions);
146 expect(result).to.equal(returnValue);
147 });
148
149 it('overrides allowMissing', function (done) {
150 var missingKey = 'missing key';
151 var onMissingKey = function (key) {
152 expect(key).to.equal(missingKey);
153 done();
154 };
155 var instance = new Polyglot({ onMissingKey: onMissingKey, allowMissing: true });
156 instance.t(missingKey);
157 });
158 });
159});
160
161describe('pluralize', function () {
162 var phrases = {
163 count_name: '%{smart_count} Name |||| %{smart_count} Names'
164 };
165
166 var polyglot;
167 beforeEach(function () {
168 polyglot = new Polyglot({ phrases: phrases, locale: 'en' });
169 });
170
171 it('supports pluralization with an integer', function () {
172 expect(polyglot.t('count_name', { smart_count: 0 })).to.equal('0 Names');
173 expect(polyglot.t('count_name', { smart_count: 1 })).to.equal('1 Name');
174 expect(polyglot.t('count_name', { smart_count: 2 })).to.equal('2 Names');
175 expect(polyglot.t('count_name', { smart_count: 3 })).to.equal('3 Names');
176 });
177
178 it('accepts a number as a shortcut to pluralize a word', function () {
179 expect(polyglot.t('count_name', 0)).to.equal('0 Names');
180 expect(polyglot.t('count_name', 1)).to.equal('1 Name');
181 expect(polyglot.t('count_name', 2)).to.equal('2 Names');
182 expect(polyglot.t('count_name', 3)).to.equal('3 Names');
183 });
184
185 it('ignores a region subtag when choosing a pluralization rule', function () {
186 var instance = new Polyglot({ phrases: phrases, locale: 'fr-FR' });
187 // French rule: "0" is singular
188 expect(instance.t('count_name', 0)).to.equal('0 Name');
189 });
190});
191
192describe('locale-specific pluralization rules', function () {
193 it('pluralizes in Arabic', function () {
194 // English would be: "1 vote" / "%{smart_count} votes"
195 var whatSomeoneTranslated = [
196 'ولا صوت',
197 'صوت واحد',
198 'صوتان',
199 '%{smart_count} أصوات',
200 '%{smart_count} صوت',
201 '%{smart_count} صوت'
202 ];
203 var phrases = {
204 n_votes: whatSomeoneTranslated.join(' |||| ')
205 };
206
207 var polyglot = new Polyglot({ phrases: phrases, locale: 'ar' });
208
209 expect(polyglot.t('n_votes', 0)).to.equal('ولا صوت');
210 expect(polyglot.t('n_votes', 1)).to.equal('صوت واحد');
211 expect(polyglot.t('n_votes', 2)).to.equal('صوتان');
212 expect(polyglot.t('n_votes', 3)).to.equal('3 أصوات');
213 expect(polyglot.t('n_votes', 11)).to.equal('11 صوت');
214 expect(polyglot.t('n_votes', 102)).to.equal('102 صوت');
215 });
216
217 it('pluralizes in Russian', function () {
218 // English would be: "1 vote" / "%{smart_count} votes"
219 var whatSomeoneTranslated = [
220 '%{smart_count} машина',
221 '%{smart_count} машины',
222 '%{smart_count} машин'
223 ];
224 var phrases = {
225 n_votes: whatSomeoneTranslated.join(' |||| ')
226 };
227
228 var polyglotLanguageCode = new Polyglot({ phrases: phrases, locale: 'ru' });
229
230 expect(polyglotLanguageCode.t('n_votes', 1)).to.equal('1 машина');
231 expect(polyglotLanguageCode.t('n_votes', 11)).to.equal('11 машин');
232 expect(polyglotLanguageCode.t('n_votes', 101)).to.equal('101 машина');
233 expect(polyglotLanguageCode.t('n_votes', 112)).to.equal('112 машин');
234 expect(polyglotLanguageCode.t('n_votes', 932)).to.equal('932 машины');
235 expect(polyglotLanguageCode.t('n_votes', 324)).to.equal('324 машины');
236 expect(polyglotLanguageCode.t('n_votes', 12)).to.equal('12 машин');
237 expect(polyglotLanguageCode.t('n_votes', 13)).to.equal('13 машин');
238 expect(polyglotLanguageCode.t('n_votes', 14)).to.equal('14 машин');
239 expect(polyglotLanguageCode.t('n_votes', 15)).to.equal('15 машин');
240
241 var polyglotLocaleId = new Polyglot({ phrases: phrases, locale: 'ru-RU' });
242
243 expect(polyglotLocaleId.t('n_votes', 1)).to.equal('1 машина');
244 expect(polyglotLocaleId.t('n_votes', 11)).to.equal('11 машин');
245 expect(polyglotLocaleId.t('n_votes', 101)).to.equal('101 машина');
246 expect(polyglotLocaleId.t('n_votes', 112)).to.equal('112 машин');
247 expect(polyglotLocaleId.t('n_votes', 932)).to.equal('932 машины');
248 expect(polyglotLocaleId.t('n_votes', 324)).to.equal('324 машины');
249 expect(polyglotLocaleId.t('n_votes', 12)).to.equal('12 машин');
250 expect(polyglotLocaleId.t('n_votes', 13)).to.equal('13 машин');
251 expect(polyglotLocaleId.t('n_votes', 14)).to.equal('14 машин');
252 expect(polyglotLocaleId.t('n_votes', 15)).to.equal('15 машин');
253 });
254
255 it('pluralizes in Croatian (guest) Test', function () {
256 // English would be: "1 vote" / "%{smart_count} votes"
257 var whatSomeoneTranslated = [
258 '%{smart_count} gost',
259 '%{smart_count} gosta',
260 '%{smart_count} gostiju'
261 ];
262 var phrases = {
263 n_guests: whatSomeoneTranslated.join(' |||| ')
264 };
265
266 var polyglotLocale = new Polyglot({ phrases: phrases, locale: 'hr-HR' });
267
268 expect(polyglotLocale.t('n_guests', 1)).to.equal('1 gost');
269 expect(polyglotLocale.t('n_guests', 11)).to.equal('11 gostiju');
270 expect(polyglotLocale.t('n_guests', 21)).to.equal('21 gost');
271
272 expect(polyglotLocale.t('n_guests', 2)).to.equal('2 gosta');
273 expect(polyglotLocale.t('n_guests', 3)).to.equal('3 gosta');
274 expect(polyglotLocale.t('n_guests', 4)).to.equal('4 gosta');
275
276 expect(polyglotLocale.t('n_guests', 12)).to.equal('12 gostiju');
277 expect(polyglotLocale.t('n_guests', 13)).to.equal('13 gostiju');
278 expect(polyglotLocale.t('n_guests', 14)).to.equal('14 gostiju');
279 expect(polyglotLocale.t('n_guests', 112)).to.equal('112 gostiju');
280 expect(polyglotLocale.t('n_guests', 113)).to.equal('113 gostiju');
281 expect(polyglotLocale.t('n_guests', 114)).to.equal('114 gostiju');
282 });
283
284 it('pluralizes in Croatian (vote) Test', function () {
285 // English would be: "1 vote" / "%{smart_count} votes"
286 var whatSomeoneTranslated = [
287 '%{smart_count} glas',
288 '%{smart_count} glasa',
289 '%{smart_count} glasova'
290 ];
291 var phrases = {
292 n_votes: whatSomeoneTranslated.join(' |||| ')
293 };
294
295 var polyglotLocale = new Polyglot({ phrases: phrases, locale: 'hr-HR' });
296
297 forEach([1, 21, 31, 101], function (c) {
298 expect(polyglotLocale.t('n_votes', c)).to.equal(c + ' glas');
299 });
300 forEach([2, 3, 4, 22, 23, 24, 32, 33, 34], function (c) {
301 expect(polyglotLocale.t('n_votes', c)).to.equal(c + ' glasa');
302 });
303 forEach([0, 5, 6, 11, 12, 13, 14, 15, 16, 17, 25, 26, 35, 36, 112, 113, 114], function (c) {
304 expect(polyglotLocale.t('n_votes', c)).to.equal(c + ' glasova');
305 });
306
307 var polyglotLanguageCode = new Polyglot({ phrases: phrases, locale: 'hr' });
308
309 forEach([1, 21, 31, 101], function (c) {
310 expect(polyglotLanguageCode.t('n_votes', c)).to.equal(c + ' glas');
311 });
312 forEach([2, 3, 4, 22, 23, 24, 32, 33, 34], function (c) {
313 expect(polyglotLanguageCode.t('n_votes', c)).to.equal(c + ' glasa');
314 });
315 forEach([0, 5, 6, 11, 12, 13, 14, 15, 16, 17, 25, 26, 35, 36, 112, 113, 114], function (c) {
316 expect(polyglotLanguageCode.t('n_votes', c)).to.equal(c + ' glasova');
317 });
318 });
319
320 it('pluralizes in Serbian (Latin & Cyrillic)', function () {
321 // English would be: "1 vote" / "%{smart_count} votes"
322 var whatSomeoneTranslated = [
323 '%{smart_count} miš',
324 '%{smart_count} miša',
325 '%{smart_count} miševa'
326 ];
327 var phrases = {
328 n_votes: whatSomeoneTranslated.join(' |||| ')
329 };
330
331 var polyglotLatin = new Polyglot({ phrases: phrases, locale: 'srl-RS' });
332
333 expect(polyglotLatin.t('n_votes', 1)).to.equal('1 miš');
334 expect(polyglotLatin.t('n_votes', 11)).to.equal('11 miševa');
335 expect(polyglotLatin.t('n_votes', 101)).to.equal('101 miš');
336 expect(polyglotLatin.t('n_votes', 932)).to.equal('932 miša');
337 expect(polyglotLatin.t('n_votes', 324)).to.equal('324 miša');
338 expect(polyglotLatin.t('n_votes', 12)).to.equal('12 miševa');
339 expect(polyglotLatin.t('n_votes', 13)).to.equal('13 miševa');
340 expect(polyglotLatin.t('n_votes', 14)).to.equal('14 miševa');
341 expect(polyglotLatin.t('n_votes', 15)).to.equal('15 miševa');
342 expect(polyglotLatin.t('n_votes', 0)).to.equal('0 miševa');
343
344 var polyglotCyrillic = new Polyglot({ phrases: phrases, locale: 'sr-RS' });
345
346 expect(polyglotCyrillic.t('n_votes', 1)).to.equal('1 miš');
347 expect(polyglotCyrillic.t('n_votes', 11)).to.equal('11 miševa');
348 expect(polyglotCyrillic.t('n_votes', 101)).to.equal('101 miš');
349 expect(polyglotCyrillic.t('n_votes', 932)).to.equal('932 miša');
350 expect(polyglotCyrillic.t('n_votes', 324)).to.equal('324 miša');
351 expect(polyglotCyrillic.t('n_votes', 12)).to.equal('12 miševa');
352 expect(polyglotCyrillic.t('n_votes', 13)).to.equal('13 miševa');
353 expect(polyglotCyrillic.t('n_votes', 14)).to.equal('14 miševa');
354 expect(polyglotCyrillic.t('n_votes', 15)).to.equal('15 miševa');
355 expect(polyglotCyrillic.t('n_votes', 0)).to.equal('0 miševa');
356 });
357
358 it('pluralizes in Bosnian (Latin & Cyrillic)', function () {
359 // English would be: "1 vote" / "%{smart_count} votes"
360 var whatSomeoneTranslated = [
361 '%{smart_count} članak',
362 '%{smart_count} članka',
363 '%{smart_count} članaka'
364 ];
365 var phrases = {
366 n_votes: whatSomeoneTranslated.join(' |||| ')
367 };
368
369 var polyglotLatin = new Polyglot({ phrases: phrases, locale: 'bs-Latn-BA' });
370
371 expect(polyglotLatin.t('n_votes', 1)).to.equal('1 članak');
372 expect(polyglotLatin.t('n_votes', 11)).to.equal('11 članaka');
373 expect(polyglotLatin.t('n_votes', 101)).to.equal('101 članak');
374 expect(polyglotLatin.t('n_votes', 932)).to.equal('932 članka');
375 expect(polyglotLatin.t('n_votes', 324)).to.equal('324 članka');
376 expect(polyglotLatin.t('n_votes', 12)).to.equal('12 članaka');
377 expect(polyglotLatin.t('n_votes', 13)).to.equal('13 članaka');
378 expect(polyglotLatin.t('n_votes', 14)).to.equal('14 članaka');
379 expect(polyglotLatin.t('n_votes', 15)).to.equal('15 članaka');
380 expect(polyglotLatin.t('n_votes', 112)).to.equal('112 članaka');
381 expect(polyglotLatin.t('n_votes', 113)).to.equal('113 članaka');
382 expect(polyglotLatin.t('n_votes', 114)).to.equal('114 članaka');
383 expect(polyglotLatin.t('n_votes', 115)).to.equal('115 članaka');
384 expect(polyglotLatin.t('n_votes', 0)).to.equal('0 članaka');
385
386 var polyglotCyrillic = new Polyglot({ phrases: phrases, locale: 'bs-Cyrl-BA' });
387
388 expect(polyglotCyrillic.t('n_votes', 1)).to.equal('1 članak');
389 expect(polyglotCyrillic.t('n_votes', 11)).to.equal('11 članaka');
390 expect(polyglotCyrillic.t('n_votes', 101)).to.equal('101 članak');
391 expect(polyglotCyrillic.t('n_votes', 932)).to.equal('932 članka');
392 expect(polyglotCyrillic.t('n_votes', 324)).to.equal('324 članka');
393 expect(polyglotCyrillic.t('n_votes', 12)).to.equal('12 članaka');
394 expect(polyglotCyrillic.t('n_votes', 13)).to.equal('13 članaka');
395 expect(polyglotCyrillic.t('n_votes', 14)).to.equal('14 članaka');
396 expect(polyglotCyrillic.t('n_votes', 15)).to.equal('15 članaka');
397 expect(polyglotCyrillic.t('n_votes', 112)).to.equal('112 članaka');
398 expect(polyglotCyrillic.t('n_votes', 113)).to.equal('113 članaka');
399 expect(polyglotCyrillic.t('n_votes', 114)).to.equal('114 članaka');
400 expect(polyglotCyrillic.t('n_votes', 115)).to.equal('115 članaka');
401 expect(polyglotCyrillic.t('n_votes', 0)).to.equal('0 članaka');
402 });
403
404 it('pluralizes in Czech', function () {
405 // English would be: "1 vote" / "%{smart_count} votes"
406 var whatSomeoneTranslated = [
407 '%{smart_count} komentář',
408 '%{smart_count} komentáře',
409 '%{smart_count} komentářů'
410 ];
411 var phrases = {
412 n_votes: whatSomeoneTranslated.join(' |||| ')
413 };
414
415 var polyglot = new Polyglot({ phrases: phrases, locale: 'cs-CZ' });
416
417 expect(polyglot.t('n_votes', 1)).to.equal('1 komentář');
418 expect(polyglot.t('n_votes', 2)).to.equal('2 komentáře');
419 expect(polyglot.t('n_votes', 3)).to.equal('3 komentáře');
420 expect(polyglot.t('n_votes', 4)).to.equal('4 komentáře');
421 expect(polyglot.t('n_votes', 0)).to.equal('0 komentářů');
422 expect(polyglot.t('n_votes', 11)).to.equal('11 komentářů');
423 expect(polyglot.t('n_votes', 12)).to.equal('12 komentářů');
424 expect(polyglot.t('n_votes', 16)).to.equal('16 komentářů');
425 });
426
427 it('pluralizes in Slovenian', function () {
428 // English would be: "1 vote" / "%{smart_count} votes"
429 var whatSomeoneTranslated = [
430 '%{smart_count} komentar',
431 '%{smart_count} komentarja',
432 '%{smart_count} komentarji',
433 '%{smart_count} komentarjev'
434 ];
435 var phrases = {
436 n_votes: whatSomeoneTranslated.join(' |||| ')
437 };
438
439 var polyglot = new Polyglot({ phrases: phrases, locale: 'sl-SL' });
440
441 forEach([1, 12301, 101, 1001, 201, 301], function (c) {
442 expect(polyglot.t('n_votes', c)).to.equal(c + ' komentar');
443 });
444
445 forEach([2, 102, 202, 302], function (c) {
446 expect(polyglot.t('n_votes', c)).to.equal(c + ' komentarja');
447 });
448
449 forEach([0, 11, 12, 13, 14, 52, 53], function (c) {
450 expect(polyglot.t('n_votes', c)).to.equal(c + ' komentarjev');
451 });
452 });
453
454 it('pluralizes in Turkish', function () {
455 var whatSomeoneTranslated = [
456 'Sepetinizde %{smart_count} X var. Bunu almak istiyor musunuz?',
457 'Sepetinizde %{smart_count} X var. Bunları almak istiyor musunuz?'
458 ];
459 var phrases = {
460 n_x_cart: whatSomeoneTranslated.join(' |||| ')
461 };
462
463 var polyglot = new Polyglot({ phrases: phrases, locale: 'tr' });
464
465 expect(polyglot.t('n_x_cart', 1)).to.equal('Sepetinizde 1 X var. Bunu almak istiyor musunuz?');
466 expect(polyglot.t('n_x_cart', 2)).to.equal('Sepetinizde 2 X var. Bunları almak istiyor musunuz?');
467 });
468
469 it('pluralizes in Lithuanian', function () {
470 var whatSomeoneTranslated = [
471 '%{smart_count} balsas',
472 '%{smart_count} balsai',
473 '%{smart_count} balsų'
474 ];
475 var phrases = {
476 n_votes: whatSomeoneTranslated.join(' |||| ')
477 };
478 var polyglot = new Polyglot({ phrases: phrases, locale: 'lt' });
479
480 expect(polyglot.t('n_votes', 0)).to.equal('0 balsų');
481 expect(polyglot.t('n_votes', 1)).to.equal('1 balsas');
482 expect(polyglot.t('n_votes', 2)).to.equal('2 balsai');
483 expect(polyglot.t('n_votes', 9)).to.equal('9 balsai');
484 expect(polyglot.t('n_votes', 10)).to.equal('10 balsų');
485 expect(polyglot.t('n_votes', 11)).to.equal('11 balsų');
486 expect(polyglot.t('n_votes', 12)).to.equal('12 balsų');
487 expect(polyglot.t('n_votes', 90)).to.equal('90 balsų');
488 expect(polyglot.t('n_votes', 91)).to.equal('91 balsas');
489 expect(polyglot.t('n_votes', 92)).to.equal('92 balsai');
490 expect(polyglot.t('n_votes', 102)).to.equal('102 balsai');
491 });
492});
493
494describe('custom pluralRules', function () {
495 var customPluralRules = {
496 pluralTypes: {
497 germanLike: function (n) {
498 // is 1
499 if (n === 1) {
500 return 0;
501 }
502 // everything else
503 return 1;
504 },
505 frenchLike: function (n) {
506 // is 0 or 1
507 if (n <= 1) {
508 return 0;
509 }
510 // everything else
511 return 1;
512 }
513 },
514 pluralTypeToLanguages: {
515 germanLike: ['x1'],
516 frenchLike: ['x2']
517 }
518 };
519
520 var testPhrases = {
521 test_phrase: '%{smart_count} form zero |||| %{smart_count} form one'
522 };
523
524 it('pluralizes in x1', function () {
525 var polyglot = new Polyglot({
526 phrases: testPhrases,
527 locale: 'x1',
528 pluralRules: customPluralRules
529 });
530
531 expect(polyglot.t('test_phrase', 0)).to.equal('0 form one');
532 expect(polyglot.t('test_phrase', 1)).to.equal('1 form zero');
533 expect(polyglot.t('test_phrase', 2)).to.equal('2 form one');
534 });
535
536 it('pluralizes in x2', function () {
537 var polyglot = new Polyglot({
538 phrases: testPhrases,
539 locale: 'x2',
540 pluralRules: customPluralRules
541 });
542
543 expect(polyglot.t('test_phrase', 0)).to.equal('0 form zero');
544 expect(polyglot.t('test_phrase', 1)).to.equal('1 form zero');
545 expect(polyglot.t('test_phrase', 2)).to.equal('2 form one');
546 });
547
548 it('memoizes plural type language correctly and selects the correct locale after several calls', function () {
549 var polyglot = new Polyglot({
550 phrases: {
551 test_phrase: '%{smart_count} Name |||| %{smart_count} Names'
552 },
553 locale: 'x1',
554 pluralRules: customPluralRules
555 });
556
557 expect(polyglot.t('test_phrase', 0)).to.equal('0 Names');
558 expect(polyglot.t('test_phrase', 0)).to.equal('0 Names');
559 expect(polyglot.t('test_phrase', 1)).to.equal('1 Name');
560 expect(polyglot.t('test_phrase', 1)).to.equal('1 Name');
561 expect(polyglot.t('test_phrase', 2)).to.equal('2 Names');
562 expect(polyglot.t('test_phrase', 2)).to.equal('2 Names');
563 });
564});
565
566describe('locale', function () {
567 var polyglot;
568 beforeEach(function () {
569 polyglot = new Polyglot();
570 });
571
572 it('defaults to "en"', function () {
573 expect(polyglot.locale()).to.equal('en');
574 });
575
576 it('gets and sets locale', function () {
577 polyglot.locale('es');
578 expect(polyglot.locale()).to.equal('es');
579
580 polyglot.locale('fr');
581 expect(polyglot.locale()).to.equal('fr');
582 });
583});
584
585describe('extend', function () {
586 var polyglot;
587 beforeEach(function () {
588 polyglot = new Polyglot();
589 });
590
591 it('handles null gracefully', function () {
592 expect(function () { polyglot.extend(null); }).to.not.throw();
593 });
594
595 it('handles undefined gracefully', function () {
596 expect(function () { polyglot.extend(undefined); }).to.not.throw();
597 });
598
599 it('supports multiple extends, overriding old keys', function () {
600 polyglot.extend({ aKey: 'First time' });
601 polyglot.extend({ aKey: 'Second time' });
602 expect(polyglot.t('aKey')).to.equal('Second time');
603 });
604
605 it('does not forget old keys', function () {
606 polyglot.extend({ firstKey: 'Numba one', secondKey: 'Numba two' });
607 polyglot.extend({ secondKey: 'Numero dos' });
608 expect(polyglot.t('firstKey')).to.equal('Numba one');
609 });
610
611 it('supports optional `prefix` argument', function () {
612 polyglot.extend({ click: 'Click', hover: 'Hover' }, 'sidebar');
613 expect(polyglot.phrases['sidebar.click']).to.equal('Click');
614 expect(polyglot.phrases['sidebar.hover']).to.equal('Hover');
615 expect(polyglot.phrases).not.to.have.property('click');
616 });
617
618 it('supports nested object', function () {
619 polyglot.extend({
620 sidebar: {
621 click: 'Click',
622 hover: 'Hover'
623 },
624 nav: {
625 header: {
626 log_in: 'Log In'
627 }
628 }
629 });
630 expect(polyglot.phrases['sidebar.click']).to.equal('Click');
631 expect(polyglot.phrases['sidebar.hover']).to.equal('Hover');
632 expect(polyglot.phrases['nav.header.log_in']).to.equal('Log In');
633 expect(polyglot.phrases).not.to.have.property('click');
634 expect(polyglot.phrases).not.to.have.property('header.log_in');
635 expect(polyglot.phrases).not.to.have.property('log_in');
636 });
637});
638
639describe('clear', function () {
640 var polyglot;
641 beforeEach(function () {
642 polyglot = new Polyglot();
643 });
644
645 it('wipes out old phrases', function () {
646 polyglot.extend({ hiFriend: 'Hi, Friend.' });
647 polyglot.clear();
648 expect(polyglot.t('hiFriend')).to.equal('hiFriend');
649 });
650});
651
652describe('replace', function () {
653 var polyglot;
654 beforeEach(function () {
655 polyglot = new Polyglot();
656 });
657
658 it('wipes out old phrases and replace with new phrases', function () {
659 polyglot.extend({ hiFriend: 'Hi, Friend.', byeFriend: 'Bye, Friend.' });
660 polyglot.replace({ hiFriend: 'Hi, Friend.' });
661 expect(polyglot.t('hiFriend')).to.equal('Hi, Friend.');
662 expect(polyglot.t('byeFriend')).to.equal('byeFriend');
663 });
664});
665
666describe('unset', function () {
667 var polyglot;
668 beforeEach(function () {
669 polyglot = new Polyglot();
670 });
671
672 it('handles null gracefully', function () {
673 expect(function () { polyglot.unset(null); }).to.not.throw();
674 });
675
676 it('handles undefined gracefully', function () {
677 expect(function () { polyglot.unset(undefined); }).to.not.throw();
678 });
679
680 it('unsets a key based on a string', function () {
681 polyglot.extend({ test_key: 'test_value' });
682 expect(polyglot.has('test_key')).to.equal(true);
683
684 polyglot.unset('test_key');
685 expect(polyglot.has('test_key')).to.equal(false);
686 });
687
688 it('unsets a key based on an object hash', function () {
689 polyglot.extend({ test_key: 'test_value', foo: 'bar' });
690 expect(polyglot.has('test_key')).to.equal(true);
691 expect(polyglot.has('foo')).to.equal(true);
692
693 polyglot.unset({ test_key: 'test_value', foo: 'bar' });
694 expect(polyglot.has('test_key')).to.equal(false);
695 expect(polyglot.has('foo')).to.equal(false);
696 });
697
698 it('unsets nested objects using recursive prefix call', function () {
699 polyglot.extend({ foo: { bar: 'foobar' } });
700 expect(polyglot.has('foo.bar')).to.equal(true);
701
702 polyglot.unset({ foo: { bar: 'foobar' } });
703 expect(polyglot.has('foo.bar')).to.equal(false);
704 });
705});
706
707describe('transformPhrase', function () {
708 var simple = '%{name} is %{attribute}';
709 var english = '%{smart_count} Name |||| %{smart_count} Names';
710 var arabic = [
711 'ولا صوت',
712 'صوت واحد',
713 'صوتان',
714 '%{smart_count} أصوات',
715 '%{smart_count} صوت',
716 '%{smart_count} صوت'
717 ].join(' |||| ');
718
719 it('does simple interpolation', function () {
720 expect(Polyglot.transformPhrase(simple, { name: 'Polyglot', attribute: 'awesome' })).to.equal('Polyglot is awesome');
721 });
722
723 it('removes missing keys', function () {
724 expect(Polyglot.transformPhrase(simple, { name: 'Polyglot' })).to.equal('Polyglot is %{attribute}');
725 });
726
727 it('selects the correct plural form based on smart_count', function () {
728 expect(Polyglot.transformPhrase(english, { smart_count: 0 }, 'en')).to.equal('0 Names');
729 expect(Polyglot.transformPhrase(english, { smart_count: 1 }, 'en')).to.equal('1 Name');
730 expect(Polyglot.transformPhrase(english, { smart_count: 2 }, 'en')).to.equal('2 Names');
731 expect(Polyglot.transformPhrase(english, { smart_count: 3 }, 'en')).to.equal('3 Names');
732 });
733
734 it('selects the correct locale', function () {
735 // French rule: "0" is singular
736 expect(Polyglot.transformPhrase(english, { smart_count: 0 }, 'fr')).to.equal('0 Name');
737 expect(Polyglot.transformPhrase(english, { smart_count: 1 }, 'fr')).to.equal('1 Name');
738 expect(Polyglot.transformPhrase(english, { smart_count: 1.5 }, 'fr')).to.equal('1.5 Name');
739 // French rule: plural starts at 2 included
740 expect(Polyglot.transformPhrase(english, { smart_count: 2 }, 'fr')).to.equal('2 Names');
741 expect(Polyglot.transformPhrase(english, { smart_count: 3 }, 'fr')).to.equal('3 Names');
742
743 // Arabic has 6 rules
744 expect(Polyglot.transformPhrase(arabic, 0, 'ar')).to.equal('ولا صوت');
745 expect(Polyglot.transformPhrase(arabic, 1, 'ar')).to.equal('صوت واحد');
746 expect(Polyglot.transformPhrase(arabic, 2, 'ar')).to.equal('صوتان');
747 expect(Polyglot.transformPhrase(arabic, 3, 'ar')).to.equal('3 أصوات');
748 expect(Polyglot.transformPhrase(arabic, 11, 'ar')).to.equal('11 صوت');
749 expect(Polyglot.transformPhrase(arabic, 102, 'ar')).to.equal('102 صوت');
750 });
751
752 it('defaults to `en`', function () {
753 // French rule: "0" is singular
754 expect(Polyglot.transformPhrase(english, { smart_count: 0 })).to.equal('0 Names');
755 });
756
757 it('ignores a region subtag when choosing a pluralization rule', function () {
758 // French rule: "0" is singular
759 expect(Polyglot.transformPhrase(english, { smart_count: 0 }, 'fr-FR')).to.equal('0 Name');
760 });
761
762 it('works without arguments', function () {
763 expect(Polyglot.transformPhrase(english)).to.equal(english);
764 });
765
766 it('respects a number as shortcut for smart_count', function () {
767 expect(Polyglot.transformPhrase(english, 0, 'en')).to.equal('0 Names');
768 expect(Polyglot.transformPhrase(english, 1, 'en')).to.equal('1 Name');
769 expect(Polyglot.transformPhrase(english, 5, 'en')).to.equal('5 Names');
770 });
771
772 it('throws without sane phrase string', function () {
773 expect(function () { Polyglot.transformPhrase(); }).to.throw(TypeError);
774 expect(function () { Polyglot.transformPhrase(null); }).to.throw(TypeError);
775 expect(function () { Polyglot.transformPhrase(32); }).to.throw(TypeError);
776 expect(function () { Polyglot.transformPhrase({}); }).to.throw(TypeError);
777 });
778
779 it('memoizes plural type language correctly and selects the correct locale after several calls', function () {
780 expect(Polyglot.transformPhrase(english, { smart_count: 0 }, 'en')).to.equal('0 Names');
781 expect(Polyglot.transformPhrase(english, { smart_count: 0 }, 'en')).to.equal('0 Names');
782 expect(Polyglot.transformPhrase(english, { smart_count: 1 }, 'en')).to.equal('1 Name');
783 expect(Polyglot.transformPhrase(english, { smart_count: 1 }, 'en')).to.equal('1 Name');
784
785 expect(Polyglot.transformPhrase(english, { smart_count: 0 }, 'fr')).to.equal('0 Name');
786 expect(Polyglot.transformPhrase(english, { smart_count: 0 }, 'fr')).to.equal('0 Name');
787 expect(Polyglot.transformPhrase(english, { smart_count: 2 }, 'fr')).to.equal('2 Names');
788 expect(Polyglot.transformPhrase(english, { smart_count: 2 }, 'fr')).to.equal('2 Names');
789 });
790});