r18n-core 0.2.3 → 0.3

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.
Files changed (47) hide show
  1. data/README.rdoc +157 -35
  2. data/base/cs.yml +34 -0
  3. data/base/de.yml +24 -0
  4. data/base/en.yml +24 -0
  5. data/base/fr.yml +24 -0
  6. data/base/pl.yml +24 -0
  7. data/base/ru.yml +30 -0
  8. data/lib/r18n-core/filters.rb +245 -0
  9. data/lib/r18n-core/i18n.rb +57 -40
  10. data/lib/r18n-core/locale.rb +116 -19
  11. data/lib/r18n-core/translated.rb +186 -0
  12. data/lib/r18n-core/translation.rb +33 -73
  13. data/lib/r18n-core/unsupported_locale.rb +27 -9
  14. data/lib/r18n-core/utils.rb +49 -0
  15. data/lib/r18n-core/version.rb +1 -1
  16. data/lib/r18n-core.rb +3 -15
  17. data/locales/cs.rb +23 -0
  18. data/locales/cs.yml +26 -0
  19. data/locales/de.yml +3 -8
  20. data/locales/en-us.rb +8 -0
  21. data/locales/en-us.yml +9 -0
  22. data/locales/en.rb +26 -0
  23. data/locales/en.yml +3 -8
  24. data/locales/eo.yml +3 -8
  25. data/locales/fr.rb +14 -0
  26. data/locales/fr.yml +3 -8
  27. data/locales/kk.yml +3 -8
  28. data/locales/pl.yml +4 -11
  29. data/locales/ru.yml +3 -8
  30. data/spec/filters_spec.rb +167 -0
  31. data/spec/i18n_spec.rb +61 -16
  32. data/spec/locale_spec.rb +46 -19
  33. data/spec/locales/cs_spec.rb +22 -0
  34. data/spec/locales/en-us_spec.rb +14 -0
  35. data/spec/locales/en_spec.rb +14 -0
  36. data/spec/locales/fr_spec.rb +10 -0
  37. data/spec/locales/pl_spec.rb +17 -17
  38. data/spec/locales/ru_spec.rb +2 -2
  39. data/spec/r18n_spec.rb +7 -3
  40. data/spec/spec_helper.rb +2 -0
  41. data/spec/translated_spec.rb +108 -0
  42. data/spec/translation_spec.rb +24 -56
  43. data/spec/translations/extension/{no_TR.yml → no-tr.yml} +0 -0
  44. data/spec/translations/general/en.yml +15 -2
  45. data/spec/translations/general/{no_LC.yml → no-lc.yml} +0 -0
  46. metadata +46 -31
  47. data/locales/en_US.yml +0 -14
