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
|
# Copyright (c) 2005-2006 David Barri
$LOAD_PATH.push File.join(File.dirname(__FILE__),'..','lib')
require 'gloc'
require 'gloc-ruby'
require 'gloc-config'
require 'gloc-rails-text'
require File.join(File.dirname(__FILE__),'lib','rails-time_ext') unless 3.respond_to?(:days)
require File.join(File.dirname(__FILE__),'lib','rails-string_ext') unless ''.respond_to?(:starts_with?)
#require 'gloc-dev'
class LClass; include GLoc; end
class LClass2 < LClass; end
class LClass_en < LClass2; set_language :en; end
class LClass_ja < LClass2; set_language :ja; end
# class LClass_forced_au < LClass; set_language :en; force_language :en_AU; set_language :ja; end
class GLocTest < Test::Unit::TestCase
include GLoc
include ActionView::Helpers::DateHelper
def setup
@l1 = LClass.new
@l2 = LClass.new
@l3 = LClass.new
@l1.set_language :ja
@l2.set_language :en
@l3.set_language 'en_AU'
@gloc_state= GLoc.backup_state true
GLoc::CONFIG.merge!({
:default_param_name => 'lang',
:default_cookie_name => 'lang',
:default_language => :ja,
:raise_string_not_found_errors => true,
:verbose => false,
})
end
def teardown
GLoc.restore_state @gloc_state
end
#---------------------------------------------------------------------------
def test_basic
assert_localized_value [nil, @l1, @l2, @l3], nil, :in_both_langs
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'lang')
assert_localized_value [nil, @l1], 'enにもjaにもある', :in_both_langs
assert_localized_value [nil, @l1], '日本語のみ', :ja_only
assert_localized_value [nil, @l1], nil, :en_only
assert_localized_value @l2, 'This is in en+ja', :in_both_langs
assert_localized_value @l2, nil, :ja_only
assert_localized_value @l2, 'English only', :en_only
assert_localized_value @l3, "Thiz in en 'n' ja", :in_both_langs
assert_localized_value @l3, nil, :ja_only
assert_localized_value @l3, 'Aussie English only bro', :en_only
@l3.set_language :en
assert_localized_value @l3, 'This is in en+ja', :in_both_langs
assert_localized_value @l3, nil, :ja_only
assert_localized_value @l3, 'English only', :en_only
assert_localized_value nil, 'enにもjaにもある', :in_both_langs
assert_localized_value nil, '日本語のみ', :ja_only
assert_localized_value nil, nil, :en_only
end
def test_load_twice_with_override
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'lang')
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'lang2')
assert_localized_value [nil, @l1], '更新された', :in_both_langs
assert_localized_value [nil, @l1], '日本語のみ', :ja_only
assert_localized_value [nil, @l1], nil, :en_only
assert_localized_value [nil, @l1], nil, :new_en
assert_localized_value [nil, @l1], '新たな日本語ストリング', :new_ja
assert_localized_value @l2, 'This is in en+ja', :in_both_langs
assert_localized_value @l2, nil, :ja_only
assert_localized_value @l2, 'overriden dude', :en_only
assert_localized_value @l2, 'This is a new English string', :new_en
assert_localized_value @l2, nil, :new_ja
assert_localized_value @l3, "Thiz in en 'n' ja", :in_both_langs
assert_localized_value @l3, nil, :ja_only
assert_localized_value @l3, 'Aussie English only bro', :en_only
end
def test_load_twice_without_override
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'lang')
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'lang2'), false
assert_localized_value [nil, @l1], 'enにもjaにもある', :in_both_langs
assert_localized_value [nil, @l1], '日本語のみ', :ja_only
assert_localized_value [nil, @l1], nil, :en_only
assert_localized_value [nil, @l1], nil, :new_en
assert_localized_value [nil, @l1], '新たな日本語ストリング', :new_ja
assert_localized_value @l2, 'This is in en+ja', :in_both_langs
assert_localized_value @l2, nil, :ja_only
assert_localized_value @l2, 'English only', :en_only
assert_localized_value @l2, 'This is a new English string', :new_en
assert_localized_value @l2, nil, :new_ja
assert_localized_value @l3, "Thiz in en 'n' ja", :in_both_langs
assert_localized_value @l3, nil, :ja_only
assert_localized_value @l3, 'Aussie English only bro', :en_only
end
def test_add_localized_strings
assert_localized_value nil, nil, :add
assert_localized_value nil, nil, :ja_only
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'lang')
assert_localized_value nil, nil, :add
assert_localized_value nil, '日本語のみ', :ja_only
GLoc.add_localized_strings 'en', {:ja_only => 'bullshit'}, true
GLoc.add_localized_strings 'en', {:ja_only => 'bullshit'}, false
assert_localized_value nil, nil, :add
assert_localized_value nil, '日本語のみ', :ja_only
GLoc.add_localized_strings 'ja', {:ja_only => 'bullshit', :add => '123'}, false
assert_localized_value nil, '123', :add
assert_localized_value nil, '日本語のみ', :ja_only
GLoc.add_localized_strings 'ja', {:ja_only => 'bullshit', :add => '234'}
assert_localized_value nil, '234', :add
assert_localized_value nil, 'bullshit', :ja_only
end
def test_class_set_language
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'lang')
@l1 = LClass_ja.new
@l2 = LClass_en.new
@l3 = LClass_en.new
assert_localized_value @l1, 'enにもjaにもある', :in_both_langs
assert_localized_value @l2, 'This is in en+ja', :in_both_langs
assert_localized_value @l3, 'This is in en+ja', :in_both_langs
@l3.set_language 'en_AU'
assert_localized_value @l1, 'enにもjaにもある', :in_both_langs
assert_localized_value @l2, 'This is in en+ja', :in_both_langs
assert_localized_value @l3, "Thiz in en 'n' ja", :in_both_langs
end
def test_ll
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'lang')
assert_equal 'enにもjaにもある', ll('ja',:in_both_langs)
assert_equal 'enにもjaにもある', GLoc::ll('ja',:in_both_langs)
assert_equal 'enにもjaにもある', LClass_en.ll('ja',:in_both_langs)
assert_equal 'enにもjaにもある', LClass_ja.ll('ja',:in_both_langs)
assert_equal 'This is in en+ja', ll('en',:in_both_langs)
assert_equal 'This is in en+ja', GLoc::ll('en',:in_both_langs)
assert_equal 'This is in en+ja', LClass_en.ll('en',:in_both_langs)
assert_equal 'This is in en+ja', LClass_ja.ll('en',:in_both_langs)
end
def test_lsym
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'lang')
assert_equal 'enにもjaにもある', LClass_ja.ltry(:in_both_langs)
assert_equal 'hello', LClass_ja.ltry('hello')
assert_equal nil, LClass_ja.ltry(nil)
end
# def test_forced
# assert_equal :en_AU, LClass_forced_au.current_language
# a= LClass_forced_au.new
# a.set_language :ja
# assert_equal :en_AU, a.current_language
# a.force_language :ja
# assert_equal :ja, a.current_language
# assert_equal :en_AU, LClass_forced_au.current_language
# end
def test_pluralization
GLoc.add_localized_strings :en, :_gloc_rule_default => %[|n| case n; when 0 then '_none'; when 1 then '_single'; else '_many'; end], :a_single => '%d man', :a_many => '%d men', :a_none => 'No men'
GLoc.add_localized_strings :en, :_gloc_rule_asd => %[|n| n<10 ? '_few' : '_heaps'], :a_few => 'a few men (%d)', :a_heaps=> 'soo many men'
set_language :en
assert_equal 'No men', lwr(:a, 0)
assert_equal '1 man', lwr(:a, 1)
assert_equal '3 men', lwr(:a, 3)
assert_equal '20 men', lwr(:a, 20)
assert_equal 'a few men (0)', lwr_(:asd, :a, 0)
assert_equal 'a few men (1)', lwr_(:asd, :a, 1)
assert_equal 'a few men (3)', lwr_(:asd, :a, 3)
assert_equal 'soo many men', lwr_(:asd, :a, 12)
assert_equal 'soo many men', lwr_(:asd, :a, 20)
end
def test_distance_in_words
load_default_strings
[
[20.seconds, 'less than a minute', '1分以内', 'меньше минуты'],
[80.seconds, '1 minute', '1分', '1 минуту'],
[3.seconds, 'less than 5 seconds', '5秒以内', 'менее 5 секунд', true],
[9.seconds, 'less than 10 seconds', '10秒以内', 'менее 10 секунд', true],
[16.seconds, 'less than 20 seconds', '20秒以内', 'менее 20 секунд', true],
[35.seconds, 'half a minute', '約30秒', 'полминуты', true],
[50.seconds, 'less than a minute', '1分以内', 'меньше минуты', true],
[1.1.minutes, '1 minute', '1分', '1 минуту'],
[2.1.minutes, '2 minutes', '2分', '2 минуты'],
[4.1.minutes, '4 minutes', '4分', '4 минуты'],
[5.1.minutes, '5 minutes', '5分', '5 минут'],
[1.1.hours, 'about an hour', '約1時間', 'около часа'],
[3.1.hours, 'about 3 hours', '約3時間', 'около 3 часов'],
[9.1.hours, 'about 9 hours', '約9時間', 'около 9 часов'],
[1.1.days, '1 day', '1日間', '1 день'],
[2.1.days, '2 days', '2日間', '2 дня'],
[4.days, '4 days', '4日間', '4 дня'],
[6.days, '6 days', '6日間', '6 дней'],
[11.days, '11 days', '11日間', '11 дней'],
[12.days, '12 days', '12日間', '12 дней'],
[15.days, '15 days', '15日間', '15 дней'],
[20.days, '20 days', '20日間', '20 дней'],
[21.days, '21 days', '21日間', '21 день'],
[22.days, '22 days', '22日間', '22 дня'],
[25.days, '25 days', '25日間', '25 дней'],
].each do |a|
t, en, ja, ru = a
inc_sec= (a.size == 5) ? a[-1] : false
set_language :en
assert_equal en, distance_of_time_in_words(t,0,inc_sec)
set_language :ja
assert_equal ja, distance_of_time_in_words(t,0,inc_sec)
set_language :ru
assert_equal ru, distance_of_time_in_words(t,0,inc_sec)
end
end
def test_age
load_default_strings
[
[1, '1 yr', '1歳', '1 год'],
[22, '22 yrs', '22歳', '22 года'],
[27, '27 yrs', '27歳', '27 лет'],
].each do |a, en, ja, ru|
set_language :en
assert_equal en, l_age(a)
set_language :ja
assert_equal ja, l_age(a)
set_language :ru
assert_equal ru, l_age(a)
end
end
def test_yesno
load_default_strings
set_language :en
assert_equal 'yes', l_yesno(true)
assert_equal 'no', l_yesno(false)
assert_equal 'Yes', l_YesNo(true)
assert_equal 'No', l_YesNo(false)
end
def test_all_languages_have_values_for_helpers
load_default_strings
t= Time.local(2000, 9, 15, 11, 23, 57)
GLoc.valid_languages.each {|l|
set_language l
0.upto(120) {|n| l_age(n)}
l_date(t)
l_datetime(t)
l_datetime_short(t)
l_time(t)
[true,false].each{|v| l_YesNo(v); l_yesno(v) }
}
end
def test_similar_languages
GLoc.add_localized_strings :en, :a => 'a'
GLoc.add_localized_strings :en_AU, :a => 'a'
GLoc.add_localized_strings :ja, :a => 'a'
GLoc.add_localized_strings :zh_tw, :a => 'a'
assert_equal :en, GLoc.similar_language(:en)
assert_equal :en, GLoc.similar_language('en')
assert_equal :ja, GLoc.similar_language(:ja)
assert_equal :ja, GLoc.similar_language('ja')
# lowercase + dashes to underscores
assert_equal :en, GLoc.similar_language('EN')
assert_equal :en, GLoc.similar_language(:EN)
assert_equal :en_AU, GLoc.similar_language(:EN_Au)
assert_equal :en_AU, GLoc.similar_language('eN-Au')
# remove dialect
assert_equal :ja, GLoc.similar_language(:ja_Au)
assert_equal :ja, GLoc.similar_language('JA-ASDF')
assert_equal :ja, GLoc.similar_language('jA_ASD_ZXC')
# different dialect
assert_equal :zh_tw, GLoc.similar_language('ZH')
assert_equal :zh_tw, GLoc.similar_language('ZH_HK')
assert_equal :zh_tw, GLoc.similar_language('ZH-BUL')
# non matching
assert_equal nil, GLoc.similar_language('WW')
assert_equal nil, GLoc.similar_language('WW_AU')
assert_equal nil, GLoc.similar_language('WW-AU')
assert_equal nil, GLoc.similar_language('eZ_en')
assert_equal nil, GLoc.similar_language('AU-ZH')
end
def test_clear_strings_and_similar_langs
GLoc.add_localized_strings :en, :a => 'a'
GLoc.add_localized_strings :en_AU, :a => 'a'
GLoc.add_localized_strings :ja, :a => 'a'
GLoc.add_localized_strings :zh_tw, :a => 'a'
GLoc.clear_strings :en, :ja
assert_equal nil, GLoc.similar_language('ja')
assert_equal :en_AU, GLoc.similar_language('en')
assert_equal :zh_tw, GLoc.similar_language('ZH_HK')
GLoc.clear_strings
assert_equal nil, GLoc.similar_language('ZH_HK')
end
def test_lang_name
GLoc.add_localized_strings :en, :general_lang_en => 'English', :general_lang_ja => 'Japanese'
GLoc.add_localized_strings :ja, :general_lang_en => '英語', :general_lang_ja => '日本語'
set_language :en
assert_equal 'Japanese', l_lang_name(:ja)
assert_equal 'English', l_lang_name('en')
set_language :ja
assert_equal '日本語', l_lang_name('ja')
assert_equal '英語', l_lang_name(:en)
end
def test_charset_change_all
load_default_strings
GLoc.add_localized_strings :ja2, :a => 'a'
GLoc.valid_languages # Force refresh if in dev mode
GLoc.class_eval 'LOCALIZED_STRINGS[:ja2]= LOCALIZED_STRINGS[:ja].clone'
[:ja, :ja2].each do |l|
set_language l
assert_equal 'はい', l_yesno(true)
assert_equal "E381AFE38184", l_yesno(true).unpack('H*')[0].upcase
end
GLoc.set_charset 'sjis'
assert_equal 'sjis', GLoc.get_charset(:ja)
assert_equal 'sjis', GLoc.get_charset(:ja2)
[:ja, :ja2].each do |l|
set_language l
assert_equal "82CD82A2", l_yesno(true).unpack('H*')[0].upcase
end
end
def test_charset_change_single
load_default_strings
GLoc.add_localized_strings :ja2, :a => 'a'
GLoc.add_localized_strings :ja3, :a => 'a'
GLoc.valid_languages # Force refresh if in dev mode
GLoc.class_eval 'LOCALIZED_STRINGS[:ja2]= LOCALIZED_STRINGS[:ja].clone'
GLoc.class_eval 'LOCALIZED_STRINGS[:ja3]= LOCALIZED_STRINGS[:ja].clone'
[:ja, :ja2, :ja3].each do |l|
set_language l
assert_equal 'はい', l_yesno(true)
assert_equal "E381AFE38184", l_yesno(true).unpack('H*')[0].upcase
end
GLoc.set_charset 'sjis', :ja
assert_equal 'sjis', GLoc.get_charset(:ja)
assert_equal 'utf-8', GLoc.get_charset(:ja2)
assert_equal 'utf-8', GLoc.get_charset(:ja3)
set_language :ja
assert_equal "82CD82A2", l_yesno(true).unpack('H*')[0].upcase
set_language :ja2
assert_equal "E381AFE38184", l_yesno(true).unpack('H*')[0].upcase
set_language :ja3
assert_equal "E381AFE38184", l_yesno(true).unpack('H*')[0].upcase
GLoc.set_charset 'euc-jp', :ja, :ja3
assert_equal 'euc-jp', GLoc.get_charset(:ja)
assert_equal 'utf-8', GLoc.get_charset(:ja2)
assert_equal 'euc-jp', GLoc.get_charset(:ja3)
set_language :ja
assert_equal "A4CFA4A4", l_yesno(true).unpack('H*')[0].upcase
set_language :ja2
assert_equal "E381AFE38184", l_yesno(true).unpack('H*')[0].upcase
set_language :ja3
assert_equal "A4CFA4A4", l_yesno(true).unpack('H*')[0].upcase
end
def test_set_language_if_valid
GLoc.add_localized_strings :en, :a => 'a'
GLoc.add_localized_strings :zh_tw, :a => 'a'
assert set_language_if_valid('en')
assert_equal :en, current_language
assert set_language_if_valid('zh_tw')
assert_equal :zh_tw, current_language
assert !set_language_if_valid(nil)
assert_equal :zh_tw, current_language
assert !set_language_if_valid('ja')
assert_equal :zh_tw, current_language
assert set_language_if_valid(:en)
assert_equal :en, current_language
end
#===========================================================================
protected
def assert_localized_value(objects,expected,key)
objects = [objects] unless objects.kind_of?(Array)
objects.each {|object|
o = object || GLoc
assert_equal !expected.nil?, o.l_has_string?(key)
if expected.nil?
assert_raise(GLoc::StringNotFoundError) {o.l(key)}
else
assert_equal expected, o.l(key)
end
}
end
def load_default_strings
GLoc.load_localized_strings File.join(File.dirname(__FILE__),'..','lang')
end
end
|