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