@@ -0,0 +1,167 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+
4
+ describe R18n::Filters do
5
+ before do
6
+ @system = R18n::Filters.defined.values
7
+ @enabled = R18n::Filters.defined.values.reject { |i| !i.enabled? }
8
+ @i18n = R18n::I18n.new('en', DIR)
9
+ end
10
+
11
+ after do
12
+ R18n::Filters.defined.values.each do |filter|
13
+ next if @system.include? filter
14
+ R18n::Filters.delete(filter)
15
+ end
16
+
17
+ @enabled.each { |i| R18n::Filters.on(i) unless i.enabled? }
18
+ (@system - @enabled).each { |i| R18n::Filters.off(i) if i.enabled? }
19
+ end
20
+
21
+ it "should add new filter" do
22
+ filter = R18n::Filters.add('my', :my_filter) { |i, config| i }
23
+
24
+ filter.should be_a(R18n::Filters::Filter)
25
+ filter.name.should == :my_filter
26
+ filter.type.should == 'my'
27
+ filter.should be_enabled
28
+
29
+ R18n::Filters.defined.should have_key(:my_filter)
30
+ @i18n.my_filter.should == 'value'
31
+ @i18n.my_tree_filter.should == {'name' => 'value'}
32
+ end
33
+
34
+ it "should use cascade filters" do
35
+ filter = R18n::Filters.add('my', :one) { |i, config| i + '1' }
36
+ filter = R18n::Filters.add('my', :two) { |i, config| i + '2' }
37
+ filter = R18n::Filters.add('my', :three, 0) { |i, config| i + '3' }
38
+ @i18n.my_filter.should == 'value312'
39
+ end
40
+
41
+ it "should return name for nameless filter" do
42
+ R18n::Filters.instance_variable_set(:@last_auto_name, 0)
43
+
44
+ R18n::Filters.add('some').name.should == 1
45
+ R18n::Filters.add('some').name.should == 2
46
+
47
+ R18n::Filters.add('some', 3)
48
+ R18n::Filters.add('some').name.should == 4
49
+ end
50
+
51
+ it "should delete filter by name" do
52
+ R18n::Filters.add('my', :my_filter) { '1' }
53
+ @i18n.my_filter.should == '1'
54
+
55
+ R18n::Filters.delete(:my_filter)
56
+ R18n::Filters.defined.should_not have_key(:my_filter)
57
+ @i18n.my_filter.should == 'value'
58
+ end
59
+
60
+ it "should delete filter by object" do
61
+ filter = R18n::Filters.add('my') { '1' }
62
+ @i18n.my_filter.should == '1'
63
+
64
+ R18n::Filters.delete(filter)
65
+ R18n::Filters.defined.should_not have_key(filter.name)
66
+ @i18n.my_filter.should == 'value'
67
+ end
68
+
69
+ it "should use global filters" do
70
+ R18n::Filters.add(String) { |result, config, a, b| result + a + b }
71
+ R18n::Filters.add(String) { |result, config| result + '!' }
72
+
73
+ @i18n.one('1', '2').should == 'One12!'
74
+ end
75
+
76
+ it "should turn off filter" do
77
+ filter = R18n::Filters.add('my', :one) { |i, config| i + '1' }
78
+ filter = R18n::Filters.add('my', :two) { |i, config| i + '2' }
79
+
80
+ R18n::Filters.off(:one)
81
+ R18n::Filters.defined[:one].should_not be_enabled
82
+ @i18n.my_filter.should == 'value2'
83
+
84
+ R18n::Filters.on(:one)
85
+ R18n::Filters.defined[:one].should be_enabled
86
+ @i18n.my_filter.should == 'value12'
87
+ end
88
+
89
+ it "should send config to filter" do
90
+ R18n::Filters.add('my') do |i, config|
91
+ config.secret_value = 1
92
+ config
93
+ end
94
+ @i18n.my_filter.locale.should == @i18n.locale
95
+ @i18n.my_filter.path.should == 'my_filter'
96
+ @i18n.my_filter.secret_value.should == 1
97
+ @i18n.my_filter.unknown_value.should be_nil
98
+ end
99
+
100
+ it "should use one config for cascade filters" do
101
+ R18n::Filters.add('my') { |content, config| config.new_secret ? 2 : 1 }
102
+ @i18n.my_filter.should == 1
103
+
104
+ R18n::Filters.add('my', nil, 0) do |content, config|
105
+ config.new_secret = true
106
+ content
107
+ end
108
+ @i18n.my_filter.should == 2
109
+ end
110
+
111
+ it "should send parameters to filter" do
112
+ R18n::Filters.add('my') { |i, config, a, b| "#{i}#{a}#{b}" }
113
+ @i18n['my_filter', 1, 2].should == 'value12'
114
+ @i18n.my_filter(1, 2).should == 'value12'
115
+ end
116
+
117
+ it "should call proc from translation" do
118
+ @i18n.sum(2, 3).should == 5
119
+ end
120
+
121
+ it "should pluralize translation" do
122
+ @i18n.comments(0, 'article').should == 'no comments for article'
123
+ @i18n.comments(1, 'article').should == 'one comment for article'
124
+ @i18n.comments(5, 'article').should == '5 comments for article'
125
+
126
+ @i18n.files(0).should == '0 files'
127
+ @i18n.files(-5.5).should == '−5.5 files'
128
+ @i18n.files(5000).should == '5,000 files'
129
+ end
130
+
131
+ it "should pluralize translation without locale" do
132
+ i18n = R18n::I18n.new('no-LC', DIR)
133
+ i18n.entries(1).should == 'ONE'
134
+ i18n.entries(5).should == 'N'
135
+ end
136
+
137
+ it "should can use params in translation" do
138
+ @i18n.params(-1, 2).should == 'Is −1 between −1 and 2?'
139
+ end
140
+
141
+ it "should have filter for escape HTML" do
142
+ @i18n.html.should == '<script>true && false</script>'
143
+ end
144
+
145
+ it "should have disabled global filter for escape HTML" do
146
+ @i18n.greater.should == '1 < 2 is true'
147
+
148
+ R18n::Filters.on(:global_escape_html)
149
+ @i18n.greater.should == '1 &lt; 2 is true'
150
+ @i18n.html.should == '&lt;script&gt;true &amp;&amp; false&lt;/script&gt;'
151
+ end
152
+
153
+ it "should have filter to disable global HTML escape" do
154
+ @i18n.no_escape.should == '<b>Warning</b>'
155
+ R18n::Filters.on(:global_escape_html)
156
+ @i18n.no_escape.should == '<b>Warning</b>'
157
+ end
158
+
159
+ it "should have Markdown filter" do
160
+ @i18n.markdown.should == '<p><strong>Hi!</strong></p>'
161
+ end
162
+
163
+ it "should have Textile filter" do
164
+ @i18n.textile.should == '<p><em>Hi!</em></p>'
165
+ end
166
+
167
+ end
data/spec/i18n_spec.rb CHANGED
@@ -2,6 +2,9 @@
2
2
  require File.join(File.dirname(__FILE__), 'spec_helper')
