##// END OF EJS Templates
Fixed that #l_hours_short shows 2 h (#21069)....
Jean-Philippe Lang -
r14384:f29aa17f1b4e
parent child
Show More
@@ -1,203 +1,203
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 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 module Redmine
19 19 module I18n
20 20 def self.included(base)
21 21 base.extend Redmine::I18n
22 22 end
23 23
24 24 def l(*args)
25 25 case args.size
26 26 when 1
27 27 ::I18n.t(*args)
28 28 when 2
29 29 if args.last.is_a?(Hash)
30 30 ::I18n.t(*args)
31 31 elsif args.last.is_a?(String)
32 32 ::I18n.t(args.first, :value => args.last)
33 33 else
34 34 ::I18n.t(args.first, :count => args.last)
35 35 end
36 36 else
37 37 raise "Translation string with multiple values: #{args.first}"
38 38 end
39 39 end
40 40
41 41 def l_or_humanize(s, options={})
42 42 k = "#{options[:prefix]}#{s}".to_sym
43 43 ::I18n.t(k, :default => s.to_s.humanize)
44 44 end
45 45
46 46 def l_hours(hours)
47 47 hours = hours.to_f
48 48 l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
49 49 end
50 50
51 51 def l_hours_short(hours)
52 l(:label_f_hour_short, :value => ("%.2f h" % hours.to_f))
52 l(:label_f_hour_short, :value => ("%.2f" % hours.to_f))
53 53 end
54 54
55 55 def ll(lang, str, arg=nil)
56 56 options = arg.is_a?(Hash) ? arg : {:value => arg}
57 57 locale = lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" }
58 58 ::I18n.t(str.to_s, options.merge(:locale => locale))
59 59 end
60 60
61 61 # Localizes the given args with user's language
62 62 def lu(user, *args)
63 63 lang = user.try(:language).presence || Setting.default_language
64 64 ll(lang, *args)
65 65 end
66 66
67 67 def format_date(date)
68 68 return nil unless date
69 69 options = {}
70 70 options[:format] = Setting.date_format unless Setting.date_format.blank?
71 71 ::I18n.l(date.to_date, options)
72 72 end
73 73
74 74 def format_time(time, include_date = true)
75 75 return nil unless time
76 76 options = {}
77 77 options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
78 78 time = time.to_time if time.is_a?(String)
79 79 zone = User.current.time_zone
80 80 local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
81 81 (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
82 82 end
83 83
84 84 def day_name(day)
85 85 ::I18n.t('date.day_names')[day % 7]
86 86 end
87 87
88 88 def day_letter(day)
89 89 ::I18n.t('date.abbr_day_names')[day % 7].first
90 90 end
91 91
92 92 def month_name(month)
93 93 ::I18n.t('date.month_names')[month]
94 94 end
95 95
96 96 def valid_languages
97 97 ::I18n.available_locales
98 98 end
99 99
100 100 # Returns an array of languages names and code sorted by names, example:
101 101 # [["Deutsch", "de"], ["English", "en"] ...]
102 102 #
103 103 # The result is cached to prevent from loading all translations files
104 104 # unless :cache => false option is given
105 105 def languages_options(options={})
106 106 options = if options[:cache] == false
107 107 valid_languages.
108 108 select {|locale| ::I18n.exists?(:general_lang_name, locale)}.
109 109 map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.
110 110 sort {|x,y| x.first <=> y.first }
111 111 else
112 112 ActionController::Base.cache_store.fetch "i18n/languages_options/#{Redmine::VERSION}" do
113 113 languages_options :cache => false
114 114 end
115 115 end
116 116 options.map {|name, lang| [name.force_encoding("UTF-8"), lang.force_encoding("UTF-8")]}
117 117 end
118 118
119 119 def find_language(lang)
120 120 @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
121 121 @@languages_lookup[lang.to_s.downcase]
122 122 end
123 123
124 124 def set_language_if_valid(lang)
125 125 if l = find_language(lang)
126 126 ::I18n.locale = l
127 127 end
128 128 end
129 129
130 130 def current_language
131 131 ::I18n.locale
132 132 end
133 133
134 134 # Custom backend based on I18n::Backend::Simple with the following changes:
135 135 # * lazy loading of translation files
136 136 # * available_locales are determined by looking at translation file names
137 137 class Backend
138 138 (class << self; self; end).class_eval { public :include }
139 139
140 140 module Implementation
141 141 include ::I18n::Backend::Base
142 142
143 143 # Stores translations for the given locale in memory.
144 144 # This uses a deep merge for the translations hash, so existing
145 145 # translations will be overwritten by new ones only at the deepest
146 146 # level of the hash.
147 147 def store_translations(locale, data, options = {})
148 148 locale = locale.to_sym
149 149 translations[locale] ||= {}
150 150 data = data.deep_symbolize_keys
151 151 translations[locale].deep_merge!(data)
152 152 end
153 153
154 154 # Get available locales from the translations filenames
155 155 def available_locales
156 156 @available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
157 157 end
158 158
159 159 # Clean up translations
160 160 def reload!
161 161 @translations = nil
162 162 @available_locales = nil
163 163 super
164 164 end
165 165
166 166 protected
167 167
168 168 def init_translations(locale)
169 169 locale = locale.to_s
170 170 paths = ::I18n.load_path.select {|path| File.basename(path, '.*') == locale}
171 171 load_translations(paths)
172 172 translations[locale] ||= {}
173 173 end
174 174
175 175 def translations
176 176 @translations ||= {}
177 177 end
178 178
179 179 # Looks up a translation from the translations hash. Returns nil if
180 180 # eiher key is nil, or locale, scope or key do not exist as a key in the
181 181 # nested translations hash. Splits keys or scopes containing dots
182 182 # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
183 183 # <tt>%w(currency format)</tt>.
184 184 def lookup(locale, key, scope = [], options = {})
185 185 init_translations(locale) unless translations.key?(locale)
186 186 keys = ::I18n.normalize_keys(locale, key, scope, options[:separator])
187 187
188 188 keys.inject(translations) do |result, _key|
189 189 _key = _key.to_sym
190 190 return nil unless result.is_a?(Hash) && result.has_key?(_key)
191 191 result = result[_key]
192 192 result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
193 193 result
194 194 end
195 195 end
196 196 end
197 197
198 198 include Implementation
199 199 # Adds fallback to default locale for untranslated strings
200 200 include ::I18n::Backend::Fallbacks
201 201 end
202 202 end
203 203 end
@@ -1,253 +1,258
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 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 = nil
26 26 end
27 27
28 28 def teardown
29 29 set_language_if_valid 'en'
30 30 end
31 31
32 32 def test_date_format_default
33 33 set_language_if_valid 'en'
34 34 today = Date.today
35 35 with_settings :date_format => '' do
36 36 assert_equal I18n.l(today), format_date(today)
37 37 end
38 38 end
39 39
40 40 def test_date_format
41 41 set_language_if_valid 'en'
42 42 today = Date.today
43 43 with_settings :date_format => '%d %m %Y' do
44 44 assert_equal today.strftime('%d %m %Y'), format_date(today)
45 45 end
46 46 end
47 47
48 48 def test_date_format_with_month_name_should_translate_with_current_locale
49 49 set_language_if_valid 'es'
50 50 date = Date.parse('2011-02-20 14:00:00')
51 51 with_settings :date_format => '%d %B %Y' do
52 52 assert_equal '20 Febrero 2011', format_date(date)
53 53 end
54 54 end
55 55
56 56 def test_date_and_time_for_each_language
57 57 with_settings :date_format => '' do
58 58 valid_languages.each do |lang|
59 59 set_language_if_valid lang
60 60 assert_nothing_raised "#{lang} failure" do
61 61 format_date(Date.today)
62 62 format_time(Time.now)
63 63 format_time(Time.now, false)
64 64 assert_not_equal 'default', ::I18n.l(Date.today, :format => :default),
65 65 "date.formats.default missing in #{lang}"
66 66 assert_not_equal 'time', ::I18n.l(Time.now, :format => :time),
67 67 "time.formats.time missing in #{lang}"
68 68 end
69 69 assert l('date.day_names').is_a?(Array)
70 70 assert_equal 7, l('date.day_names').size
71 71
72 72 assert l('date.month_names').is_a?(Array)
73 73 assert_equal 13, l('date.month_names').size
74 74 end
75 75 end
76 76 end
77 77
78 78 def test_time_for_each_zone
79 79 ActiveSupport::TimeZone.all.each do |zone|
80 80 User.current.stubs(:time_zone).returns(zone.name)
81 81 assert_nothing_raised "#{zone} failure" do
82 82 format_time(Time.now)
83 83 end
84 84 end
85 85 end
86 86
87 87 def test_time_format
88 88 set_language_if_valid 'en'
89 89 now = Time.parse('2011-02-20 15:45:22')
90 90 with_settings :time_format => '%H:%M' do
91 91 with_settings :date_format => '' do
92 92 assert_equal '02/20/2011 15:45', format_time(now)
93 93 assert_equal '15:45', format_time(now, false)
94 94 end
95 95 with_settings :date_format => '%Y-%m-%d' do
96 96 assert_equal '2011-02-20 15:45', format_time(now)
97 97 assert_equal '15:45', format_time(now, false)
98 98 end
99 99 end
100 100 end
101 101
102 102 def test_time_format_default
103 103 set_language_if_valid 'en'
104 104 now = Time.parse('2011-02-20 15:45:22')
105 105 with_settings :time_format => '' do
106 106 with_settings :date_format => '' do
107 107 assert_equal '02/20/2011 03:45 PM', format_time(now)
108 108 assert_equal '03:45 PM', format_time(now, false)
109 109 end
110 110 with_settings :date_format => '%Y-%m-%d' do
111 111 assert_equal '2011-02-20 03:45 PM', format_time(now)
112 112 assert_equal '03:45 PM', format_time(now, false)
113 113 end
114 114 end
115 115 end
116 116
117 117 def test_utc_time_format
118 118 set_language_if_valid 'en'
119 119 now = Time.now
120 120 with_settings :date_format => '%d %m %Y', :time_format => '%H %M' do
121 121 assert_equal now.localtime.strftime('%d %m %Y %H %M'), format_time(now.utc), "User time zone was #{User.current.time_zone}"
122 122 assert_equal now.localtime.strftime('%H %M'), format_time(now.utc, false)
123 123 end
124 124 end
125 125
126 126 def test_number_to_human_size_for_each_language
127 127 valid_languages.each do |lang|
128 128 set_language_if_valid lang
129 129 assert_nothing_raised "#{lang} failure" do
130 130 size = number_to_human_size(257024)
131 131 assert_match /251/, size
132 132 end
133 133 end
134 134 end
135 135
136 136 def test_day_name
137 137 set_language_if_valid 'fr'
138 138 assert_equal 'dimanche', day_name(0)
139 139 assert_equal 'jeudi', day_name(4)
140 140 end
141 141
142 142 def test_day_letter
143 143 set_language_if_valid 'fr'
144 144 assert_equal 'd', day_letter(0)
145 145 assert_equal 'j', day_letter(4)
146 146 end
147 147
148 148 def test_number_to_currency_for_each_language
149 149 valid_languages.each do |lang|
150 150 set_language_if_valid lang
151 151 assert_nothing_raised "#{lang} failure" do
152 152 number_to_currency(-1000.2)
153 153 end
154 154 end
155 155 end
156 156
157 def test_l_hours_short
158 set_language_if_valid 'en'
159 assert_equal '2.00 h', l_hours_short(2.0)
160 end
161
157 162 def test_number_to_currency_default
158 163 set_language_if_valid 'bs'
159 164 assert_equal "KM -1000,20", number_to_currency(-1000.2)
160 165 set_language_if_valid 'de'
161 166 euro_sign = "\xe2\x82\xac".force_encoding('UTF-8')
162 167 assert_equal "-1000,20 #{euro_sign}", number_to_currency(-1000.2)
163 168 end
164 169
165 170 def test_lu_should_not_error_when_user_language_is_an_empty_string
166 171 user = User.new
167 172 user.language = ''
168 173
169 174 assert_nothing_raised do
170 175 lu(user, :label_issue)
171 176 end
172 177 end
173 178
174 179 def test_valid_languages
175 180 assert valid_languages.is_a?(Array)
176 181 assert valid_languages.first.is_a?(Symbol)
177 182 end
178 183
179 184 def test_languages_options
180 185 options = languages_options
181 186 assert options.is_a?(Array)
182 187 assert_equal valid_languages.size, options.size
183 188 assert_nil options.detect {|option| !option.is_a?(Array)}
184 189 assert_nil options.detect {|option| option.size != 2}
185 190 assert_nil options.detect {|option| !option.first.is_a?(String) || !option.last.is_a?(String)}
186 191 assert_include ["English", "en"], options
187 192 ja = "Japanese (\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e)".force_encoding('UTF-8')
188 193 assert_include [ja, "ja"], options
189 194 end
190 195
191 196 def test_languages_options_should_return_strings_with_utf8_encoding
192 197 strings = languages_options.flatten
193 198 assert_equal ["UTF-8"], strings.map(&:encoding).uniq.map(&:name).sort
194 199 end
195 200
196 201 def test_languages_options_should_ignore_locales_without_general_lang_name_key
197 202 stubs(:valid_languages).returns([:en, :foo])
198 203 assert_equal [["English", "en"]], languages_options(:cache => false)
199 204 end
200 205
201 206 def test_locales_validness
202 207 lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
203 208 assert_equal lang_files_count, valid_languages.size
204 209 valid_languages.each do |lang|
205 210 assert set_language_if_valid(lang)
206 211 end
207 212 set_language_if_valid('en')
208 213 end
209 214
210 215 def test_valid_language
211 216 to_test = {'fr' => :fr,
212 217 'Fr' => :fr,
213 218 'zh' => :zh,
214 219 'zh-tw' => :"zh-TW",
215 220 'zh-TW' => :"zh-TW",
216 221 'zh-ZZ' => nil }
217 222 to_test.each {|lang, expected| assert_equal expected, find_language(lang)}
218 223 end
219 224
220 225 def test_fallback
221 226 ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"})
222 227 ::I18n.locale = 'en'
223 228 assert_equal "Untranslated string", l(:untranslated)
224 229 ::I18n.locale = 'fr'
225 230 assert_equal "Untranslated string", l(:untranslated)
226 231
227 232 ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"})
228 233 ::I18n.locale = 'en'
229 234 assert_equal "Untranslated string", l(:untranslated)
230 235 ::I18n.locale = 'fr'
231 236 assert_equal "Pas de traduction", l(:untranslated)
232 237 end
233 238
234 239 def test_utf8
235 240 set_language_if_valid 'ja'
236 241 str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84".force_encoding('UTF-8')
237 242 i18n_ja_yes = l(:general_text_Yes)
238 243 assert_equal str_ja_yes, i18n_ja_yes
239 244 assert_equal "UTF-8", i18n_ja_yes.encoding.to_s
240 245 end
241 246
242 247 def test_traditional_chinese_locale
243 248 set_language_if_valid 'zh-TW'
244 249 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)".force_encoding('UTF-8')
245 250 assert_equal str_tw, l(:general_lang_name)
246 251 end
247 252
248 253 def test_french_locale
249 254 set_language_if_valid 'fr'
250 255 str_fr = "French (Fran\xc3\xa7ais)".force_encoding('UTF-8')
251 256 assert_equal str_fr, l(:general_lang_name)
252 257 end
253 258 end
General Comments 0
You need to be logged in to leave comments. Login now