@@ -1,269 +1,271 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.expand_path('../../../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class Redmine::I18nTest < ActiveSupport::TestCase |
|
20 | class Redmine::I18nTest < ActiveSupport::TestCase | |
21 | include Redmine::I18n |
|
21 | include Redmine::I18n | |
22 | include ActionView::Helpers::NumberHelper |
|
22 | include ActionView::Helpers::NumberHelper | |
23 |
|
23 | |||
24 | def setup |
|
24 | def setup | |
25 | User.current.language = nil |
|
25 | User.current.language = nil | |
26 | end |
|
26 | end | |
27 |
|
27 | |||
28 | def teardown |
|
28 | def teardown | |
29 | set_language_if_valid 'en' |
|
29 | set_language_if_valid 'en' | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
32 | def test_date_format_default |
|
32 | def test_date_format_default | |
33 | set_language_if_valid 'en' |
|
33 | set_language_if_valid 'en' | |
34 | today = Date.today |
|
34 | today = Date.today | |
35 | Setting.date_format = '' |
|
35 | Setting.date_format = '' | |
36 | assert_equal I18n.l(today), format_date(today) |
|
36 | assert_equal I18n.l(today), format_date(today) | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def test_date_format |
|
39 | def test_date_format | |
40 | set_language_if_valid 'en' |
|
40 | set_language_if_valid 'en' | |
41 | today = Date.today |
|
41 | today = Date.today | |
42 | Setting.date_format = '%d %m %Y' |
|
42 | Setting.date_format = '%d %m %Y' | |
43 | assert_equal today.strftime('%d %m %Y'), format_date(today) |
|
43 | assert_equal today.strftime('%d %m %Y'), format_date(today) | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def test_date_format_default_with_user_locale |
|
46 | def test_date_format_default_with_user_locale | |
47 | set_language_if_valid 'es' |
|
47 | set_language_if_valid 'es' | |
48 | today = now = Time.parse('2011-02-20 14:00:00') |
|
48 | today = now = Time.parse('2011-02-20 14:00:00') | |
49 | Setting.date_format = '%d %B %Y' |
|
49 | Setting.date_format = '%d %B %Y' | |
50 | User.current.language = 'fr' |
|
50 | User.current.language = 'fr' | |
51 | s1 = "20 f\xc3\xa9vrier 2011" |
|
51 | s1 = "20 f\xc3\xa9vrier 2011" | |
52 | s1.force_encoding("UTF-8") if s1.respond_to?(:force_encoding) |
|
52 | s1.force_encoding("UTF-8") if s1.respond_to?(:force_encoding) | |
53 | assert_equal s1, format_date(today) |
|
53 | assert_equal s1, format_date(today) | |
54 | User.current.language = nil |
|
54 | User.current.language = nil | |
55 | assert_equal '20 Febrero 2011', format_date(today) |
|
55 | assert_equal '20 Febrero 2011', format_date(today) | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | def test_date_and_time_for_each_language |
|
58 | def test_date_and_time_for_each_language | |
59 | Setting.date_format = '' |
|
59 | Setting.date_format = '' | |
60 | valid_languages.each do |lang| |
|
60 | valid_languages.each do |lang| | |
61 | set_language_if_valid lang |
|
61 | set_language_if_valid lang | |
62 | assert_nothing_raised "#{lang} failure" do |
|
62 | assert_nothing_raised "#{lang} failure" do | |
63 | format_date(Date.today) |
|
63 | format_date(Date.today) | |
64 | format_time(Time.now) |
|
64 | format_time(Time.now) | |
65 | format_time(Time.now, false) |
|
65 | format_time(Time.now, false) | |
66 | assert_not_equal 'default', ::I18n.l(Date.today, :format => :default), |
|
66 | assert_not_equal 'default', ::I18n.l(Date.today, :format => :default), | |
67 | "date.formats.default missing in #{lang}" |
|
67 | "date.formats.default missing in #{lang}" | |
68 | assert_not_equal 'time', ::I18n.l(Time.now, :format => :time), |
|
68 | assert_not_equal 'time', ::I18n.l(Time.now, :format => :time), | |
69 | "time.formats.time missing in #{lang}" |
|
69 | "time.formats.time missing in #{lang}" | |
70 | end |
|
70 | end | |
71 | assert l('date.day_names').is_a?(Array) |
|
71 | assert l('date.day_names').is_a?(Array) | |
72 | assert_equal 7, l('date.day_names').size |
|
72 | assert_equal 7, l('date.day_names').size | |
73 |
|
73 | |||
74 | assert l('date.month_names').is_a?(Array) |
|
74 | assert l('date.month_names').is_a?(Array) | |
75 | assert_equal 13, l('date.month_names').size |
|
75 | assert_equal 13, l('date.month_names').size | |
76 | end |
|
76 | end | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | def test_time_for_each_zone |
|
79 | def test_time_for_each_zone | |
80 | ActiveSupport::TimeZone.all.each do |zone| |
|
80 | ActiveSupport::TimeZone.all.each do |zone| | |
81 | User.current.stubs(:time_zone).returns(zone.name) |
|
81 | User.current.stubs(:time_zone).returns(zone.name) | |
82 | assert_nothing_raised "#{zone} failure" do |
|
82 | assert_nothing_raised "#{zone} failure" do | |
83 | format_time(Time.now) |
|
83 | format_time(Time.now) | |
84 | end |
|
84 | end | |
85 | end |
|
85 | end | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | def test_time_format |
|
88 | def test_time_format | |
89 | set_language_if_valid 'en' |
|
89 | set_language_if_valid 'en' | |
90 | now = Time.parse('2011-02-20 15:45:22') |
|
90 | now = Time.parse('2011-02-20 15:45:22') | |
91 | with_settings :time_format => '%H:%M' do |
|
91 | with_settings :time_format => '%H:%M' do | |
92 | with_settings :date_format => '' do |
|
92 | with_settings :date_format => '' do | |
93 | assert_equal '02/20/2011 15:45', format_time(now) |
|
93 | assert_equal '02/20/2011 15:45', format_time(now) | |
94 | assert_equal '15:45', format_time(now, false) |
|
94 | assert_equal '15:45', format_time(now, false) | |
95 | end |
|
95 | end | |
96 | with_settings :date_format => '%Y-%m-%d' do |
|
96 | with_settings :date_format => '%Y-%m-%d' do | |
97 | assert_equal '2011-02-20 15:45', format_time(now) |
|
97 | assert_equal '2011-02-20 15:45', format_time(now) | |
98 | assert_equal '15:45', format_time(now, false) |
|
98 | assert_equal '15:45', format_time(now, false) | |
99 | end |
|
99 | end | |
100 | end |
|
100 | end | |
101 | end |
|
101 | end | |
102 |
|
102 | |||
103 | def test_time_format_default |
|
103 | def test_time_format_default | |
104 | set_language_if_valid 'en' |
|
104 | set_language_if_valid 'en' | |
105 | now = Time.parse('2011-02-20 15:45:22') |
|
105 | now = Time.parse('2011-02-20 15:45:22') | |
106 | with_settings :time_format => '' do |
|
106 | with_settings :time_format => '' do | |
107 | with_settings :date_format => '' do |
|
107 | with_settings :date_format => '' do | |
108 | assert_equal '02/20/2011 03:45 pm', format_time(now) |
|
108 | assert_equal '02/20/2011 03:45 pm', format_time(now) | |
109 | assert_equal '03:45 pm', format_time(now, false) |
|
109 | assert_equal '03:45 pm', format_time(now, false) | |
110 | end |
|
110 | end | |
111 | with_settings :date_format => '%Y-%m-%d' do |
|
111 | with_settings :date_format => '%Y-%m-%d' do | |
112 | assert_equal '2011-02-20 03:45 pm', format_time(now) |
|
112 | assert_equal '2011-02-20 03:45 pm', format_time(now) | |
113 | assert_equal '03:45 pm', format_time(now, false) |
|
113 | assert_equal '03:45 pm', format_time(now, false) | |
114 | end |
|
114 | end | |
115 | end |
|
115 | end | |
116 | end |
|
116 | end | |
117 |
|
117 | |||
118 | def test_time_format_default_with_user_locale |
|
118 | def test_time_format_default_with_user_locale | |
119 | set_language_if_valid 'en' |
|
119 | set_language_if_valid 'en' | |
120 | User.current.language = 'fr' |
|
120 | User.current.language = 'fr' | |
121 | now = Time.parse('2011-02-20 15:45:22') |
|
121 | now = Time.parse('2011-02-20 15:45:22') | |
122 | with_settings :time_format => '' do |
|
122 | with_settings :time_format => '' do | |
123 | with_settings :date_format => '' do |
|
123 | with_settings :date_format => '' do | |
124 | assert_equal '20/02/2011 15:45', format_time(now) |
|
124 | assert_equal '20/02/2011 15:45', format_time(now) | |
125 | assert_equal '15:45', format_time(now, false) |
|
125 | assert_equal '15:45', format_time(now, false) | |
126 | end |
|
126 | end | |
127 | with_settings :date_format => '%Y-%m-%d' do |
|
127 | with_settings :date_format => '%Y-%m-%d' do | |
128 | assert_equal '2011-02-20 15:45', format_time(now) |
|
128 | assert_equal '2011-02-20 15:45', format_time(now) | |
129 | assert_equal '15:45', format_time(now, false) |
|
129 | assert_equal '15:45', format_time(now, false) | |
130 | end |
|
130 | end | |
131 | end |
|
131 | end | |
132 | end |
|
132 | end | |
133 |
|
133 | |||
134 | def test_time_format |
|
134 | def test_time_format | |
135 | set_language_if_valid 'en' |
|
135 | set_language_if_valid 'en' | |
136 | now = Time.now |
|
136 | now = Time.now | |
137 | Setting.date_format = '%d %m %Y' |
|
137 | Setting.date_format = '%d %m %Y' | |
138 | Setting.time_format = '%H %M' |
|
138 | Setting.time_format = '%H %M' | |
139 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now) |
|
139 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now) | |
140 | assert_equal now.strftime('%H %M'), format_time(now, false) |
|
140 | assert_equal now.strftime('%H %M'), format_time(now, false) | |
141 | end |
|
141 | end | |
142 |
|
142 | |||
143 | def test_utc_time_format |
|
143 | def test_utc_time_format | |
144 | set_language_if_valid 'en' |
|
144 | set_language_if_valid 'en' | |
145 | now = Time.now |
|
145 | now = Time.now | |
146 | Setting.date_format = '%d %m %Y' |
|
146 | Setting.date_format = '%d %m %Y' | |
147 | Setting.time_format = '%H %M' |
|
147 | Setting.time_format = '%H %M' | |
148 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc) |
|
148 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc) | |
149 | assert_equal now.strftime('%H %M'), format_time(now.utc, false) |
|
149 | assert_equal now.strftime('%H %M'), format_time(now.utc, false) | |
150 | end |
|
150 | end | |
151 |
|
151 | |||
152 | def test_number_to_human_size_for_each_language |
|
152 | def test_number_to_human_size_for_each_language | |
153 | valid_languages.each do |lang| |
|
153 | valid_languages.each do |lang| | |
154 | set_language_if_valid lang |
|
154 | set_language_if_valid lang | |
155 | assert_nothing_raised "#{lang} failure" do |
|
155 | assert_nothing_raised "#{lang} failure" do | |
156 | size = number_to_human_size(257024) |
|
156 | size = number_to_human_size(257024) | |
157 | assert_match /251/, size |
|
157 | assert_match /251/, size | |
158 | end |
|
158 | end | |
159 | end |
|
159 | end | |
160 | end |
|
160 | end | |
161 |
|
161 | |||
162 | def test_day_name |
|
162 | def test_day_name | |
163 | set_language_if_valid 'fr' |
|
163 | set_language_if_valid 'fr' | |
164 | assert_equal 'dimanche', day_name(0) |
|
164 | assert_equal 'dimanche', day_name(0) | |
165 | assert_equal 'jeudi', day_name(4) |
|
165 | assert_equal 'jeudi', day_name(4) | |
166 | end |
|
166 | end | |
167 |
|
167 | |||
168 | def test_day_letter |
|
168 | def test_day_letter | |
169 | set_language_if_valid 'fr' |
|
169 | set_language_if_valid 'fr' | |
170 | assert_equal 'd', day_letter(0) |
|
170 | assert_equal 'd', day_letter(0) | |
171 | assert_equal 'j', day_letter(4) |
|
171 | assert_equal 'j', day_letter(4) | |
172 | end |
|
172 | end | |
173 |
|
173 | |||
174 | def test_number_to_currency_for_each_language |
|
174 | def test_number_to_currency_for_each_language | |
175 | valid_languages.each do |lang| |
|
175 | valid_languages.each do |lang| | |
176 | set_language_if_valid lang |
|
176 | set_language_if_valid lang | |
177 | assert_nothing_raised "#{lang} failure" do |
|
177 | assert_nothing_raised "#{lang} failure" do | |
178 | number_to_currency(-1000.2) |
|
178 | number_to_currency(-1000.2) | |
179 | end |
|
179 | end | |
180 | end |
|
180 | end | |
181 | end |
|
181 | end | |
182 |
|
182 | |||
183 | def test_number_to_currency_default |
|
183 | def test_number_to_currency_default | |
184 | set_language_if_valid 'bs' |
|
184 | set_language_if_valid 'bs' | |
185 | assert_equal "KM -1000,20", number_to_currency(-1000.2) |
|
185 | assert_equal "KM -1000,20", number_to_currency(-1000.2) | |
186 | set_language_if_valid 'de' |
|
186 | set_language_if_valid 'de' | |
187 | euro_sign = "\xe2\x82\xac" |
|
187 | euro_sign = "\xe2\x82\xac" | |
188 | euro_sign.force_encoding('UTF-8') if euro_sign.respond_to?(:force_encoding) |
|
188 | euro_sign.force_encoding('UTF-8') if euro_sign.respond_to?(:force_encoding) | |
189 | assert_equal "-1000,20 #{euro_sign}", number_to_currency(-1000.2) |
|
189 | assert_equal "-1000,20 #{euro_sign}", number_to_currency(-1000.2) | |
190 | end |
|
190 | end | |
191 |
|
191 | |||
192 | def test_valid_languages |
|
192 | def test_valid_languages | |
193 | assert valid_languages.is_a?(Array) |
|
193 | assert valid_languages.is_a?(Array) | |
194 | assert valid_languages.first.is_a?(Symbol) |
|
194 | assert valid_languages.first.is_a?(Symbol) | |
195 | end |
|
195 | end | |
196 |
|
196 | |||
197 | def test_languages_options |
|
197 | def test_languages_options | |
198 | options = languages_options |
|
198 | options = languages_options | |
199 |
|
||||
200 | assert options.is_a?(Array) |
|
199 | assert options.is_a?(Array) | |
201 | assert_equal valid_languages.size, options.size |
|
200 | assert_equal valid_languages.size, options.size | |
202 | assert_nil options.detect {|option| !option.is_a?(Array)} |
|
201 | assert_nil options.detect {|option| !option.is_a?(Array)} | |
203 | assert_nil options.detect {|option| option.size != 2} |
|
202 | assert_nil options.detect {|option| option.size != 2} | |
204 | assert_nil options.detect {|option| !option.first.is_a?(String) || !option.last.is_a?(String)} |
|
203 | assert_nil options.detect {|option| !option.first.is_a?(String) || !option.last.is_a?(String)} | |
205 | assert_include ["English", "en"], options |
|
204 | assert_include ["English", "en"], options | |
|
205 | ja = "Japanese (\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e)" | |||
|
206 | ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding) | |||
|
207 | assert_include [ja, "ja"], options | |||
206 | end |
|
208 | end | |
207 |
|
209 | |||
208 | def test_locales_validness |
|
210 | def test_locales_validness | |
209 | lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size |
|
211 | lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size | |
210 | assert_equal lang_files_count, valid_languages.size |
|
212 | assert_equal lang_files_count, valid_languages.size | |
211 | valid_languages.each do |lang| |
|
213 | valid_languages.each do |lang| | |
212 | assert set_language_if_valid(lang) |
|
214 | assert set_language_if_valid(lang) | |
213 | end |
|
215 | end | |
214 | set_language_if_valid('en') |
|
216 | set_language_if_valid('en') | |
215 | end |
|
217 | end | |
216 |
|
218 | |||
217 | def test_valid_language |
|
219 | def test_valid_language | |
218 | to_test = {'fr' => :fr, |
|
220 | to_test = {'fr' => :fr, | |
219 | 'Fr' => :fr, |
|
221 | 'Fr' => :fr, | |
220 | 'zh' => :zh, |
|
222 | 'zh' => :zh, | |
221 | 'zh-tw' => :"zh-TW", |
|
223 | 'zh-tw' => :"zh-TW", | |
222 | 'zh-TW' => :"zh-TW", |
|
224 | 'zh-TW' => :"zh-TW", | |
223 | 'zh-ZZ' => nil } |
|
225 | 'zh-ZZ' => nil } | |
224 | to_test.each {|lang, expected| assert_equal expected, find_language(lang)} |
|
226 | to_test.each {|lang, expected| assert_equal expected, find_language(lang)} | |
225 | end |
|
227 | end | |
226 |
|
228 | |||
227 | def test_fallback |
|
229 | def test_fallback | |
228 | ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"}) |
|
230 | ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"}) | |
229 | ::I18n.locale = 'en' |
|
231 | ::I18n.locale = 'en' | |
230 | assert_equal "Untranslated string", l(:untranslated) |
|
232 | assert_equal "Untranslated string", l(:untranslated) | |
231 | ::I18n.locale = 'fr' |
|
233 | ::I18n.locale = 'fr' | |
232 | assert_equal "Untranslated string", l(:untranslated) |
|
234 | assert_equal "Untranslated string", l(:untranslated) | |
233 |
|
235 | |||
234 | ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"}) |
|
236 | ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"}) | |
235 | ::I18n.locale = 'en' |
|
237 | ::I18n.locale = 'en' | |
236 | assert_equal "Untranslated string", l(:untranslated) |
|
238 | assert_equal "Untranslated string", l(:untranslated) | |
237 | ::I18n.locale = 'fr' |
|
239 | ::I18n.locale = 'fr' | |
238 | assert_equal "Pas de traduction", l(:untranslated) |
|
240 | assert_equal "Pas de traduction", l(:untranslated) | |
239 | end |
|
241 | end | |
240 |
|
242 | |||
241 | def test_utf8 |
|
243 | def test_utf8 | |
242 | set_language_if_valid 'ja' |
|
244 | set_language_if_valid 'ja' | |
243 | str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84" |
|
245 | str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84" | |
244 | i18n_ja_yes = l(:general_text_Yes) |
|
246 | i18n_ja_yes = l(:general_text_Yes) | |
245 | if str_ja_yes.respond_to?(:force_encoding) |
|
247 | if str_ja_yes.respond_to?(:force_encoding) | |
246 | str_ja_yes.force_encoding('UTF-8') |
|
248 | str_ja_yes.force_encoding('UTF-8') | |
247 | assert_equal "UTF-8", i18n_ja_yes.encoding.to_s |
|
249 | assert_equal "UTF-8", i18n_ja_yes.encoding.to_s | |
248 | end |
|
250 | end | |
249 | assert_equal str_ja_yes, i18n_ja_yes |
|
251 | assert_equal str_ja_yes, i18n_ja_yes | |
250 | end |
|
252 | end | |
251 |
|
253 | |||
252 | def test_traditional_chinese_locale |
|
254 | def test_traditional_chinese_locale | |
253 | set_language_if_valid 'zh-TW' |
|
255 | set_language_if_valid 'zh-TW' | |
254 | str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)" |
|
256 | str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)" | |
255 | if str_tw.respond_to?(:force_encoding) |
|
257 | if str_tw.respond_to?(:force_encoding) | |
256 | str_tw.force_encoding('UTF-8') |
|
258 | str_tw.force_encoding('UTF-8') | |
257 | end |
|
259 | end | |
258 | assert_equal str_tw, l(:general_lang_name) |
|
260 | assert_equal str_tw, l(:general_lang_name) | |
259 | end |
|
261 | end | |
260 |
|
262 | |||
261 | def test_french_locale |
|
263 | def test_french_locale | |
262 | set_language_if_valid 'fr' |
|
264 | set_language_if_valid 'fr' | |
263 | str_fr = "Fran\xc3\xa7ais" |
|
265 | str_fr = "Fran\xc3\xa7ais" | |
264 | if str_fr.respond_to?(:force_encoding) |
|
266 | if str_fr.respond_to?(:force_encoding) | |
265 | str_fr.force_encoding('UTF-8') |
|
267 | str_fr.force_encoding('UTF-8') | |
266 | end |
|
268 | end | |
267 | assert_equal str_fr, l(:general_lang_name) |
|
269 | assert_equal str_fr, l(:general_lang_name) | |
268 | end |
|
270 | end | |
269 | end |
|
271 | end |
General Comments 0
You need to be logged in to leave comments.
Login now