3
3
 
4
4
  describe R18n::I18n do
5
+ after do
6
+ R18n::I18n.default = 'en'
7
+ end
5
8
 
6
9
  it "should parse HTTP_ACCEPT_LANGUAGE" do
7
10
  R18n::I18n.parse_http(nil).should == []
@@ -19,14 +22,16 @@ describe R18n::I18n do
19
22
  i18n = R18n::I18n.new('en', DIR)
20
23
  i18n.locales.should == [R18n::Locale.load('en')]
21
24
 
22
- i18n = R18n::I18n.new(['ru', 'no_LC'], DIR)
25
+ i18n = R18n::I18n.new(['ru', 'no-LC'], DIR)
23
26
  i18n.locales.should == [R18n::Locale.load('ru'),
24
- R18n::UnsupportedLocale.new('no_LC')]
27
+ R18n::UnsupportedLocale.new('no-LC'),
28
+ R18n::UnsupportedLocale.new('no'),
29
+ R18n::UnsupportedLocale.new('en')]
25
30
  end
26
31
 
27
32
  it "should return translations dir" do
28
33
  i18n = R18n::I18n.new('en', DIR)
29
- i18n.translations_dirs.expand_path.to_s.should == DIR.expand_path.to_s
34
+ i18n.translation_dirs.expand_path.to_s.should == DIR.expand_path.to_s
30
35
  end
31
36
 
32
37
  it "should load translations" do
@@ -37,15 +42,13 @@ describe R18n::I18n do
37
42
  end
38
43
 
39
44
  it "should load default translation" do
40
- R18n::I18n.default = 'en'
41
-
42
- i18n = R18n::I18n.new('no_LC', DIR)
45
+ i18n = R18n::I18n.new('no-LC', DIR)
43
46
  i18n.one.should == 'ONE'
44
47
  i18n.two.should == 'Two'
45
48
  end
46
49
 
47
50
  it "should load sublocales for first locale" do
48
- R18n::I18n.default = 'no_TR'
51
+ R18n::I18n.default = 'no-TR'
49
52
 
