@@ -1,204 +1,206 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class Redmine::I18nTest < ActiveSupport::TestCase |
|
21 | 21 | include Redmine::I18n |
|
22 | 22 | include ActionView::Helpers::NumberHelper |
|
23 | 23 | |
|
24 | 24 | def setup |
|
25 | 25 | User.current.language = nil |
|
26 | 26 | end |
|
27 | 27 | |
|
28 | 28 | def test_date_format_default |
|
29 | 29 | set_language_if_valid 'en' |
|
30 | 30 | today = Date.today |
|
31 | 31 | Setting.date_format = '' |
|
32 | 32 | assert_equal I18n.l(today), format_date(today) |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def test_date_format |
|
36 | 36 | set_language_if_valid 'en' |
|
37 | 37 | today = Date.today |
|
38 | 38 | Setting.date_format = '%d %m %Y' |
|
39 | 39 | assert_equal today.strftime('%d %m %Y'), format_date(today) |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | def test_date_format_default_with_user_locale |
|
43 | 43 | set_language_if_valid 'es' |
|
44 | 44 | today = now = Time.parse('2011-02-20 14:00:00') |
|
45 | 45 | Setting.date_format = '%d %B %Y' |
|
46 | 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 | 50 | User.current.language = nil |
|
49 | 51 | assert_equal '20 Febrero 2011', format_date(today) |
|
50 | 52 | end |
|
51 | 53 | |
|
52 | 54 | def test_date_and_time_for_each_language |
|
53 | 55 | Setting.date_format = '' |
|
54 | 56 | valid_languages.each do |lang| |
|
55 | 57 | set_language_if_valid lang |
|
56 | 58 | assert_nothing_raised "#{lang} failure" do |
|
57 | 59 | format_date(Date.today) |
|
58 | 60 | format_time(Time.now) |
|
59 | 61 | format_time(Time.now, false) |
|
60 | 62 | assert_not_equal 'default', ::I18n.l(Date.today, :format => :default), |
|
61 | 63 | "date.formats.default missing in #{lang}" |
|
62 | 64 | assert_not_equal 'time', ::I18n.l(Time.now, :format => :time), |
|
63 | 65 | "time.formats.time missing in #{lang}" |
|
64 | 66 | end |
|
65 | 67 | assert l('date.day_names').is_a?(Array) |
|
66 | 68 | assert_equal 7, l('date.day_names').size |
|
67 | 69 | |
|
68 | 70 | assert l('date.month_names').is_a?(Array) |
|
69 | 71 | assert_equal 13, l('date.month_names').size |
|
70 | 72 | end |
|
71 | 73 | end |
|
72 | 74 | |
|
73 | 75 | def test_time_for_each_zone |
|
74 | 76 | ActiveSupport::TimeZone.all.each do |zone| |
|
75 | 77 | User.current.stubs(:time_zone).returns(zone.name) |
|
76 | 78 | assert_nothing_raised "#{zone} failure" do |
|
77 | 79 | format_time(Time.now) |
|
78 | 80 | end |
|
79 | 81 | end |
|
80 | 82 | end |
|
81 | 83 | |
|
82 | 84 | def test_time_format |
|
83 | 85 | set_language_if_valid 'en' |
|
84 | 86 | now = Time.parse('2011-02-20 15:45:22') |
|
85 | 87 | with_settings :time_format => '%H:%M' do |
|
86 | 88 | with_settings :date_format => '' do |
|
87 | 89 | assert_equal '02/20/2011 15:45', format_time(now) |
|
88 | 90 | assert_equal '15:45', format_time(now, false) |
|
89 | 91 | end |
|
90 | 92 | with_settings :date_format => '%Y-%m-%d' do |
|
91 | 93 | assert_equal '2011-02-20 15:45', format_time(now) |
|
92 | 94 | assert_equal '15:45', format_time(now, false) |
|
93 | 95 | end |
|
94 | 96 | end |
|
95 | 97 | end |
|
96 | 98 | |
|
97 | 99 | def test_time_format_default |
|
98 | 100 | set_language_if_valid 'en' |
|
99 | 101 | now = Time.parse('2011-02-20 15:45:22') |
|
100 | 102 | with_settings :time_format => '' do |
|
101 | 103 | with_settings :date_format => '' do |
|
102 | 104 | assert_equal '02/20/2011 03:45 pm', format_time(now) |
|
103 | 105 | assert_equal '03:45 pm', format_time(now, false) |
|
104 | 106 | end |
|
105 | 107 | with_settings :date_format => '%Y-%m-%d' do |
|
106 | 108 | assert_equal '2011-02-20 03:45 pm', format_time(now) |
|
107 | 109 | assert_equal '03:45 pm', format_time(now, false) |
|
108 | 110 | end |
|
109 | 111 | end |
|
110 | 112 | end |
|
111 | 113 | |
|
112 | 114 | def test_time_format_default_with_user_locale |
|
113 | 115 | set_language_if_valid 'en' |
|
114 | 116 | User.current.language = 'fr' |
|
115 | 117 | now = Time.parse('2011-02-20 15:45:22') |
|
116 | 118 | with_settings :time_format => '' do |
|
117 | 119 | with_settings :date_format => '' do |
|
118 | 120 | assert_equal '20/02/2011 15:45', format_time(now) |
|
119 | 121 | assert_equal '15:45', format_time(now, false) |
|
120 | 122 | end |
|
121 | 123 | with_settings :date_format => '%Y-%m-%d' do |
|
122 | 124 | assert_equal '2011-02-20 15:45', format_time(now) |
|
123 | 125 | assert_equal '15:45', format_time(now, false) |
|
124 | 126 | end |
|
125 | 127 | end |
|
126 | 128 | end |
|
127 | 129 | |
|
128 | 130 | def test_time_format |
|
129 | 131 | set_language_if_valid 'en' |
|
130 | 132 | now = Time.now |
|
131 | 133 | Setting.date_format = '%d %m %Y' |
|
132 | 134 | Setting.time_format = '%H %M' |
|
133 | 135 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now) |
|
134 | 136 | assert_equal now.strftime('%H %M'), format_time(now, false) |
|
135 | 137 | end |
|
136 | 138 | |
|
137 | 139 | def test_utc_time_format |
|
138 | 140 | set_language_if_valid 'en' |
|
139 | 141 | now = Time.now |
|
140 | 142 | Setting.date_format = '%d %m %Y' |
|
141 | 143 | Setting.time_format = '%H %M' |
|
142 | 144 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc) |
|
143 | 145 | assert_equal now.strftime('%H %M'), format_time(now.utc, false) |
|
144 | 146 | end |
|
145 | 147 | |
|
146 | 148 | def test_number_to_human_size_for_each_language |
|
147 | 149 | valid_languages.each do |lang| |
|
148 | 150 | set_language_if_valid lang |
|
149 | 151 | assert_nothing_raised "#{lang} failure" do |
|
150 | 152 | size = number_to_human_size(257024) |
|
151 | 153 | assert_match /251/, size |
|
152 | 154 | end |
|
153 | 155 | end |
|
154 | 156 | end |
|
155 | 157 | |
|
156 | 158 | def test_valid_languages |
|
157 | 159 | assert valid_languages.is_a?(Array) |
|
158 | 160 | assert valid_languages.first.is_a?(Symbol) |
|
159 | 161 | end |
|
160 | 162 | |
|
161 | 163 | def test_locales_validness |
|
162 | 164 | lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size |
|
163 | 165 | assert_equal lang_files_count, valid_languages.size |
|
164 | 166 | valid_languages.each do |lang| |
|
165 | 167 | assert set_language_if_valid(lang) |
|
166 | 168 | end |
|
167 | 169 | set_language_if_valid('en') |
|
168 | 170 | end |
|
169 | 171 | |
|
170 | 172 | def test_valid_language |
|
171 | 173 | to_test = {'fr' => :fr, |
|
172 | 174 | 'Fr' => :fr, |
|
173 | 175 | 'zh' => :zh, |
|
174 | 176 | 'zh-tw' => :"zh-TW", |
|
175 | 177 | 'zh-TW' => :"zh-TW", |
|
176 | 178 | 'zh-ZZ' => nil } |
|
177 | 179 | to_test.each {|lang, expected| assert_equal expected, find_language(lang)} |
|
178 | 180 | end |
|
179 | 181 | |
|
180 | 182 | def test_fallback |
|
181 | 183 | ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"}) |
|
182 | 184 | ::I18n.locale = 'en' |
|
183 | 185 | assert_equal "Untranslated string", l(:untranslated) |
|
184 | 186 | ::I18n.locale = 'fr' |
|
185 | 187 | assert_equal "Untranslated string", l(:untranslated) |
|
186 | 188 | |
|
187 | 189 | ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"}) |
|
188 | 190 | ::I18n.locale = 'en' |
|
189 | 191 | assert_equal "Untranslated string", l(:untranslated) |
|
190 | 192 | ::I18n.locale = 'fr' |
|
191 | 193 | assert_equal "Pas de traduction", l(:untranslated) |
|
192 | 194 | end |
|
193 | 195 | |
|
194 | 196 | def test_utf8 |
|
195 | 197 | set_language_if_valid 'ja' |
|
196 | 198 | str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84" |
|
197 | 199 | i18n_ja_yes = l(:general_text_Yes) |
|
198 | 200 | if str_ja_yes.respond_to?(:force_encoding) |
|
199 | 201 | str_ja_yes.force_encoding('UTF-8') |
|
200 | 202 | assert_equal "UTF-8", i18n_ja_yes.encoding.to_s |
|
201 | 203 | end |
|
202 | 204 | assert_equal str_ja_yes, i18n_ja_yes |
|
203 | 205 | end |
|
204 | 206 | end |
General Comments 0
You need to be logged in to leave comments.
Login now