tr8n_core 4.0.1 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +163 -33
- data/lib/tr8n/application.rb +29 -27
- data/lib/tr8n/base.rb +2 -22
- data/lib/tr8n/cache.rb +9 -9
- data/lib/tr8n/cache_adapters/cdb.rb +2 -2
- data/lib/tr8n/cache_adapters/file.rb +2 -2
- data/lib/tr8n/cache_adapters/memcache.rb +3 -3
- data/lib/tr8n/cache_adapters/redis.rb +3 -3
- data/lib/tr8n/component.rb +1 -1
- data/lib/tr8n/config.rb +256 -202
- data/lib/tr8n/decorators/base.rb +1 -1
- data/lib/tr8n/decorators/default.rb +1 -1
- data/lib/tr8n/decorators/html.rb +4 -4
- data/lib/tr8n/exception.rb +1 -1
- data/lib/tr8n/language.rb +17 -27
- data/lib/tr8n/language_case.rb +4 -3
- data/lib/tr8n/language_case_rule.rb +1 -1
- data/lib/tr8n/language_context.rb +4 -2
- data/lib/tr8n/language_context_rule.rb +1 -1
- data/lib/tr8n/logger.rb +8 -3
- data/lib/tr8n/rules_engine/evaluator.rb +4 -7
- data/lib/tr8n/rules_engine/parser.rb +1 -28
- data/lib/tr8n/session.rb +87 -0
- data/lib/tr8n/source.rb +1 -1
- data/lib/tr8n/tokens/data.rb +2 -2
- data/lib/tr8n/tokens/data_tokenizer.rb +1 -1
- data/lib/tr8n/tokens/decoration_tokenizer.rb +1 -1
- data/lib/tr8n/tokens/hidden.rb +1 -1
- data/lib/tr8n/tokens/method.rb +1 -1
- data/lib/tr8n/tokens/transform.rb +2 -2
- data/lib/tr8n/translation.rb +2 -2
- data/lib/tr8n/translation_key.rb +4 -4
- data/lib/tr8n/translator.rb +1 -1
- data/lib/tr8n/utils.rb +1 -1
- data/lib/tr8n_core/ext/array.rb +4 -4
- data/lib/tr8n_core/ext/date.rb +38 -13
- data/lib/tr8n_core/ext/fixnum.rb +3 -3
- data/lib/tr8n_core/ext/hash.rb +1 -1
- data/lib/tr8n_core/ext/string.rb +3 -3
- data/lib/tr8n_core/ext/time.rb +30 -24
- data/lib/tr8n_core/generators/cache/base.rb +4 -4
- data/lib/tr8n_core/generators/cache/cdb.rb +1 -1
- data/lib/tr8n_core/generators/cache/file.rb +4 -3
- data/lib/tr8n_core/version.rb +2 -2
- data/lib/tr8n_core.rb +1 -1
- data/spec/application_spec.rb +5 -192
- data/spec/base_spec.rb +1 -1
- data/spec/config_spec.rb +21 -5
- data/spec/decorator_spec.rb +1 -1
- data/spec/decorators/base_spec.rb +1 -1
- data/spec/decorators/default_spec.rb +1 -1
- data/spec/decorators/html_spec.rb +8 -8
- data/spec/fixtures/languages/ru.json +36 -4
- data/spec/language_case_rule_spec.rb +1 -1
- data/spec/language_case_spec.rb +1 -1
- data/spec/language_context_rule_spec.rb +1 -1
- data/spec/language_context_spec.rb +1 -1
- data/spec/language_spec.rb +603 -4
- data/spec/rules_engine/evaluator_spec.rb +1 -1
- data/spec/rules_engine/parser_spec.rb +1 -1
- data/spec/{helper.rb → spec_helper.rb} +15 -7
- data/spec/tokens/data_spec.rb +2 -2
- data/spec/tokens/data_tokenizer_spec.rb +1 -1
- data/spec/tokens/decoration_tokenizer_spec.rb +1 -1
- data/spec/tokens/hidden_spec.rb +1 -1
- data/spec/tokens/method_spec.rb +1 -1
- data/spec/tokens/transform_spec.rb +1 -1
- data/spec/translation_key_spec.rb +1 -1
- data/spec/translation_spec.rb +1 -1
- data/spec/utils_spec.rb +1 -3
- metadata +5 -8
- data/config/config.yml +0 -34
- data/config/tokens/data.yml +0 -45
- data/config/tokens/decorations.yml +0 -37
- data/lib/tr8n_core/modules/logger.rb +0 -43
data/spec/language_spec.rb
CHANGED
@@ -1,16 +1,615 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Tr8n::Language do
|
6
6
|
describe "#initialize" do
|
7
7
|
it "sets language attributes" do
|
8
|
-
russian = Tr8n::Language.new(load_json('languages/ru.json'))
|
9
|
-
expect(russian.locale).to eq('ru')
|
10
|
-
expect(russian.full_name).to eq("Russian - Русский")
|
8
|
+
@russian = Tr8n::Language.new(load_json('languages/ru.json'))
|
9
|
+
expect(@russian.locale).to eq('ru')
|
10
|
+
expect(@russian.full_name).to eq("Russian - Русский")
|
11
|
+
expect(@russian.dir).to eq("ltr")
|
12
|
+
|
13
|
+
expect(@russian.align("left")).to eq("left")
|
14
|
+
|
15
|
+
expect(@russian.current_source(:source => "custom")).to eq("custom")
|
16
|
+
expect(@russian.current_source({})).to eq("undefined")
|
17
|
+
|
18
|
+
expect(@russian.class.cache_key("ru")).to eq("l@_[ru]")
|
19
|
+
expect(@russian.class.cache_prefix).to eq("l@")
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
describe "testing translations" do
|
26
|
+
before do
|
27
|
+
Tr8n.session.current_translator = nil
|
28
|
+
|
29
|
+
@app = init_application
|
30
|
+
@english = @app.language('en-US')
|
31
|
+
@russian = @app.language('ru')
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "basic translations" do
|
35
|
+
it "should return correct translations" do
|
36
|
+
Tr8n.session.with_block_options(:dry => true) do
|
37
|
+
load_translation_key_from_hash(@app, {
|
38
|
+
"label" => "Hello World",
|
39
|
+
"translations" => {
|
40
|
+
"ru" => [
|
41
|
+
{
|
42
|
+
"label"=> "Привет Мир"
|
43
|
+
}
|
44
|
+
]
|
45
|
+
}
|
46
|
+
})
|
47
|
+
|
48
|
+
expect(@english.translate('Hello World')).to eq("Hello World")
|
49
|
+
expect(@russian.translate('Hello World')).to eq("Привет Мир")
|
50
|
+
|
51
|
+
load_translation_key_from_array(@app, [{
|
52
|
+
"label" => "Invite",
|
53
|
+
"description" => "An invitation",
|
54
|
+
"translations" => {
|
55
|
+
"ru" => [
|
56
|
+
{
|
57
|
+
"label"=> "Приглашение"
|
58
|
+
}
|
59
|
+
]
|
60
|
+
}
|
61
|
+
}, {
|
62
|
+
"label" => "Invite",
|
63
|
+
"description" => "An action to invite",
|
64
|
+
"translations" => {
|
65
|
+
"ru" => [
|
66
|
+
{
|
67
|
+
"label"=> "Приглашать"
|
68
|
+
}
|
69
|
+
]
|
70
|
+
}
|
71
|
+
}])
|
72
|
+
|
73
|
+
expect(@english.translate('Invite', 'An invitation')).to eq("Invite")
|
74
|
+
expect(@english.translate('Invite', 'An action to invite')).to eq("Invite")
|
75
|
+
expect(@russian.translate('Invite', 'An invitation')).to eq("Приглашение")
|
76
|
+
expect(@russian.translate('Invite', 'An action to invite')).to eq("Приглашать")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "numeric tokens" do
|
82
|
+
it "should return correct translations" do
|
83
|
+
Tr8n.session.with_block_options(:dry => true) do
|
84
|
+
|
85
|
+
expect(@english.translate('You have {count||message}.', :count => 0)).to eq("You have 0 messages.")
|
86
|
+
expect(@english.translate('You have {count||message}.', :count => 1)).to eq("You have 1 message.")
|
87
|
+
expect(@english.translate('You have {count||message}.', :count => 5)).to eq("You have 5 messages.")
|
88
|
+
|
89
|
+
load_translation_key_from_hash(@app, {
|
90
|
+
"label" => "You have {count||message}.",
|
91
|
+
"translations" => {
|
92
|
+
"ru" => [
|
93
|
+
{
|
94
|
+
"label"=> "У вас есть {count|| one: сообщение, few: сообщения, other: сообщений}."
|
95
|
+
}
|
96
|
+
]
|
97
|
+
}
|
98
|
+
})
|
99
|
+
|
100
|
+
expect(@russian.translate('You have {count||message}.', :count => 0)).to eq("У вас есть 0 сообщений.")
|
101
|
+
expect(@russian.translate('You have {count||message}.', :count => 1)).to eq("У вас есть 1 сообщение.")
|
102
|
+
expect(@russian.translate('You have {count||message}.', :count => 2)).to eq("У вас есть 2 сообщения.")
|
103
|
+
expect(@russian.translate('You have {count||message}.', :count => 23)).to eq("У вас есть 23 сообщения.")
|
104
|
+
expect(@russian.translate('You have {count||message}.', :count => 5)).to eq("У вас есть 5 сообщений.")
|
105
|
+
expect(@russian.translate('You have {count||message}.', :count => 15)).to eq("У вас есть 15 сообщений.")
|
106
|
+
|
107
|
+
|
108
|
+
# Shorter form
|
109
|
+
|
110
|
+
load_translation_key_from_hash(@app, {
|
111
|
+
"label" => "You have {count||message}.",
|
112
|
+
"translations" => {
|
113
|
+
"ru" => [
|
114
|
+
{
|
115
|
+
"label"=> "У вас есть {count|| сообщение, сообщения, сообщений}."
|
116
|
+
}
|
117
|
+
]
|
118
|
+
}
|
119
|
+
})
|
120
|
+
|
121
|
+
expect(@russian.translate('You have {count||message}.', :count => 0)).to eq("У вас есть 0 сообщений.")
|
122
|
+
expect(@russian.translate('You have {count||message}.', :count => 1)).to eq("У вас есть 1 сообщение.")
|
123
|
+
expect(@russian.translate('You have {count||message}.', :count => 2)).to eq("У вас есть 2 сообщения.")
|
124
|
+
expect(@russian.translate('You have {count||message}.', :count => 23)).to eq("У вас есть 23 сообщения.")
|
125
|
+
expect(@russian.translate('You have {count||message}.', :count => 5)).to eq("У вас есть 5 сообщений.")
|
126
|
+
expect(@russian.translate('You have {count||message}.', :count => 15)).to eq("У вас есть 15 сообщений.")
|
127
|
+
|
128
|
+
# Alternative form
|
129
|
+
|
130
|
+
load_translation_key_from_hash(@app, {
|
131
|
+
"label" => "You have {count||message}.",
|
132
|
+
"translations" => {
|
133
|
+
"ru" => [
|
134
|
+
{
|
135
|
+
"label"=> "У вас есть {count} сообщение.",
|
136
|
+
"context"=> {
|
137
|
+
"count"=> { "number" => "one"}
|
138
|
+
}
|
139
|
+
},
|
140
|
+
{
|
141
|
+
"label"=> "У вас есть {count} сообщения.",
|
142
|
+
"context"=> {
|
143
|
+
"count"=> { "number" => "few"}
|
144
|
+
}
|
145
|
+
},
|
146
|
+
{
|
147
|
+
"label"=> "У вас есть {count} сообщений.",
|
148
|
+
"context"=> {
|
149
|
+
"count"=> { "number" => "many"}
|
150
|
+
}
|
151
|
+
}
|
152
|
+
]
|
153
|
+
}
|
154
|
+
})
|
155
|
+
|
156
|
+
expect(@russian.translate('You have {count||message}.', :count => 0)).to eq("У вас есть 0 сообщений.")
|
157
|
+
expect(@russian.translate('You have {count||message}.', :count => 1)).to eq("У вас есть 1 сообщение.")
|
158
|
+
expect(@russian.translate('You have {count||message}.', :count => 2)).to eq("У вас есть 2 сообщения.")
|
159
|
+
expect(@russian.translate('You have {count||message}.', :count => 23)).to eq("У вас есть 23 сообщения.")
|
160
|
+
expect(@russian.translate('You have {count||message}.', :count => 5)).to eq("У вас есть 5 сообщений.")
|
161
|
+
expect(@russian.translate('You have {count||message}.', :count => 15)).to eq("У вас есть 15 сообщений.")
|
162
|
+
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
describe "gender tokens" do
|
168
|
+
it "should return correct translations" do
|
169
|
+
Tr8n.session.with_block_options(:dry => true) do
|
170
|
+
|
171
|
+
female = {:gender => :female, :name => "Anna"}
|
172
|
+
male = {:gender => :male, :name => "Michael"}
|
173
|
+
|
174
|
+
expect(@english.translate('{user} updated {user| male: his, female: her} profile.', :user => {:object => female, :attribute => :name})).to eq("Anna updated her profile.")
|
175
|
+
expect(@english.translate('{user} updated {user| his, her} profile.', :user => {:object => female, :attribute => :name})).to eq("Anna updated her profile.")
|
176
|
+
|
177
|
+
expect(@english.translate('{user} updated {user| male: his, female: her} profile.', :user => {:object => male, :attribute => :name})).to eq("Michael updated his profile.")
|
178
|
+
expect(@english.translate('{user} updated {user| his, her} profile.', :user => {:object => male, :attribute => :name})).to eq("Michael updated his profile.")
|
179
|
+
|
180
|
+
load_translation_key_from_hash(@app, {
|
181
|
+
"label" => "{user} updated {user| his, her} profile.",
|
182
|
+
"translations" => {
|
183
|
+
"ru" => [
|
184
|
+
{
|
185
|
+
"label"=> "{user|| male: обновил, female: обновила} свой профиль."
|
186
|
+
}
|
187
|
+
]
|
188
|
+
}
|
189
|
+
})
|
190
|
+
|
191
|
+
expect(@russian.translate('{user} updated {user| his, her} profile.', :user => {:object => female, :attribute => :name})).to eq("Anna обновила свой профиль.")
|
192
|
+
expect(@russian.translate('{user} updated {user| his, her} profile.', :user => {:object => male, :attribute => :name})).to eq("Michael обновил свой профиль.")
|
193
|
+
|
194
|
+
load_translation_key_from_hash(@app, {
|
195
|
+
"label" => "{user} updated {user| his, her} profile.",
|
196
|
+
"translations" => {
|
197
|
+
"ru" => [
|
198
|
+
{
|
199
|
+
"label"=> "{user|| обновил, обновила} свой профиль."
|
200
|
+
}
|
201
|
+
]
|
202
|
+
}
|
203
|
+
})
|
204
|
+
|
205
|
+
expect(@russian.translate('{user} updated {user| his, her} profile.', :user => {:object => female, :attribute => :name})).to eq("Anna обновила свой профиль.")
|
206
|
+
expect(@russian.translate('{user} updated {user| his, her} profile.', :user => {:object => male, :attribute => :name})).to eq("Michael обновил свой профиль.")
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe "gender tokens with language cases" do
|
212
|
+
it "should return correct translations" do
|
213
|
+
Tr8n.session.with_block_options(:dry => true) do
|
214
|
+
|
215
|
+
female = {:gender => :female, :name => "Anna"}
|
216
|
+
male = {:gender => :male, :name => "Michael"}
|
217
|
+
|
218
|
+
expect(@english.translate('You like {user::pos} post.', :user => {:object => male, :attribute => :name})).to eq("You like Michael's post.")
|
219
|
+
|
220
|
+
expect(@english.translate('{actor} liked {target::pos} post.', :actor => {:object => female, :attribute => :name}, :target => {:object => male, :attribute => :name})).to eq("Anna liked Michael's post.")
|
221
|
+
|
222
|
+
female = {:gender => :female, :name => "Анна"}
|
223
|
+
male = {:gender => :male, :name => "Михаил"}
|
224
|
+
|
225
|
+
|
226
|
+
load_translation_key_from_hash(@app, {
|
227
|
+
"label" => "{actor} liked {target::pos} post.",
|
228
|
+
"translations" => {
|
229
|
+
"ru" => [
|
230
|
+
{
|
231
|
+
"label"=> "{actor::dat} понравилось сообщение {target::gen}."
|
232
|
+
}
|
233
|
+
]
|
234
|
+
}
|
235
|
+
})
|
236
|
+
|
237
|
+
expect(@russian.translate('{actor} liked {target::pos} post.', :actor => {:object => female, :attribute => :name}, :target => {:object => male, :attribute => :name})).to eq("Анне понравилось сообщение Михаила.")
|
238
|
+
expect(@russian.translate('{actor} liked {target::pos} post.', :actor => {:object => male, :attribute => :name}, :target => {:object => female, :attribute => :name})).to eq("Михаилу понравилось сообщение Анны.")
|
239
|
+
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
describe "decoration tokens" do
|
245
|
+
it "should return correct translations" do
|
246
|
+
Tr8n.session.with_block_options(:dry => true) do
|
247
|
+
|
248
|
+
expect(@english.translate("[bold: This text] should be bold", :bold => lambda{|text| "<strong>#{text}</strong>"})).to eq("<strong>This text</strong> should be bold")
|
249
|
+
|
250
|
+
load_translation_key_from_hash(@app, {
|
251
|
+
"label" => "[bold: This text] should be bold",
|
252
|
+
"translations" => {
|
253
|
+
"ru" => [
|
254
|
+
{
|
255
|
+
"label"=> "[bold: Этот текст] должны быть жирным"
|
256
|
+
}
|
257
|
+
]
|
258
|
+
}
|
259
|
+
})
|
260
|
+
|
261
|
+
expect(@russian.translate("[bold: This text] should be bold", :bold => lambda{|text| "<strong>#{text}</strong>"})).to eq("<strong>Этот текст</strong> должны быть жирным")
|
262
|
+
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
describe "nested decoration tokens" do
|
268
|
+
it "should return correct translations" do
|
269
|
+
Tr8n.session.with_block_options(:dry => true) do
|
270
|
+
|
271
|
+
expect(@english.translate("[bold: Bold text [italic: with italic text]] together", :bold => "<b>{$0}</b>", :italic => "<i>{$0}</i>")).to eq("<b>Bold text <i>with italic text</i></b> together")
|
272
|
+
|
273
|
+
load_translation_key_from_hash(@app, {
|
274
|
+
"label" => "[bold: Bold text [italic: with italic text]] together",
|
275
|
+
"translations" => {
|
276
|
+
"ru" => [
|
277
|
+
{
|
278
|
+
"label"=> "[bold: Жирный текст [italic: с курсив]] вместе"
|
279
|
+
}
|
280
|
+
]
|
281
|
+
}
|
282
|
+
})
|
283
|
+
|
284
|
+
expect(@russian.translate("[bold: Bold text [italic: with italic text]] together", :bold => "<b>{$0}</b>", :italic => "<i>{$0}</i>")).to eq("<b>Жирный текст <i>с курсив</i></b> вместе")
|
285
|
+
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
|
291
|
+
describe "nested decoration tokens with data tokens" do
|
292
|
+
it "should return correct translations" do
|
293
|
+
Tr8n.session.with_block_options(:dry => true) do
|
294
|
+
|
295
|
+
user = {:gender => :male, :name => "Michael"}
|
296
|
+
|
297
|
+
expect(@english.translate("[bold: {user}] received [link: [bold: {count}] {count|message}]", :user => {:object => user, :attribute => :name}, :bold => "<b>{$0}</b>", :count => 1, :link => "<a href='url'>{$0}</a>")).to eq("<b>Michael</b> received <a href='url'><b>1</b> message</a>")
|
298
|
+
expect(@english.translate("[bold: {user}] received [link: [bold: {count}] {count|message}]", :user => {:object => user, :attribute => :name}, :bold => "<b>{$0}</b>", :count => 5, :link => "<a href='url'>{$0}</a>")).to eq("<b>Michael</b> received <a href='url'><b>5</b> messages</a>")
|
299
|
+
|
300
|
+
load_translation_key_from_hash(@app, {
|
301
|
+
"label" => "[bold: {user}] received [link: [bold: {count}] {count|message}]",
|
302
|
+
"translations" => {
|
303
|
+
"ru" => [
|
304
|
+
{
|
305
|
+
"label"=> "[bold: {user}] {user| получил, получила} <a href='url'><b>{count}</b> {count| сообщение, сообщения, сообщений}</a>"
|
306
|
+
}
|
307
|
+
]
|
308
|
+
}
|
309
|
+
})
|
310
|
+
|
311
|
+
expect(@russian.translate("[bold: {user}] received [link: [bold: {count}] {count|message}]", :user => {:object => user, :attribute => :name}, :bold => "<b>{$0}</b>", :count => 1, :link => "<a href='url'>{$0}</a>")).to eq("<b>Michael</b> получил <a href='url'><b>1</b> сообщение</a>")
|
312
|
+
expect(@russian.translate("[bold: {user}] received [link: [bold: {count}] {count|message}]", :user => {:object => user, :attribute => :name}, :bold => "<b>{$0}</b>", :count => 2, :link => "<a href='url'>{$0}</a>")).to eq("<b>Michael</b> получил <a href='url'><b>2</b> сообщения</a>")
|
313
|
+
expect(@russian.translate("[bold: {user}] received [link: [bold: {count}] {count|message}]", :user => {:object => user, :attribute => :name}, :bold => "<b>{$0}</b>", :count => 5, :link => "<a href='url'>{$0}</a>")).to eq("<b>Michael</b> получил <a href='url'><b>5</b> сообщений</a>")
|
314
|
+
|
315
|
+
user = {:gender => :female, :name => "Anna"}
|
316
|
+
|
317
|
+
expect(@russian.translate("[bold: {user}] received [link: [bold: {count}] {count|message}]", :user => {:object => user, :attribute => :name}, :bold => "<b>{$0}</b>", :count => 1, :link => "<a href='url'>{$0}</a>")).to eq("<b>Anna</b> получила <a href='url'><b>1</b> сообщение</a>")
|
318
|
+
expect(@russian.translate("[bold: {user}] received [link: [bold: {count}] {count|message}]", :user => {:object => user, :attribute => :name}, :bold => "<b>{$0}</b>", :count => 2, :link => "<a href='url'>{$0}</a>")).to eq("<b>Anna</b> получила <a href='url'><b>2</b> сообщения</a>")
|
319
|
+
expect(@russian.translate("[bold: {user}] received [link: [bold: {count}] {count|message}]", :user => {:object => user, :attribute => :name}, :bold => "<b>{$0}</b>", :count => 5, :link => "<a href='url'>{$0}</a>")).to eq("<b>Anna</b> получила <a href='url'><b>5</b> сообщений</a>")
|
320
|
+
|
321
|
+
end
|
322
|
+
end
|
11
323
|
end
|
324
|
+
|
325
|
+
|
326
|
+
describe "date translations" do
|
327
|
+
it "should return correct translations" do
|
328
|
+
Tr8n.session.with_block_options(:dry => true) do
|
329
|
+
|
330
|
+
date = Date.new(2011, 01, 01)
|
331
|
+
expect(@english.translate("This message was received on {date}", :date => date)).to eq("This message was received on 2011-01-01")
|
332
|
+
|
333
|
+
expect(date.translate()).to eq("1/1/2011")
|
334
|
+
expect(date.tr()).to eq("1/1/2011")
|
335
|
+
expect(@english.translate("This message was received on {date}", :date => date.tr())).to eq("This message was received on 1/1/2011")
|
336
|
+
|
337
|
+
expect(date.tr(:verbose)).to eq("Saturday, January 1, 2011")
|
338
|
+
expect(@english.translate("This message was received on {date}", :date => date.tr(:verbose))).to eq("This message was received on Saturday, January 1, 2011")
|
339
|
+
|
340
|
+
Tr8n.config.localization[:custom_date_formats][:short_numeric] = '%m/%d'
|
341
|
+
expect(@english.translate("This message was received on {date}", :date => date.tr(:short_numeric))).to eq("This message was received on 1/1")
|
342
|
+
expect(@english.translate("This message was received on {date}", :date => date.tr("%B %d"))).to eq("This message was received on January 1")
|
343
|
+
expect(@english.translate("This message was received on {date}", :date => date.tr("{month_name} {days::ord}"))).to eq("This message was received on January 1st")
|
344
|
+
|
345
|
+
expect(@english.translate("This message was received on {date}", :date => date.tr("{month_name} {days}"))).to eq("This message was received on January 1")
|
346
|
+
|
347
|
+
load_translation_key_from_array(@app, [{
|
348
|
+
"label" => "{month_name} {days}",
|
349
|
+
"translations" => {
|
350
|
+
"ru" => [
|
351
|
+
{
|
352
|
+
"label"=> "{days}ого {month_name::gen}"
|
353
|
+
}
|
354
|
+
]
|
355
|
+
}
|
356
|
+
}, {
|
357
|
+
"label" => "This message was received on {date}",
|
358
|
+
"translations" => {
|
359
|
+
"ru" => [
|
360
|
+
{
|
361
|
+
"label"=> "Это сообщение было получено {date}"
|
362
|
+
}
|
363
|
+
]
|
364
|
+
}
|
365
|
+
}, {
|
366
|
+
"label" => "January",
|
367
|
+
"description" => "Month name",
|
368
|
+
"translations" => {
|
369
|
+
"ru" => [
|
370
|
+
{
|
371
|
+
"label"=> "Январь"
|
372
|
+
}
|
373
|
+
]
|
374
|
+
}
|
375
|
+
}])
|
376
|
+
|
377
|
+
expect(@russian.translate("January", "Month name")).to eq("Январь")
|
378
|
+
expect(@russian.translate(Tr8n.config.default_month_name(0), "Month name")).to eq("Январь")
|
379
|
+
expect(@russian.translate("This message was received on {date}", :date => date.tr("{month_name} {days}", @russian))).to eq("Это сообщение было получено 1ого Января")
|
380
|
+
end
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
|
385
|
+
describe "time translations" do
|
386
|
+
it "should return correct translations" do
|
387
|
+
Tr8n.session.with_block_options(:dry => true) do
|
388
|
+
|
389
|
+
time = Time.new(2014, 01, 02, 11, 12, 13)
|
390
|
+
expect(@english.translate("This message was received at {time}", :time => time)).to eq("This message was received at 2014-01-02 11:12:13 -0800")
|
391
|
+
|
392
|
+
|
393
|
+
expect(time.translate()).to eq("1/2/2014")
|
394
|
+
expect(time.tr()).to eq("1/2/2014")
|
395
|
+
expect(@english.translate("This message was received on {time}", :time => time.tr(:date_time))).to eq("This message was received on 1/2/2014 at 11:12")
|
396
|
+
|
397
|
+
expect(@english.translate("This message was received on {time}", :time => time.tr("{month_name} {days::ord} at {short_hours}:{minutes} {am_pm}"))).to eq("This message was received on January 2nd at 11:12 AM")
|
398
|
+
|
399
|
+
load_translation_key_from_array(@app, [{
|
400
|
+
"label" => "{month_name} {days::ord} at {short_hours}:{minutes} {am_pm}",
|
401
|
+
"translations" => {
|
402
|
+
"ru" => [
|
403
|
+
{
|
404
|
+
"label"=> "{days}ого {month_name::gen} в {short_hours}:{minutes} {am_pm}"
|
405
|
+
}
|
406
|
+
]
|
407
|
+
}
|
408
|
+
}, {
|
409
|
+
"label" => "This message was received on {time}",
|
410
|
+
"translations" => {
|
411
|
+
"ru" => [
|
412
|
+
{
|
413
|
+
"label"=> "Это сообщение было получено {time}"
|
414
|
+
}
|
415
|
+
]
|
416
|
+
}
|
417
|
+
}, {
|
418
|
+
"label" => "January",
|
419
|
+
"description" => "Month name",
|
420
|
+
"translations" => {
|
421
|
+
"ru" => [
|
422
|
+
{
|
423
|
+
"label"=> "Январь"
|
424
|
+
}
|
425
|
+
]
|
426
|
+
}
|
427
|
+
}, {
|
428
|
+
"label" => "AM",
|
429
|
+
"description" => "Meridian indicator",
|
430
|
+
"translations" => {
|
431
|
+
"ru" => [
|
432
|
+
{
|
433
|
+
"label"=> "бп"
|
434
|
+
}
|
435
|
+
]
|
436
|
+
}
|
437
|
+
} , {
|
438
|
+
"label" => "PM",
|
439
|
+
"description" => "Meridian indicator",
|
440
|
+
"translations" => {
|
441
|
+
"ru" => [
|
442
|
+
{
|
443
|
+
"label"=> "пп"
|
444
|
+
}
|
445
|
+
]
|
446
|
+
}
|
447
|
+
}])
|
448
|
+
|
449
|
+
expect(@russian.translate("This message was received on {time}", :time => time.tr("{month_name} {days::ord} at {short_hours}:{minutes} {am_pm}", @russian))).to eq("Это сообщение было получено 2ого Января в 11:12 бп")
|
450
|
+
end
|
451
|
+
end
|
452
|
+
end
|
453
|
+
|
454
|
+
|
12
455
|
end
|
13
456
|
|
14
457
|
|
458
|
+
describe "#translation" do
|
459
|
+
#before do
|
460
|
+
# @@app = init_application
|
461
|
+
# @@english = @app.language('en-US')
|
462
|
+
# @@russian = @app.language('ru')
|
463
|
+
#end
|
464
|
+
#
|
465
|
+
#it "translates with fallback to English" do
|
466
|
+
# Tr8n.session.with_block_options(:dry => true) do
|
467
|
+
# #expect(@@russian.translate("{count||message}", {:count => 1})).to eq("1 message")
|
468
|
+
# #expect(@@russian.translate("{count||message}", {:count => 5})).to eq("5 messages")
|
469
|
+
# #expect(@@russian.translate("{count||message}", {:count => 0})).to eq("0 messages")
|
470
|
+
# end
|
471
|
+
#end
|
15
472
|
|
473
|
+
# it "translates basic phrases to Russian" do
|
474
|
+
# load_translation_keys_from_file(@app, 'translations/ru/basic.json')
|
475
|
+
# Tr8n.config.with_block_options(:dry => true) do
|
476
|
+
# expect(@@russian.translate("Hello World")).to eq("Привет Мир")
|
477
|
+
# expect(@@russian.translate("Hello World", "Wrong context")).to eq("Hello World")
|
478
|
+
# expect(@@russian.translate("Hello World", "Greeting context")).to eq("Привет Мир")
|
479
|
+
# expect(@@russian.translate("Hello world")).to eq("Hello world")
|
480
|
+
# expect(@@russian.translate("Hello {user}", nil, :user => "Михаил")).to eq("Привет Михаил")
|
481
|
+
# end
|
482
|
+
# end
|
483
|
+
#
|
484
|
+
# it "translates basic phrases with data tokens to Russian" do
|
485
|
+
# load_translation_keys_from_file(@app, 'translations/ru/basic.json')
|
486
|
+
# Tr8n.config.with_block_options(:dry => true) do
|
487
|
+
# expect(@@russian.translate("Hello {user}", nil, :user => "Михаил")).to eq("Привет Михаил")
|
488
|
+
# end
|
489
|
+
# end
|
490
|
+
#
|
491
|
+
# it "uses default data tokens" do
|
492
|
+
# Tr8n.config.with_block_options(:dry => true) do
|
493
|
+
# expect(@english.translate("He said: {quot}Hello{quot}", nil)).to eq("He said: "Hello"")
|
494
|
+
# expect(@english.translate("Code sample: {lbrace}a:'b'{rbrace}", nil)).to eq("Code sample: {a:'b'}")
|
495
|
+
# end
|
496
|
+
# end
|
497
|
+
#
|
498
|
+
# it "uses basic decoration tokens" do
|
499
|
+
# Tr8n.config.with_block_options(:dry => true) do
|
500
|
+
# expect(@english.translate("Hello [decor: World]", nil, :decor => lambda{|text| "''#{text}''"})).to eq("Hello ''World''")
|
501
|
+
# end
|
502
|
+
# end
|
503
|
+
#
|
504
|
+
# it "uses default decoration tokens" do
|
505
|
+
# load_translation_keys_from_file(@app, 'translations/ru/basic.json')
|
506
|
+
# Tr8n.config.with_block_options(:dry => true) do
|
507
|
+
# expect(@english.translate("Hello [i: World]")).to eq("Hello <i>World</i>")
|
508
|
+
# expect(@@russian.translate("Hello [i: World]")).to eq("Привет <i>Мир</i>")
|
509
|
+
# end
|
510
|
+
# end
|
511
|
+
#
|
512
|
+
# it "uses mixed tokens" do
|
513
|
+
# load_translation_keys_from_file(@app, 'translations/ru/basic.json')
|
514
|
+
# Tr8n.config.with_block_options(:dry => true) do
|
515
|
+
# expect(@english.translate("Hello [i: {user}]", nil, :user => "Michael")).to eq("Hello <i>Michael</i>")
|
516
|
+
# expect(@@russian.translate("Hello [i: {user}]", nil, :user => "Michael")).to eq("Привет <i>Michael</i>")
|
517
|
+
# end
|
518
|
+
# end
|
519
|
+
#
|
520
|
+
# it "uses method tokens" do
|
521
|
+
# load_translation_keys_from_file(@app, 'translations/ru/basic.json')
|
522
|
+
# Tr8n.config.with_block_options(:dry => true) do
|
523
|
+
# expect(@@russian.translate("Hello {user.first_name} [i: {user.last_name}]", nil,
|
524
|
+
# :user => stub_object({:first_name => "Tom", :last_name => "Anderson"}))).to eq("Привет Tom <i>Anderson</i>")
|
525
|
+
# end
|
526
|
+
# end
|
527
|
+
#
|
528
|
+
# it "translates phrases with numeric rules to Russian" do
|
529
|
+
# load_translation_keys_from_file(@app, 'translations/ru/counters.json')
|
530
|
+
# trn = @@russian.translate("{count||message}", nil, {:count => 1})
|
531
|
+
# expect(trn).to eq("1 сообщение")
|
532
|
+
# trn = @@russian.translate("{count||message}", nil, {:count => 2})
|
533
|
+
# expect(trn).to eq("2 сообщения")
|
534
|
+
# trn = @@russian.translate("{count||message}", nil, {:count => 5})
|
535
|
+
# expect(trn).to eq("5 сообщений")
|
536
|
+
# trn = @@russian.translate("{count||message}", nil, {:count => 15})
|
537
|
+
# expect(trn).to eq("15 сообщений")
|
538
|
+
# end
|
539
|
+
#
|
540
|
+
# it "translates phrases with gender rules to Russian" do
|
541
|
+
# #load_translation_key_from_hash(@app, {
|
542
|
+
# # "label" => "{actor} sent {target} a gift.",
|
543
|
+
# # "translations" => {
|
544
|
+
# # "ru" => [
|
545
|
+
# # {
|
546
|
+
# # "label"=> "{actor} послал подарок {target::dat}.",
|
547
|
+
# # "locale"=> "ru",
|
548
|
+
# # "context"=> {
|
549
|
+
# # "actor"=> [{ "type"=> "gender", "key"=> "male"}]
|
550
|
+
# # }
|
551
|
+
# # },
|
552
|
+
# # {
|
553
|
+
# # "label"=> "{actor} послала подарок {target::dat}.",
|
554
|
+
# # "locale"=> "ru",
|
555
|
+
# # "context"=> {
|
556
|
+
# # "actor"=> [{ "type"=> "gender", "key"=> "female"}]
|
557
|
+
# # },
|
558
|
+
# # },
|
559
|
+
# # {
|
560
|
+
# # "label"=> "{actor} послал/а подарок {target::dat}.",
|
561
|
+
# # "locale"=> "ru",
|
562
|
+
# # "context"=> {
|
563
|
+
# # "actor"=> [{ "type"=> "gender", "key"=> "unknown"}]
|
564
|
+
# # },
|
565
|
+
# # }
|
566
|
+
# # ]
|
567
|
+
# # }
|
568
|
+
# #});
|
569
|
+
#
|
570
|
+
# load_translation_keys_from_file(@app, "translations/ru/genders.json")
|
571
|
+
#
|
572
|
+
# actor = {'gender' => 'female', 'name' => 'Таня'}
|
573
|
+
# target = {'gender' => 'male', 'name' => 'Михаил'}
|
574
|
+
#
|
575
|
+
# Tr8n.config.with_block_options(:dry => true) do
|
576
|
+
# expect(@@russian.translate(
|
577
|
+
# '{actor} sent {target} a gift.', nil,
|
578
|
+
# :actor => {:object => actor, :attribute => 'name'},
|
579
|
+
# :target => {:object => target, :attribute => 'name'})
|
580
|
+
# ).to eq("Таня послала подарок Михаилу.")
|
581
|
+
#
|
582
|
+
# expect(@@russian.translate(
|
583
|
+
# '{actor} sent {target} a gift.', nil,
|
584
|
+
# :actor => {:object => target, :attribute => 'name'},
|
585
|
+
# :target => {:object => actor, :attribute => 'name'})
|
586
|
+
# ).to eq("Михаил послал подарок Тане.")
|
587
|
+
#
|
588
|
+
# expect(@@russian.translate(
|
589
|
+
# '{actor} loves {target}.', nil,
|
590
|
+
# :actor => {:object => actor, :attribute => 'name'},
|
591
|
+
# :target => {:object => target, :attribute => 'name'})
|
592
|
+
# ).to eq("Таня любит Михаила.")
|
593
|
+
#
|
594
|
+
# expect(@@russian.translate(
|
595
|
+
# '{actor} saw {target} {count||day} ago.', nil,
|
596
|
+
# :actor => {:object => actor, :attribute => 'name'},
|
597
|
+
# :target => {:object => target, :attribute => 'name'},
|
598
|
+
# :count => 2)
|
599
|
+
# ).to eq("Таня видела Михаила 2 дня назад.")
|
600
|
+
#
|
601
|
+
# expect(@@russian.translate(
|
602
|
+
# '{actor} saw {target} {count||day} ago.', nil,
|
603
|
+
# :actor => {:object => target, :attribute => 'name'},
|
604
|
+
# :target => {:object => actor, :attribute => 'name'},
|
605
|
+
# :count => 2)
|
606
|
+
# ).to eq("Михаил видел Таню 2 дня назад.")
|
607
|
+
#
|
608
|
+
# end
|
609
|
+
#
|
610
|
+
# # trn = @@russian.translate("{count||message}", nil, {:count => 1})
|
611
|
+
# # expect(trn).to eq("1 сообщение")
|
612
|
+
# end
|
613
|
+
#
|
614
|
+
end
|
16
615
|
end
|