50
53
  i18n = R18n::I18n.new('ru', DIR)
51
54
  i18n.one.should == 'Один'
@@ -54,32 +57,74 @@ describe R18n::I18n do
54
57
 
55
58
  it "should return available translations" do
56
59
  i18n = R18n::I18n.new('en', DIR)
57
- i18n.translations.should == {
58
- 'no_LC' => 'no_LC', 'ru' => 'Русский', 'en' => 'English'}
60
+ i18n.translations.should == { 'no-lc' => 'no-lc', 'ru' => 'Русский',
61
+ 'en' => 'English' }
59
62
  end
60
63
 
61
64
  it "should return first locale with locale file" do
62
- i18n = R18n::I18n.new(['no_LC', 'ru', 'en'], DIR)
63
- i18n.locale.should == R18n::Locale.load('ru')
65
+ i18n = R18n::I18n.new(['no-LC', 'ru', 'en'], DIR)
66
+ i18n.locale.should == R18n::Locale.load('no-LC')
67
+ i18n.locale.base.should == R18n::Locale.load('ru')
64
68
  end
65
69
 
66
70
  it "should localize objects" do
67
- i18n = R18n::I18n.new('ru', DIR)
71
+ i18n = R18n::I18n.new('ru')
68
72
 
69
73
  i18n.l(-123456789).should == '−123 456 789'
70
74
  i18n.l(-12345.67).should == '−12 345,67'
71
75
 
72
76
  time = Time.at(0).utc
73
- i18n.l(time).should =~ /^Чтв, 01 янв 1970, 00:00:00 (GMT|UTC)$/
74
- i18n.l(time, :time).should == '00:00'
75
77
  i18n.l(time, '%A').should == 'Четверг'
78
+ i18n.l(time, :month).should == 'Январь'
79
+ i18n.l(time, :standard).should == '01.01.1970 00:00'
80
+ i18n.l(time, :full).should == ' 1 января 1970 00:00'
76
81
 
77
82
  i18n.l(Date.new(0)).should == '01.01.0000'
78
83
  end
79
84
 
80
- it "should loading without translations" do
85
+ it "should localize date for human" do
81
86
  i18n = R18n::I18n.new('ru')
82
- i18n.l(Date.new(0)).should == '01.01.0000'
87
+
88
+ i18n.l(Date.today + 2, :human).should == 'через 2 дня'
89
+ i18n.l(Date.today + 1, :human).should == 'завтра'
90
+ i18n.l(Date.today, :human).should == 'сегодня'
91
+ i18n.l(Date.today - 1, :human).should == 'вчера'
92
+ i18n.l(Date.today - 3, :human).should == '3 дня назад'
93
+
94
+ y2000 = Date.parse('2000-01-08')
95
+ i18n.l(y2000, :human, Date.parse('2000-01-01')).should == ' 8 января'
96
+ i18n.l(y2000, :human, Date.parse('1999-01-01')).should == ' 8 января 2000'
97
+ end
98
+
99
+ it "should localize times for human" do
100
+ i18n = R18n::I18n.new('ru')
101
+ minute = 60
102
+ hour = 60 * minute
103
+ day = 24 * hour
104
+ zero = Time.at(0).utc
105
+
106
+ i18n.l( zero + 7 * day, :human, zero).should == ' 8 января 00:00'
107
+ i18n.l( zero + 50 * hour, :human, zero).should == 'через 2 дня 02:00'
108
+ i18n.l( zero + 25 * hour, :human, zero).should == 'завтра 01:00'
109
+ i18n.l( zero + 70 * minute, :human, zero).should == 'через 1 час'
110
+ i18n.l( zero + 38 * minute, :human, zero).should == 'через 38 минут'
111
+ i18n.l( zero + 5, :human, zero).should == 'сейчас'
112
+ i18n.l( zero - 15, :human, zero).should == 'сейчас'
113
+ i18n.l( zero - minute, :human, zero).should == '1 минуту назад'
114
+ i18n.l( zero - 2 * hour, :human, zero).should == '2 часа назад'
115
+ i18n.l( zero - 13 * hour, :human, zero).should == 'вчера 11:00'
116
+ i18n.l( zero - 50 * hour, :human, zero).should == '3 дня назад 22:00'
117
+ i18n.l( zero - 9 * day, :human, zero).should == '23 декабря 1969 00:00'
118
+ end
119
+
120
+ it "should use standard formatter by default" do
121
+ R18n::I18n.new('ru').l(Time.at(0).utc).should == '01.01.1970 00:00'
122
+ end
123
+
124
+ it "should raise error on unknown formatter" do
125
+ lambda {
126
+ R18n::I18n.new('ru').l(Time.at(0).utc, :unknown)
127
+ }.should raise_error(ArgumentError, /formatter/)
83
128
  end
