File: translation_spec.rb

package info (click to toggle)
ruby-fast-gettext 4.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 676 kB
  • sloc: ruby: 3,209; makefile: 4
file content (467 lines) | stat: -rw-r--r-- 13,515 bytes parent folder | download | duplicates (5)
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
require "spec_helper"

SingleCov.covered!

describe FastGettext::Translation do
  include FastGettext::TranslationMultidomain

  before do
    default_setup
  end

  describe "unknown locale" do
    before do
      FastGettext.available_locales = nil
      FastGettext.locale = 'xx'
    end

    it "does not translate" do
      _('car').should == 'car'
    end

    it "does not translate plurals" do
      n_('car','cars',2).should == 'cars'
    end
  end

  describe :_ do
    it "translates simple text" do
      _('car').should == 'Auto'
    end

    it "returns the original string if its translation is blank" do
      _('Untranslated').should == 'Untranslated'
    end

    it "does not return the blank translation if a string's translation is blank" do
      _('Untranslated').should_not == ''
    end

    it "returns key if not translation was found" do
      _('NOT|FOUND').should == 'NOT|FOUND'
    end

    it "does not return the gettext meta information" do
      _('').should == ''
    end

    it "returns nil when specified" do
      _('not found'){nil}.should be_nil
    end

    it "returns block when specified" do
      _('not found'){:block}.should == :block
    end
  end

  describe :n_ do
    it "translates pluralized" do
      n_('Axis','Axis',1).should == 'Achse'
      n_('Axis','Axis',2).should == 'Achsen'
      n_('Axis','Axis',0).should == 'Achsen'
    end

    describe "pluralisations rules" do
      it "supports abstract pluralisation rules" do
        FastGettext.pluralisation_rule = lambda{|n|2}
        n_('a','b','c','d',4).should == 'c'
      end

      it "supports false as singular" do
        FastGettext.pluralisation_rule = lambda{|n|n!=2}
        n_('singular','plural','c','d',2).should == 'singular'
      end

      it "supports true as plural" do
        FastGettext.pluralisation_rule = lambda{|n|n==2}
        n_('singular','plural','c','d',2).should == 'plural'
      end
    end

    it "returns a simple translation when no combined was found" do
      n_('Axis','NOTFOUNDs',1).should == 'Achse'
    end

    it "returns the appropriate key if no translation was found" do
      n_('NOTFOUND','NOTFOUNDs',1).should == 'NOTFOUND'
      n_('NOTFOUND','NOTFOUNDs',2).should == 'NOTFOUNDs'
    end

    it "returns the last key when no translation was found and keys where to short" do
      FastGettext.pluralisation_rule = lambda{|x|4}
      n_('Apple','Apples',2).should == 'Apples'
    end

    it "returns block when specified" do
      n_('not found'){:block}.should == :block
    end
  end

  describe :p_ do
    it "returns key if a translation was not found" do
      p_("XXX","not found").should == "not found"
    end

    it "returns block when specified" do
      p_("XXX",'not found'){:block}.should == :block
    end
  end

  describe :s_ do
    it "translates simple text" do
      s_('car').should == 'Auto'
    end

    it "returns cleaned key if a translation was not found" do
      s_("XXX|not found").should == "not found"
    end

    it "can use a custom separator" do
      s_("XXX/not found",'/').should == "not found"
    end

    it "returns block when specified" do
      s_('not found'){:block}.should == :block
    end
  end

  describe :N_ do
    it "returns the key" do
      N_('XXXXX').should == 'XXXXX'
    end
  end

  describe :Nn_ do
    it "returns the keys as array" do
      Nn_('X','Y').should == ['X','Y']
    end
  end

  describe :ns_ do
    before { default_setup }

    it "translates plural with namespace" do
      ns_('Cat|Foot', 'Foot', 1).should == 'Tatze'
      ns_('Cat|Foot', 'Feet', 2).should == 'Tatzen'
      ns_('Cat|Foo', 'Feet', 3).should == 'Tatzen'
    end

    it "translates plural with double namespace" do
      # This behavior does not match gettext but
      # let's make sure it continues to work as to not introduce
      # a breaking change.
      ns_('Fruit|Apple','Fruit|Apples',2).should == 'Apples'
    end

    it "returns block when specified" do
      ns_('not found'){:block}.should == :block
      ns_('not found'){nil}.should be_nil
    end
  end

  describe :np_ do
    it "translates whith namespace" do
      np_('Fruit', 'Banana', 'Bananas', 1).should == 'Banane'
      np_('Fruit', 'Banana', 'Bananas', 2).should == 'Bananen'
    end

    it "return key when not found" do
      np_('fruit','not-found',2).should == 'not-found'
    end

    it "returns block when specified" do
      np_('fruit','not-found', 2){:block}.should == :block
      np_('fruit','not-found', 2){nil}.should be_nil
    end
  end

  describe "aliases" do
    include FastGettext::TranslationAliased

    it "provides gettext alternatives" do
      gettext('car').should == 'Auto'
      sgettext('car').should == 'Auto'
    end
  end

  describe "namespace" do
    let(:tester) { Class.new }

    it "has few methods" do
      before = tester.instance_methods
      tester.include FastGettext::Translation
      (tester.instance_methods - before - FastGettext::TRANSLATION_METHODS).sort.should eq [:N_, :Nn_].sort
    end

    it "has few constants" do
      before = tester.constants
      tester.include FastGettext::Translation
      (tester.constants - before).should eq []
    end
  end

  describe "multi domain" do
    before do
      setup_extra_domain
    end

    describe :d_ do
      it "translates simple text" do
        d_('test', 'car').should == 'Auto'
      end

      it "translates simple text in different domain" do
        d_('test2', 'car').should == 'Auto 2'
      end

      it "translates simple text in different domain one transaction" do
        d_('test', 'car').should == 'Auto'
        d_('test2', 'car').should == 'Auto 2'
      end

      it "returns the original string if its translation is blank" do
        d_('test', 'Untranslated').should == 'Untranslated'
      end

      it "sets text domain back to previous one" do
        old_domain = FastGettext.text_domain
        d_('test2', 'car').should == 'Auto 2'
        FastGettext.text_domain.should == old_domain
      end

      it "returns appropriate key if translation is not found in a domain" do
        FastGettext.translation_repositories['fake'] = {}
        d_('fake', 'car').should == 'car'
      end
    end

    describe :dn_ do
      it "translates pluralized" do
        dn_('test', 'Axis','Axis',1).should == 'Achse'
        dn_('test2', 'Axis','Axis',1).should == 'Achse 2'
      end

      it "returns a simple translation when no combined was found" do
        dn_('test', 'Axis','NOTFOUNDs',1).should == 'Achse'
        dn_('test2', 'Axis','NOTFOUNDs',1).should == 'Achse 2'
      end

      it "returns the appropriate key if no translation was found" do
        dn_('test', 'NOTFOUND','NOTFOUNDs',1).should == 'NOTFOUND'
        dn_('test', 'NOTFOUND','NOTFOUNDs',2).should == 'NOTFOUNDs'
      end

      it "returns the last key when no translation was found and keys where to short" do
        FastGettext.pluralisation_rule = lambda{|x|4}
        dn_('test', 'Apple','Apples',2).should == 'Apples'
      end
    end

    describe :dp_ do
      it "translates simple text" do
        dp_('test2', 'Fruit', 'Apple').should == 'Apfel 2'
        dp_('test', 'Fruit', 'Apple').should == 'Apfel'
      end

      it "returns key if a translation was not found" do
        dp_('test2', "Fruit","not found").should == "not found"
        dp_('test2', "Fruit","not found"){:block}.should == :block
      end
    end

    describe :ds_ do
      it "translates simple text" do
        ds_('test2', 'car').should == 'Auto 2'
        ds_('test', 'car').should == 'Auto'
      end

      it "returns cleaned key if a translation was not found" do
        ds_('test2', "XXX|not found").should == "not found"
      end

      it "can use a custom separator" do
        ds_('test2', "XXX/not found",'/').should == "not found"
      end
    end

    describe :dns_ do
      it "translates with namespace" do
        dns_('test', 'Fruit|Apple','Fruit|Apples',2).should == 'Apples'
        dns_('test2', 'Fruit|Apple','Fruit|Apples',2).should == 'Apples'
      end
    end

    describe :dnp_ do
      it "translates with context" do
        dnp_('test', 'Fruit', 'Banana','Bananas', 1).should == 'Banane'
        dnp_('test', 'Fruit', 'Banana','Bananas', 2).should == 'Bananen'
        dnp_('test2', 'Fruit', 'Banana','Bananas', 1).should == 'Banane 2'
        dnp_('test2', 'Fruit', 'Banana','Bananas', 2).should == 'Bananen 2'
      end
    end
  end

  describe "multi domain all" do
    before do
      setup_extra_domain
    end

    describe :D_ do
      it "translates simple text" do
        D_('only in test2 domain').should == 'nur in test2 Domain'
      end

      it "returns translation from random domain" do
        D_('car').should match('(Auto|Auto 2)')
      end

      it "sets text domain back to previous one" do
        old_domain = FastGettext.text_domain
        D_('car').should match('(Auto|Auto 2)')
        FastGettext.text_domain.should == old_domain
      end

      it "returns key or block on not found" do
        D_('not found').should == 'not found'
        D_('not found'){:block}.should == :block
      end
    end

    describe :Dn_ do
      it "translates pluralized" do
        Dn_('Axis','Axis',1).should match('(Achse|Achse 2)')
      end

      it "returns a simple translation (or block) when no combined was found" do
        Dn_('Axis','NOTFOUNDs',1).should match('(Achse|Achse 2)')
        Dn_('Axis','NOTFOUNDs',1){:block}.should == :block
      end

      it "returns the appropriate key (or block) if no translation was found" do
        Dn_('NOTFOUND','NOTFOUNDs',1).should == 'NOTFOUND'
        Dn_('NOTFOUND','NOTFOUNDs',1){:block}.should == :block
      end

      it "returns the last key when no translation was found and keys where to short" do
        Dn_('Apple','Apples',2).should == 'Apples'
        Dn_('Apple','Apples',2){:block}.should == :block
      end
    end

    describe :Dp_ do
      it "translates simple text" do
        Dp_('Fruit','Apple').should match('(Apfel|Apfel 2)')
      end

      it "returns key or block if a translation was not found" do
        Dp_('Fruit',"not found").should == "not found"
        Dp_('Fruit',"not found"){:block}.should == :block
      end
    end

    describe :Ds_ do
      it "translates simple text" do
        Ds_('car').should match('(Auto|Auto 2)')
      end

      it "returns cleaned key (or block) if a translation was not found" do
        Ds_("XXX|not found").should == "not found"
        Ds_("XXX|not found"){:block}.should == :block
      end

      it "can use a custom separator" do
        Ds_("XXX/not found",'/').should == "not found"
        Ds_("XXX/not found",'/'){:block}.should == :block
      end
    end

    describe :Dnp_ do
      it "translates with context" do
        Dnp_('Fruit','Apple','Apples',1).should == 'Apple'
        Dnp_('Fruit','Apple','Apples',2).should == 'Apples'
      end

      it "returns cleaned key (or block) if a translation was not found" do
        Dnp_("XXX","not found", "not found", 1).should == "not found"
        Dnp_("XXX","not found", "not found", 2).should == "not found"
        Dnp_("XXX","not found", "not found", 2){:block}.should == :block
      end
    end

    describe :Dns_ do
      it "translates with namespace" do
        Dns_('Fruit|Apple','Fruit|Apples',1).should == 'Apple'
        Dns_('Fruit|Apple','Fruit|Apples',2).should == 'Apples'
        Dns_('Fruit|Apple','Fruit|Apples',4).should == 'Apples'
        Dns_('Fruit|Apple','Apples',2).should == 'Apples'
      end

      it "returns cleaned key if a translation was not found" do
        Dns_("XXX|not found", "YYY|not found", 1).should == "not found"
        Dns_("XXX|not found", "YYY|not found", 2).should == "not found"
        Dns_("XXX|not found", "not found", 2).should == "not found"
        Dns_("XXX|not found", "not found", 2){:block} == :block
      end
    end
  end

  describe "caching" do
    describe :cache_hit do
      before do
        #singular cache keys
        FastGettext.cache['xxx'] = '1'
        FastGettext.cache["zzz\004qqq"] = '3'

        #plural cache keys
        FastGettext.cache['||||xxx'] = ['1','2']
        FastGettext.cache['||||xxx||||yyy'] = ['1','2']
      end

      it "uses the cache when translating with _" do
        _('xxx').should == '1'
      end

      it "uses the cache when translating with p_" do
        p_('zzz','qqq').should == '3'
      end

      it "uses the cache when translating with s_" do
        s_('xxx').should == '1'
      end

      it "uses the cache when translating with n_" do
        n_('xxx','yyy',1).should == '1'
      end

      it "uses the cache when translating with n_ and single argument" do
        n_('xxx',1).should == '1'
      end
    end

    it "caches different locales separatly" do
      FastGettext.locale = 'en'
      _('car').should == 'car'
      FastGettext.locale = 'de'
      _('car').should == 'Auto'
    end

    it "caches different textdomains separatly" do
      _('car').should == 'Auto'

      FastGettext.translation_repositories['fake'] = {}
      FastGettext.text_domain = 'fake'
      _('car').should == 'car'

      FastGettext.text_domain = 'test'
      _('car').should == 'Auto'
    end

    it "caches different textdomains separatly for d_" do
      _('car').should == 'Auto'

      FastGettext.translation_repositories['fake'] = {}
      d_('fake', 'car').should == 'car'
      d_('test','car').should == 'Auto'
    end
  end
end