@@ -1,204 +1,206 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 test_date_format_default |
|
28 | def test_date_format_default | |
29 | set_language_if_valid 'en' |
|
29 | set_language_if_valid 'en' | |
30 | today = Date.today |
|
30 | today = Date.today | |
31 | Setting.date_format = '' |
|
31 | Setting.date_format = '' | |
32 | assert_equal I18n.l(today), format_date(today) |
|
32 | assert_equal I18n.l(today), format_date(today) | |
33 | end |
|
33 | end | |
34 |
|
34 | |||
35 | def test_date_format |
|
35 | def test_date_format | |
36 | set_language_if_valid 'en' |
|
36 | set_language_if_valid 'en' | |
37 | today = Date.today |
|
37 | today = Date.today | |
38 | Setting.date_format = '%d %m %Y' |
|
38 | Setting.date_format = '%d %m %Y' | |
39 | assert_equal today.strftime('%d %m %Y'), format_date(today) |
|
39 | assert_equal today.strftime('%d %m %Y'), format_date(today) | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | def test_date_format_default_with_user_locale |
|
42 | def test_date_format_default_with_user_locale | |
43 | set_language_if_valid 'es' |
|
43 | set_language_if_valid 'es' | |
44 | today = now = Time.parse('2011-02-20 14:00:00') |
|
44 | today = now = Time.parse('2011-02-20 14:00:00') | |
45 | Setting.date_format = '%d %B %Y' |
|
45 | Setting.date_format = '%d %B %Y' | |
46 | User.current.language = 'fr' |
|
46 | User.current.language = 'fr' | |
47 | assert_equal "20 f\u00E9vrier 2011", format_date(today) |
|
47 | s1 = "20 f\xc3\xa9vrier 2011" | |
|
48 | s1.force_encoding("UTF-8") if s1.respond_to?(:force_encoding) | |||
|
49 | assert_equal s1, format_date(today) | |||
48 | User.current.language = nil |
|
50 | User.current.language = nil | |
49 | assert_equal '20 Febrero 2011', format_date(today) |
|
51 | assert_equal '20 Febrero 2011', format_date(today) | |
50 | end |
|
52 | end | |
51 |
|
53 | |||
52 | def test_date_and_time_for_each_language |
|
54 | def test_date_and_time_for_each_language | |
53 | Setting.date_format = '' |
|
55 | Setting.date_format = '' | |
54 | valid_languages.each do |lang| |
|
56 | valid_languages.each do |lang| | |
55 | set_language_if_valid lang |
|
57 | set_language_if_valid lang | |
56 | assert_nothing_raised "#{lang} failure" do |
|
58 | assert_nothing_raised "#{lang} failure" do | |
57 | format_date(Date.today) |
|
59 | format_date(Date.today) | |
58 | format_time(Time.now) |
|
60 | format_time(Time.now) | |
59 | format_time(Time.now, false) |
|
61 | format_time(Time.now, false) | |
60 | assert_not_equal 'default', ::I18n.l(Date.today, :format => :default), |
|
62 | assert_not_equal 'default', ::I18n.l(Date.today, :format => :default), | |
61 | "date.formats.default missing in #{lang}" |
|
63 | "date.formats.default missing in #{lang}" | |
62 | assert_not_equal 'time', ::I18n.l(Time.now, :format => :time), |
|
64 | assert_not_equal 'time', ::I18n.l(Time.now, :format => :time), | |
63 | "time.formats.time missing in #{lang}" |
|
65 | "time.formats.time missing in #{lang}" | |
64 | end |
|
66 | end | |
65 | assert l('date.day_names').is_a?(Array) |
|
67 | assert l('date.day_names').is_a?(Array) | |
66 | assert_equal 7, l('date.day_names').size |
|
68 | assert_equal 7, l('date.day_names').size | |
67 |
|
69 | |||
68 | assert l('date.month_names').is_a?(Array) |
|
70 | assert l('date.month_names').is_a?(Array) | |
69 | assert_equal 13, l('date.month_names').size |
|
71 | assert_equal 13, l('date.month_names').size | |
70 | end |
|
72 | end | |
71 | end |
|
73 | end | |
72 |
|
74 | |||
73 | def test_time_for_each_zone |
|
75 | def test_time_for_each_zone | |
74 | ActiveSupport::TimeZone.all.each do |zone| |
|
76 | ActiveSupport::TimeZone.all.each do |zone| | |
75 | User.current.stubs(:time_zone).returns(zone.name) |
|
77 | User.current.stubs(:time_zone).returns(zone.name) | |
76 | assert_nothing_raised "#{zone} failure" do |
|
78 | assert_nothing_raised "#{zone} failure" do | |
77 | format_time(Time.now) |
|
79 | format_time(Time.now) | |
78 | end |
|
80 | end | |
79 | end |
|
81 | end | |
80 | end |
|
82 | end | |
81 |
|
83 | |||
82 | def test_time_format |
|
84 | def test_time_format | |
83 | set_language_if_valid 'en' |
|
85 | set_language_if_valid 'en' | |
84 | now = Time.parse('2011-02-20 15:45:22') |
|
86 | now = Time.parse('2011-02-20 15:45:22') | |
85 | with_settings :time_format => '%H:%M' do |
|
87 | with_settings :time_format => '%H:%M' do | |
86 | with_settings :date_format => '' do |
|
88 | with_settings :date_format => '' do | |
87 | assert_equal '02/20/2011 15:45', format_time(now) |
|
89 | assert_equal '02/20/2011 15:45', format_time(now) | |
88 | assert_equal '15:45', format_time(now, false) |
|
90 | assert_equal '15:45', format_time(now, false) | |
89 | end |
|
91 | end | |
90 | with_settings :date_format => '%Y-%m-%d' do |
|
92 | with_settings :date_format => '%Y-%m-%d' do | |
91 | assert_equal '2011-02-20 15:45', format_time(now) |
|
93 | assert_equal '2011-02-20 15:45', format_time(now) | |
92 | assert_equal '15:45', format_time(now, false) |
|
94 | assert_equal '15:45', format_time(now, false) | |
93 | end |
|
95 | end | |
94 | end |
|
96 | end | |
95 | end |
|
97 | end | |
96 |
|
98 | |||
97 | def test_time_format_default |
|
99 | def test_time_format_default | |
98 | set_language_if_valid 'en' |
|
100 | set_language_if_valid 'en' | |
99 | now = Time.parse('2011-02-20 15:45:22') |
|
101 | now = Time.parse('2011-02-20 15:45:22') | |
100 | with_settings :time_format => '' do |
|
102 | with_settings :time_format => '' do | |
101 | with_settings :date_format => '' do |
|
103 | with_settings :date_format => '' do | |
102 | assert_equal '02/20/2011 03:45 pm', format_time(now) |
|
104 | assert_equal '02/20/2011 03:45 pm', format_time(now) | |
103 | assert_equal '03:45 pm', format_time(now, false) |
|
105 | assert_equal '03:45 pm', format_time(now, false) | |
104 | end |
|
106 | end | |
105 | with_settings :date_format => '%Y-%m-%d' do |
|
107 | with_settings :date_format => '%Y-%m-%d' do | |
106 | assert_equal '2011-02-20 03:45 pm', format_time(now) |
|
108 | assert_equal '2011-02-20 03:45 pm', format_time(now) | |
107 | assert_equal '03:45 pm', format_time(now, false) |
|
109 | assert_equal '03:45 pm', format_time(now, false) | |
108 | end |
|
110 | end | |
109 | end |
|
111 | end | |
110 | end |
|
112 | end | |
111 |
|
113 | |||
112 | def test_time_format_default_with_user_locale |
|
114 | def test_time_format_default_with_user_locale | |
113 | set_language_if_valid 'en' |
|
115 | set_language_if_valid 'en' | |
114 | User.current.language = 'fr' |
|
116 | User.current.language = 'fr' | |
115 | now = Time.parse('2011-02-20 15:45:22') |
|
117 | now = Time.parse('2011-02-20 15:45:22') | |
116 | with_settings :time_format => '' do |
|
118 | with_settings :time_format => '' do | |
117 | with_settings :date_format => '' do |
|
119 | with_settings :date_format => '' do | |
118 | assert_equal '20/02/2011 15:45', format_time(now) |
|
120 | assert_equal '20/02/2011 15:45', format_time(now) | |
119 | assert_equal '15:45', format_time(now, false) |
|
121 | assert_equal '15:45', format_time(now, false) | |
120 | end |
|
122 | end | |
121 | with_settings :date_format => '%Y-%m-%d' do |
|
123 | with_settings :date_format => '%Y-%m-%d' do | |
122 | assert_equal '2011-02-20 15:45', format_time(now) |
|
124 | assert_equal '2011-02-20 15:45', format_time(now) | |
123 | assert_equal '15:45', format_time(now, false) |
|
125 | assert_equal '15:45', format_time(now, false) | |
124 | end |
|
126 | end | |
125 | end |
|
127 | end | |
126 | end |
|
128 | end | |
127 |
|
129 | |||
128 | def test_time_format |
|
130 | def test_time_format | |
129 | set_language_if_valid 'en' |
|
131 | set_language_if_valid 'en' | |
130 | now = Time.now |
|
132 | now = Time.now | |
131 | Setting.date_format = '%d %m %Y' |
|
133 | Setting.date_format = '%d %m %Y' | |
132 | Setting.time_format = '%H %M' |
|
134 | Setting.time_format = '%H %M' | |
133 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now) |
|
135 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now) | |
134 | assert_equal now.strftime('%H %M'), format_time(now, false) |
|
136 | assert_equal now.strftime('%H %M'), format_time(now, false) | |
135 | end |
|
137 | end | |
136 |
|
138 | |||
137 | def test_utc_time_format |
|
139 | def test_utc_time_format | |
138 | set_language_if_valid 'en' |
|
140 | set_language_if_valid 'en' | |
139 | now = Time.now |
|
141 | now = Time.now | |
140 | Setting.date_format = '%d %m %Y' |
|
142 | Setting.date_format = '%d %m %Y' | |
141 | Setting.time_format = '%H %M' |
|
143 | Setting.time_format = '%H %M' | |
142 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc) |
|
144 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc) | |
143 | assert_equal now.strftime('%H %M'), format_time(now.utc, false) |
|
145 | assert_equal now.strftime('%H %M'), format_time(now.utc, false) | |
144 | end |
|
146 | end | |
145 |
|
147 | |||
146 | def test_number_to_human_size_for_each_language |
|
148 | def test_number_to_human_size_for_each_language | |
147 | valid_languages.each do |lang| |
|
149 | valid_languages.each do |lang| | |
148 | set_language_if_valid lang |
|
150 | set_language_if_valid lang | |
149 | assert_nothing_raised "#{lang} failure" do |
|
151 | assert_nothing_raised "#{lang} failure" do | |
150 | size = number_to_human_size(257024) |
|
152 | size = number_to_human_size(257024) | |
151 | assert_match /251/, size |
|
153 | assert_match /251/, size | |
152 | end |
|
154 | end | |
153 | end |
|
155 | end | |
154 | end |
|
156 | end | |
155 |
|
157 | |||
156 | def test_valid_languages |
|
158 | def test_valid_languages | |
157 | assert valid_languages.is_a?(Array) |
|
159 | assert valid_languages.is_a?(Array) | |
158 | assert valid_languages.first.is_a?(Symbol) |
|
160 | assert valid_languages.first.is_a?(Symbol) | |
159 | end |
|
161 | end | |
160 |
|
162 | |||
161 | def test_locales_validness |
|
163 | def test_locales_validness | |
162 | lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size |
|
164 | lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size | |
163 | assert_equal lang_files_count, valid_languages.size |
|
165 | assert_equal lang_files_count, valid_languages.size | |
164 | valid_languages.each do |lang| |
|
166 | valid_languages.each do |lang| | |
165 | assert set_language_if_valid(lang) |
|
167 | assert set_language_if_valid(lang) | |
166 | end |
|
168 | end | |
167 | set_language_if_valid('en') |
|
169 | set_language_if_valid('en') | |
168 | end |
|
170 | end | |
169 |
|
171 | |||
170 | def test_valid_language |
|
172 | def test_valid_language | |
171 | to_test = {'fr' => :fr, |
|
173 | to_test = {'fr' => :fr, | |
172 | 'Fr' => :fr, |
|
174 | 'Fr' => :fr, | |
173 | 'zh' => :zh, |
|
175 | 'zh' => :zh, | |
174 | 'zh-tw' => :"zh-TW", |
|
176 | 'zh-tw' => :"zh-TW", | |
175 | 'zh-TW' => :"zh-TW", |
|
177 | 'zh-TW' => :"zh-TW", | |
176 | 'zh-ZZ' => nil } |
|
178 | 'zh-ZZ' => nil } | |
177 | to_test.each {|lang, expected| assert_equal expected, find_language(lang)} |
|
179 | to_test.each {|lang, expected| assert_equal expected, find_language(lang)} | |
178 | end |
|
180 | end | |
179 |
|
181 | |||
180 | def test_fallback |
|
182 | def test_fallback | |
181 | ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"}) |
|
183 | ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"}) | |
182 | ::I18n.locale = 'en' |
|
184 | ::I18n.locale = 'en' | |
183 | assert_equal "Untranslated string", l(:untranslated) |
|
185 | assert_equal "Untranslated string", l(:untranslated) | |
184 | ::I18n.locale = 'fr' |
|
186 | ::I18n.locale = 'fr' | |
185 | assert_equal "Untranslated string", l(:untranslated) |
|
187 | assert_equal "Untranslated string", l(:untranslated) | |
186 |
|
188 | |||
187 | ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"}) |
|
189 | ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"}) | |
188 | ::I18n.locale = 'en' |
|
190 | ::I18n.locale = 'en' | |
189 | assert_equal "Untranslated string", l(:untranslated) |
|
191 | assert_equal "Untranslated string", l(:untranslated) | |
190 | ::I18n.locale = 'fr' |
|
192 | ::I18n.locale = 'fr' | |
191 | assert_equal "Pas de traduction", l(:untranslated) |
|
193 | assert_equal "Pas de traduction", l(:untranslated) | |
192 | end |
|
194 | end | |
193 |
|
195 | |||
194 | def test_utf8 |
|
196 | def test_utf8 | |
195 | set_language_if_valid 'ja' |
|
197 | set_language_if_valid 'ja' | |
196 | str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84" |
|
198 | str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84" | |
197 | i18n_ja_yes = l(:general_text_Yes) |
|
199 | i18n_ja_yes = l(:general_text_Yes) | |
198 | if str_ja_yes.respond_to?(:force_encoding) |
|
200 | if str_ja_yes.respond_to?(:force_encoding) | |
199 | str_ja_yes.force_encoding('UTF-8') |
|
201 | str_ja_yes.force_encoding('UTF-8') | |
200 | assert_equal "UTF-8", i18n_ja_yes.encoding.to_s |
|
202 | assert_equal "UTF-8", i18n_ja_yes.encoding.to_s | |
201 | end |
|
203 | end | |
202 | assert_equal str_ja_yes, i18n_ja_yes |
|
204 | assert_equal str_ja_yes, i18n_ja_yes | |
203 | end |
|
205 | end | |
204 | end |
|
206 | end |
General Comments 0
You need to be logged in to leave comments.
Login now