84
129
 
85
130
  end
data/spec/locale_spec.rb CHANGED
@@ -1,33 +1,38 @@
1
1
  # encoding: utf-8
2
- require File.join(File.dirname(__FILE__), "spec_helper")
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
3
 
4
4
  describe R18n::Locale do
5
5
 
6
6
  it "should check is locale exists" do
7
7
  R18n::Locale.exists?('ru').should be_true
8
- R18n::Locale.exists?('no_LC').should be_false
8
+ R18n::Locale.exists?('no-LC').should be_false
9
9
  end
10
10
 
11
11
  it "should load locale" do
12
12
  locale = R18n::Locale.load('ru')
13
- locale['code'].should == 'ru'
14
- locale['title'].should == 'Русский'
13
+ locale.code.should == 'ru'
14
+ locale.title.should == 'Русский'
15
+ locale.ltr?.should be_true
16
+ end
17
+
18
+ it "should load locale by Symbol" do
19
+ R18n::Locale.load(:ru).should == R18n::Locale.load('ru')
15
20
  end
16
21
 
17
22
  it "should use locale's class" do
18
23
  ru = R18n::Locale.load('ru')
19
24
  ru.class.should == R18n::Locales::Ru
20
25
 
21
- en = R18n::Locale.load('en')
22
- en.class.should == R18n::Locale
26
+ eo = R18n::Locale.load('eo')
27
+ eo.class.should == R18n::Locale
23
28
  end
24
29
 
25
30
  it "should include locale by +include+ option" do
26
- en_US = R18n::Locale.load('en_US')
31
+ enUS = R18n::Locale.load('en-US')
27
32
  en = R18n::Locale.load('en')
28
- en_US['title'].should == 'English (US)'
29
- en['title'].should == 'English'
30
- en_US['week'].should == en['week']
33
+ enUS.title.should == 'English (US)'
34
+ en.title.should == 'English'
35
+ enUS['week'].should == en['week']
31
36
  end
32
37
 
33
38
  it "should be equal to another locale with same code" do
@@ -55,12 +60,23 @@ describe R18n::Locale do
55
60
  end
56
61
 
57
62
  it "should use UnsupportedLocale if locale file isn't exists" do
58
- locale = R18n::Locale.load('no_LC')
59
- locale.should be_a(R18n::UnsupportedLocale)
60
- locale['code'].should == 'no_LC'
61
- locale['title'].should be_empty
62
- locale['direction'].should == 'ltr'
63
- locale.pluralize(5).should == 'n'
63
+ supported = R18n::Locale.load('en')
64
+ supported.should be_supported
65
+
66
+ unsupported = R18n::Locale.load('no-LC')
67
+ unsupported.should_not be_supported
68
+ unsupported.should be_a(R18n::UnsupportedLocale)
69
+
70
+ unsupported.code.should == 'no-LC'
71
+ unsupported.title.should == 'no-LC'
72
+ unsupported.ltr?.should be_true
73
+
74
+ unsupported['code'].should == 'no-LC'
75
+ unsupported['title'].should == 'no-LC'
76
+ unsupported['direction'].should == 'ltr'
77
+
78
+ unsupported.pluralize(5).should == 'n'
79
+ unsupported.inspect.should == 'Unsupported locale no-LC'
64
80
  end
