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