translation_spec.rb
3.7 KB
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
current_folder = File.dirname(__FILE__)
require File.join(current_folder,'..','spec_helper')
describe FastGettext::Translation do
include FastGettext::Translation
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 key if not translation was found" do
_('NOT|FOUND').should == 'NOT|FOUND'
end
it "does not return the gettext meta information" do
_('').should == ''
end
end
describe :n_ do
before do
FastGettext.pluralisation_rule = nil
end
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 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
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 seperator" do
s_("XXX/not found",'/').should == "not found"
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 :caching do
describe :cache_hit do
before do
FastGettext.translation_repositories.replace({})
#singular cache keys
FastGettext.current_cache['xxx'] = '1'
#plural cache keys
FastGettext.current_cache['||||xxx'] = ['1','2']
FastGettext.current_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 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 seperatly" do
FastGettext.locale = 'en'
_('car').should == 'car'
FastGettext.locale = 'de'
_('car').should == 'Auto'
end
it "caches different textdomains seperatly" do
_('car').should == 'Auto'
FastGettext.translation_repositories['fake'] = {}
FastGettext.text_domain = 'fake'
_('car').should == 'car'
FastGettext.text_domain = 'test'
_('car').should == 'Auto'
end
end
end