65
81
 
66
82
  it "should format number in local traditions" do
@@ -80,14 +96,25 @@ describe R18n::Locale do
80
96
  locale.strftime(time, '%a %A').should == 'Чтв Четверг'
81
97
  locale.strftime(time, '%b %B').should == 'янв января'
82
98
  locale.strftime(time, '%H:%M%p').should == '00:00 утра'
83
-
84
- locale.strftime(time, :month).should == 'Январь'
85
- locale.strftime(time, :datetime).should =~ /^Чтв, 01 янв 1970, 00:00:00 (GMT|UTC)$/
86
99
  end
87
100
 
88
101
  it "should delete slashed from locale for security reasons" do
89
102
  locale = R18n::Locale.load('../spec/translations/general/en')
90
103
  locale.should be_a(R18n::UnsupportedLocale)
91
104
  end
105
+
106
+ it "should ignore code case in locales" do
107
+ upcase = R18n::Locale.load('RU')
108
+ downcase = R18n::Locale.load('ru')
109
+ upcase.should == downcase
110
+ upcase.code.should == 'ru'
111
+ downcase.code.should == 'ru'
112
+
113
+ upcase = R18n::Locale.load('no-LC')
114
+ downcase = R18n::Locale.load('no-lc')
115
+ upcase.should == downcase
116
+ upcase.code.should == 'no-LC'
117
+ downcase.code.should == 'no-lc'
118
+ end
92
119
 
93
120
  end
@@ -0,0 +1,22 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'locales', 'cs')
3
+
4
+ describe R18n::Locales::Cs do
5
+ it "should use Czech pluralization" do
6
+ ru = R18n::Locale.load 'cs'
7
+ ru.pluralize(0).should == 0
8
+ ru.pluralize(1).should == 1
9
+ ru.pluralize(2).should == 2
10
+ ru.pluralize(3).should == 2
11
+ ru.pluralize(4).should == 2
12
+ ru.pluralize(5).should == 'n'
13
+ ru.pluralize(21).should == 'n'
14
+ ru.pluralize(11).should == 'n'
15
+ ru.pluralize(12).should == 'n'
16
+ ru.pluralize(22).should == 'n'
17
+ ru.pluralize(57).should == 'n'
18
+ ru.pluralize(101).should == 'n'
19
+ ru.pluralize(102).should == 'n'
20
+ ru.pluralize(111).should == 'n'
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'locales', 'en-us')
3
+
4
+ describe R18n::Locales::EnUs do
5
+ it "should format American English date" do
6
+ enUS = R18n::I18n.new('en-US')
7
+ enUS.l(Date.parse('2009-05-01'), :full).should == 'May 1st, 2009'
8
+ enUS.l(Date.parse('2009-05-02'), :full).should == 'May 2nd, 2009'
9
+ enUS.l(Date.parse('2009-05-03'), :full).should == 'May 3rd, 2009'
10
+ enUS.l(Date.parse('2009-05-04'), :full).should == 'May 4th, 2009'
11
+ enUS.l(Date.parse('2009-05-11'), :full).should == 'May 11th, 2009'
12
+ enUS.l(Date.parse('2009-05-21'), :full).should == 'May 21st, 2009'
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'locales', 'en')
3
+
4
+ describe R18n::Locales::En do
5
+ it "should format English date" do
6
+ en = R18n::I18n.new('en')
7
+ en.l(Date.parse('2009-05-01'), :full).should == '1st of May, 2009'
8
+ en.l(Date.parse('2009-05-02'), :full).should == '2nd of May, 2009'
9
+ en.l(Date.parse('2009-05-03'), :full).should == '3rd of May, 2009'
10
+ en.l(Date.parse('2009-05-04'), :full).should == '4th of May, 2009'
11
+ en.l(Date.parse('2009-05-11'), :full).should == '11th of May, 2009'
12
+ en.l(Date.parse('2009-05-21'), :full).should == '21st of May, 2009'
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'locales', 'fr')
3
+
4
+ describe R18n::Locales::Fr do
5
+ it "should format French date" do
6
+ fr = R18n::I18n.new('fr')
7
+ fr.l(Date.parse('2009-07-01'), :full).should == '1er juillet 2009'
8
+ fr.l(Date.parse('2009-07-02'), :full).should == ' 2 juillet 2009'
9
+ end
10
+ end
@@ -1,25 +1,25 @@
1
- require File.join(File.dirname(__FILE__), "..", "spec_helper")
2
- require File.join(File.dirname(__FILE__), "..", "..", "locales", "pl")
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'locales', 'pl')
3
3
 
4
4
  describe R18n::Locales::Pl do
5
5
  it "should use Polish pluralization" do
6
- ru = R18n::Locale.load 'pl'
7
- ru.pluralize(0).should == 0
6
+ pl = R18n::Locale.load 'pl'
7
+ pl.pluralize(0).should == 0
8
8
 
9
- ru.pluralize(1).should == 1
10
- ru.pluralize(21).should == 'n'
11
- ru.pluralize(101).should == 'n'
9
+ pl.pluralize(1).should == 1
12
10
 
13
- ru.pluralize(2).should == 2
14
- ru.pluralize(4).should == 2
15
- ru.pluralize(22).should == 2
16
- ru.pluralize(102).should == 2
11
+ pl.pluralize(2).should == 2
12
+ pl.pluralize(4).should == 2
13
+ pl.pluralize(22).should == 2
14
+ pl.pluralize(102).should == 2
17
15
 
18
- ru.pluralize(5).should == 'n'
19
- ru.pluralize(11).should == 'n'
20
- ru.pluralize(12).should == 'n'
21
- ru.pluralize(57).should == 'n'
22
- ru.pluralize(111).should == 'n'
23
- ru.pluralize(112).should == 'n'
16
+ pl.pluralize(5).should == 'n'
17
+ pl.pluralize(11).should == 'n'
18
+ pl.pluralize(12).should == 'n'
19
+ pl.pluralize(21).should == 'n'
20
+ pl.pluralize(57).should == 'n'
21
+ pl.pluralize(101).should == 'n'
22
+ pl.pluralize(111).should == 'n'
23
+ pl.pluralize(112).should == 'n'
24
24
  end
25
25
  end
@@ -1,5 +1,5 @@
1
- require File.join(File.dirname(__FILE__), "..", "spec_helper")
2
- require File.join(File.dirname(__FILE__), "..", "..", "locales", "ru")
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'locales', 'ru')
3
3
 
4
4
  describe R18n::Locales::Ru do
5
5
  it "should use Russian pluralization" do
data/spec/r18n_spec.rb CHANGED
@@ -22,13 +22,17 @@ describe R18n do
22
22
  :c => 2 }
23
23
  end
24
24
 
25
+ it "should convert Time to Date" do
26
+ R18n::Utils.to_date(Time.now).should == Date.today
27
+ end
28
+
25
29
  it "should set untranslated format" do
26
- translation = R18n::Translation.load('en', DIR)
30
+ i18n = R18n::I18n.new('en', DIR)
27
31
  R18n.untranslated = nil
28
- translation.in.not.to_s.should be_nil
32
+ i18n.in.not.to_s.should be_nil
29
33
 
30
34
  R18n.untranslated = '%1 %2[%3]'
31
- translation.in.not.to_s.should == 'in.not in.[not]'
35
+ i18n.in.not.to_s.should == 'in.not in.[not]'
32
36
  end
33
37
 
34
38
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  # encoding: utf-8
2
+ require 'pp'
3
+
2
4
  require File.join(File.dirname(__FILE__), '../lib/r18n-core')
3
5
 
4
6
  DIR = Pathname(__FILE__).dirname + 'translations/general' unless defined? DIR
@@ -0,0 +1,108 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'r18n-core', 'translated')
4
+
5
+ describe R18n::Translated do
6
+ before do
7
+ @user_class = Class.new do
8
+ include R18n::Translated
9
+ attr_accessor :name_ru, :name_en
10
+ end
11
+ R18n.set(R18n::I18n.new('en'))
12
+ end
13
+
14
+ it "should save methods map" do
15
+ @user_class.translation :name, :methods => { :ru => :name_ru }
16
+ @user_class.unlocalized_getters(:name).should == { 'ru' => 'name_ru' }
17
+ @user_class.unlocalized_setters(:name).should == { 'ru' => 'name_ru=' }
18
+ end
19
+
20
+ it "should autodetect methods map" do
21
+ @user_class.translation :name
22
+ @user_class.unlocalized_getters(:name).should == {
23
+ 'en' => 'name_en', 'ru' => 'name_ru' }
24
+ @user_class.unlocalized_setters(:name).should == {
25
+ 'en' => 'name_en=', 'ru' => 'name_ru=' }
26
+ end
27
+
28
+ it "should translate methods" do
29
+ @user_class.translation :name
30
+ user = @user_class.new
31
+
32
+ user.name.should be_nil
33
+ user.name = 'John'
34
+ user.name.should == 'John'
35
+
36
+ R18n.set(R18n::I18n.new('ru'))
37
+ user.name.should == 'John'
38
+ user.name = 'Джон'
39
+ user.name.should == 'Джон'
40
+ end
41
+
42
+ it "should return TranslatedString" do
43
+ class ::SomeTranslatedClass
44
+ include R18n::Translated
45
+ def name_en; 'John'; end
46
+ translation :name
47
+ end
48
+ obj = ::SomeTranslatedClass.new
49
+
50
+ obj.name.should be_a(R18n::TranslatedString)
51
+ obj.name.locale.should == R18n::Locale.load('en')
52
+ obj.name.path.should == 'SomeTranslatedClass#name'
53
+ end
54
+
55
+ it "should search translation by locales priority" do
56
+ @user_class.translation :name
57
+ user = @user_class.new
58
+
59
+ R18n.set(R18n::I18n.new(['no-LC', 'ru', 'en']))
60
+ user.name_ru = 'Иван'
61
+ user.name.locale.should == R18n::Locale.load('ru')
62
+ end
63
+
64
+ it "should use default locale" do
65
+ @user_class.translation :name
66
+ user = @user_class.new
67
+
68
+ R18n.set(R18n::I18n.new('no-LC'))
69
+ user.name_en = 'John'
70
+ user.name.locale.should == R18n::Locale.load('en')
71
+ end
72
+
73
+ it "should use filters" do
74
+ @user_class.class_eval do
75
+ def age_en; {1 => '%1 year', 'n' => '%1 years'} end
76
+ translation :age, :type => 'pl', :no_params => true
77
+ end
78
+ user = @user_class.new
79
+
80
+ user.age(20).should == '20 years'
81
+ end
82
+
83
+ it "should send params to method if user want it" do
84
+ @user_class.class_eval do
85
+ def no_params_en(*params) params.join(' '); end
86
+ def params_en(*params) params.join(' '); end
87
+ translations [:no_params, {:no_params => true}], :params
88
+ end
89
+ user = @user_class.new
90
+
91
+ user.no_params(1, 2).should == ''
92
+ user.params(1, 2).should == '1 2'
93
+ end
94
+
95
+ it "should return Untranslated when can't find translation" do
96
+ class ::SomeUntranslatedClass
97
+ include R18n::Translated
98
+
99
+ translation :no
100
+ end
101
+ obj = ::SomeUntranslatedClass.new
102
+
103
+ obj.no.should be_a(R18n::Untranslated)
104
+ obj.no.translated_path.should == 'SomeUntranslatedClass#'
105
+ obj.no.untranslated_path.should == 'no'
106
+ end
107
+
108
+ end