##// END OF EJS Templates
Display dates using user's locale if available (#2190)....
Etienne Massip -
r9655:dbdc9b9da756
parent child
Show More
@@ -1,22 +1,22
1 <%= form_tag({:action => 'edit', :tab => 'display'}) do %>
1 <%= form_tag({:action => 'edit', :tab => 'display'}) do %>
2
2
3 <div class="box tabular settings">
3 <div class="box tabular settings">
4 <p><%= setting_select :ui_theme, Redmine::Themes.themes.collect {|t| [t.name, t.id]}, :blank => :label_default, :label => :label_theme %></p>
4 <p><%= setting_select :ui_theme, Redmine::Themes.themes.collect {|t| [t.name, t.id]}, :blank => :label_default, :label => :label_theme %></p>
5
5
6 <p><%= setting_select :default_language, lang_options_for_select(false) %></p>
6 <p><%= setting_select :default_language, lang_options_for_select(false) %></p>
7
7
8 <p><%= setting_select :start_of_week, [[day_name(1),'1'], [day_name(6),'6'], [day_name(7),'7']], :blank => :label_language_based %></p>
8 <p><%= setting_select :start_of_week, [[day_name(1),'1'], [day_name(6),'6'], [day_name(7),'7']], :blank => :label_language_based %></p>
9 <% locale = User.current.language.blank? ? ::I18n.locale : User.current.language %>
10 <p><%= setting_select :date_format, Setting::DATE_FORMATS.collect {|f| [::I18n.l(Date.today, :locale => locale, :format => f), f]}, :blank => :label_language_based %></p>
9
11
10 <p><%= setting_select :date_format, Setting::DATE_FORMATS.collect {|f| [Date.today.strftime(f), f]}, :blank => :label_language_based %></p>
12 <p><%= setting_select :time_format, Setting::TIME_FORMATS.collect {|f| [::I18n.l(Time.now, :locale => locale, :format => f), f]}, :blank => :label_language_based %></p>
11
12 <p><%= setting_select :time_format, Setting::TIME_FORMATS.collect {|f| [Time.now.strftime(f), f]}, :blank => :label_language_based %></p>
13
13
14 <p><%= setting_select :user_format, @options[:user_format] %></p>
14 <p><%= setting_select :user_format, @options[:user_format] %></p>
15
15
16 <p><%= setting_check_box :gravatar_enabled %></p>
16 <p><%= setting_check_box :gravatar_enabled %></p>
17
17
18 <p><%= setting_select :gravatar_default, [["Wavatars", 'wavatar'], ["Identicons", 'identicon'], ["Monster ids", 'monsterid'], ["Retro", 'retro'], ["Mystery man", 'mm']], :blank => :label_none %></p>
18 <p><%= setting_select :gravatar_default, [["Wavatars", 'wavatar'], ["Identicons", 'identicon'], ["Monster ids", 'monsterid'], ["Retro", 'retro'], ["Mystery man", 'mm']], :blank => :label_none %></p>
19 </div>
19 </div>
20
20
21 <%= submit_tag l(:button_save) %>
21 <%= submit_tag l(:button_save) %>
22 <% end %>
22 <% end %>
@@ -1,79 +1,84
1 module Redmine
1 module Redmine
2 module I18n
2 module I18n
3 def self.included(base)
3 def self.included(base)
4 base.extend Redmine::I18n
4 base.extend Redmine::I18n
5 end
5 end
6
6
7 def l(*args)
7 def l(*args)
8 case args.size
8 case args.size
9 when 1
9 when 1
10 ::I18n.t(*args)
10 ::I18n.t(*args)
11 when 2
11 when 2
12 if args.last.is_a?(Hash)
12 if args.last.is_a?(Hash)
13 ::I18n.t(*args)
13 ::I18n.t(*args)
14 elsif args.last.is_a?(String)
14 elsif args.last.is_a?(String)
15 ::I18n.t(args.first, :value => args.last)
15 ::I18n.t(args.first, :value => args.last)
16 else
16 else
17 ::I18n.t(args.first, :count => args.last)
17 ::I18n.t(args.first, :count => args.last)
18 end
18 end
19 else
19 else
20 raise "Translation string with multiple values: #{args.first}"
20 raise "Translation string with multiple values: #{args.first}"
21 end
21 end
22 end
22 end
23
23
24 def l_or_humanize(s, options={})
24 def l_or_humanize(s, options={})
25 k = "#{options[:prefix]}#{s}".to_sym
25 k = "#{options[:prefix]}#{s}".to_sym
26 ::I18n.t(k, :default => s.to_s.humanize)
26 ::I18n.t(k, :default => s.to_s.humanize)
27 end
27 end
28
28
29 def l_hours(hours)
29 def l_hours(hours)
30 hours = hours.to_f
30 hours = hours.to_f
31 l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
31 l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
32 end
32 end
33
33
34 def ll(lang, str, value=nil)
34 def ll(lang, str, value=nil)
35 ::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" })
35 ::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" })
36 end
36 end
37
37
38 def format_date(date)
38 def format_date(date)
39 return nil unless date
39 return nil unless date
40 Setting.date_format.blank? ? ::I18n.l(date.to_date) : date.strftime(Setting.date_format)
40 options = {}
41 options[:format] = Setting.date_format unless Setting.date_format.blank?
42 options[:locale] = User.current.language unless User.current.language.blank?
43 ::I18n.l(date.to_date, options)
41 end
44 end
42
45
43 def format_time(time, include_date = true)
46 def format_time(time, include_date = true)
44 return nil unless time
47 return nil unless time
48 options = {}
49 options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
50 options[:locale] = User.current.language unless User.current.language.blank?
45 time = time.to_time if time.is_a?(String)
51 time = time.to_time if time.is_a?(String)
46 zone = User.current.time_zone
52 zone = User.current.time_zone
47 local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
53 local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
48 (include_date ? "#{format_date(local)} " : "") +
54 (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
49 (Setting.time_format.blank? ? ::I18n.l(local, :format => :time) : local.strftime(Setting.time_format))
50 end
55 end
51
56
52 def day_name(day)
57 def day_name(day)
53 ::I18n.t('date.day_names')[day % 7]
58 ::I18n.t('date.day_names')[day % 7]
54 end
59 end
55
60
56 def month_name(month)
61 def month_name(month)
57 ::I18n.t('date.month_names')[month]
62 ::I18n.t('date.month_names')[month]
58 end
63 end
59
64
60 def valid_languages
65 def valid_languages
61 @@valid_languages ||= Dir.glob(File.join(Rails.root, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym)
66 @@valid_languages ||= Dir.glob(File.join(Rails.root, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym)
62 end
67 end
63
68
64 def find_language(lang)
69 def find_language(lang)
65 @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
70 @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
66 @@languages_lookup[lang.to_s.downcase]
71 @@languages_lookup[lang.to_s.downcase]
67 end
72 end
68
73
69 def set_language_if_valid(lang)
74 def set_language_if_valid(lang)
70 if l = find_language(lang)
75 if l = find_language(lang)
71 ::I18n.locale = l
76 ::I18n.locale = l
72 end
77 end
73 end
78 end
74
79
75 def current_language
80 def current_language
76 ::I18n.locale
81 ::I18n.locale
77 end
82 end
78 end
83 end
79 end
84 end
@@ -1,178 +1,204
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 @hook_module = Redmine::Hook
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
43 set_language_if_valid 'es'
44 today = now = Time.parse('2011-02-20 14:00:00')
45 Setting.date_format = '%d %B %Y'
46 User.current.language = 'fr'
47 assert_equal "20 f\u00E9vrier 2011", format_date(today)
48 User.current.language = nil
49 assert_equal '20 Febrero 2011', format_date(today)
50 end
51
42 def test_date_and_time_for_each_language
52 def test_date_and_time_for_each_language
43 Setting.date_format = ''
53 Setting.date_format = ''
44 valid_languages.each do |lang|
54 valid_languages.each do |lang|
45 set_language_if_valid lang
55 set_language_if_valid lang
46 assert_nothing_raised "#{lang} failure" do
56 assert_nothing_raised "#{lang} failure" do
47 format_date(Date.today)
57 format_date(Date.today)
48 format_time(Time.now)
58 format_time(Time.now)
49 format_time(Time.now, false)
59 format_time(Time.now, false)
50 assert_not_equal 'default', ::I18n.l(Date.today, :format => :default),
60 assert_not_equal 'default', ::I18n.l(Date.today, :format => :default),
51 "date.formats.default missing in #{lang}"
61 "date.formats.default missing in #{lang}"
52 assert_not_equal 'time', ::I18n.l(Time.now, :format => :time),
62 assert_not_equal 'time', ::I18n.l(Time.now, :format => :time),
53 "time.formats.time missing in #{lang}"
63 "time.formats.time missing in #{lang}"
54 end
64 end
55 assert l('date.day_names').is_a?(Array)
65 assert l('date.day_names').is_a?(Array)
56 assert_equal 7, l('date.day_names').size
66 assert_equal 7, l('date.day_names').size
57
67
58 assert l('date.month_names').is_a?(Array)
68 assert l('date.month_names').is_a?(Array)
59 assert_equal 13, l('date.month_names').size
69 assert_equal 13, l('date.month_names').size
60 end
70 end
61 end
71 end
62
72
63 def test_time_for_each_zone
73 def test_time_for_each_zone
64 ActiveSupport::TimeZone.all.each do |zone|
74 ActiveSupport::TimeZone.all.each do |zone|
65 User.current.stubs(:time_zone).returns(zone.name)
75 User.current.stubs(:time_zone).returns(zone.name)
66 assert_nothing_raised "#{zone} failure" do
76 assert_nothing_raised "#{zone} failure" do
67 format_time(Time.now)
77 format_time(Time.now)
68 end
78 end
69 end
79 end
70 end
80 end
71
81
72 def test_time_format
82 def test_time_format
73 set_language_if_valid 'en'
83 set_language_if_valid 'en'
74 now = Time.parse('2011-02-20 15:45:22')
84 now = Time.parse('2011-02-20 15:45:22')
75 with_settings :time_format => '%H:%M' do
85 with_settings :time_format => '%H:%M' do
76 with_settings :date_format => '' do
86 with_settings :date_format => '' do
77 assert_equal '02/20/2011 15:45', format_time(now)
87 assert_equal '02/20/2011 15:45', format_time(now)
78 assert_equal '15:45', format_time(now, false)
88 assert_equal '15:45', format_time(now, false)
79 end
89 end
80 with_settings :date_format => '%Y-%m-%d' do
90 with_settings :date_format => '%Y-%m-%d' do
81 assert_equal '2011-02-20 15:45', format_time(now)
91 assert_equal '2011-02-20 15:45', format_time(now)
82 assert_equal '15:45', format_time(now, false)
92 assert_equal '15:45', format_time(now, false)
83 end
93 end
84 end
94 end
85 end
95 end
86
96
87 def test_time_format_default
97 def test_time_format_default
88 set_language_if_valid 'en'
98 set_language_if_valid 'en'
89 now = Time.parse('2011-02-20 15:45:22')
99 now = Time.parse('2011-02-20 15:45:22')
90 with_settings :time_format => '' do
100 with_settings :time_format => '' do
91 with_settings :date_format => '' do
101 with_settings :date_format => '' do
92 assert_equal '02/20/2011 03:45 pm', format_time(now)
102 assert_equal '02/20/2011 03:45 pm', format_time(now)
93 assert_equal '03:45 pm', format_time(now, false)
103 assert_equal '03:45 pm', format_time(now, false)
94 end
104 end
95 with_settings :date_format => '%Y-%m-%d' do
105 with_settings :date_format => '%Y-%m-%d' do
96 assert_equal '2011-02-20 03:45 pm', format_time(now)
106 assert_equal '2011-02-20 03:45 pm', format_time(now)
97 assert_equal '03:45 pm', format_time(now, false)
107 assert_equal '03:45 pm', format_time(now, false)
98 end
108 end
99 end
109 end
100 end
110 end
101
111
112 def test_time_format_default_with_user_locale
113 set_language_if_valid 'en'
114 User.current.language = 'fr'
115 now = Time.parse('2011-02-20 15:45:22')
116 with_settings :time_format => '' do
117 with_settings :date_format => '' do
118 assert_equal '20/02/2011 15:45', format_time(now)
119 assert_equal '15:45', format_time(now, false)
120 end
121 with_settings :date_format => '%Y-%m-%d' do
122 assert_equal '2011-02-20 15:45', format_time(now)
123 assert_equal '15:45', format_time(now, false)
124 end
125 end
126 end
127
102 def test_time_format
128 def test_time_format
103 set_language_if_valid 'en'
129 set_language_if_valid 'en'
104 now = Time.now
130 now = Time.now
105 Setting.date_format = '%d %m %Y'
131 Setting.date_format = '%d %m %Y'
106 Setting.time_format = '%H %M'
132 Setting.time_format = '%H %M'
107 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now)
133 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now)
108 assert_equal now.strftime('%H %M'), format_time(now, false)
134 assert_equal now.strftime('%H %M'), format_time(now, false)
109 end
135 end
110
136
111 def test_utc_time_format
137 def test_utc_time_format
112 set_language_if_valid 'en'
138 set_language_if_valid 'en'
113 now = Time.now
139 now = Time.now
114 Setting.date_format = '%d %m %Y'
140 Setting.date_format = '%d %m %Y'
115 Setting.time_format = '%H %M'
141 Setting.time_format = '%H %M'
116 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc)
142 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc)
117 assert_equal now.strftime('%H %M'), format_time(now.utc, false)
143 assert_equal now.strftime('%H %M'), format_time(now.utc, false)
118 end
144 end
119
145
120 def test_number_to_human_size_for_each_language
146 def test_number_to_human_size_for_each_language
121 valid_languages.each do |lang|
147 valid_languages.each do |lang|
122 set_language_if_valid lang
148 set_language_if_valid lang
123 assert_nothing_raised "#{lang} failure" do
149 assert_nothing_raised "#{lang} failure" do
124 size = number_to_human_size(257024)
150 size = number_to_human_size(257024)
125 assert_match /251/, size
151 assert_match /251/, size
126 end
152 end
127 end
153 end
128 end
154 end
129
155
130 def test_valid_languages
156 def test_valid_languages
131 assert valid_languages.is_a?(Array)
157 assert valid_languages.is_a?(Array)
132 assert valid_languages.first.is_a?(Symbol)
158 assert valid_languages.first.is_a?(Symbol)
133 end
159 end
134
160
135 def test_locales_validness
161 def test_locales_validness
136 lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
162 lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
137 assert_equal lang_files_count, valid_languages.size
163 assert_equal lang_files_count, valid_languages.size
138 valid_languages.each do |lang|
164 valid_languages.each do |lang|
139 assert set_language_if_valid(lang)
165 assert set_language_if_valid(lang)
140 end
166 end
141 set_language_if_valid('en')
167 set_language_if_valid('en')
142 end
168 end
143
169
144 def test_valid_language
170 def test_valid_language
145 to_test = {'fr' => :fr,
171 to_test = {'fr' => :fr,
146 'Fr' => :fr,
172 'Fr' => :fr,
147 'zh' => :zh,
173 'zh' => :zh,
148 'zh-tw' => :"zh-TW",
174 'zh-tw' => :"zh-TW",
149 'zh-TW' => :"zh-TW",
175 'zh-TW' => :"zh-TW",
150 'zh-ZZ' => nil }
176 'zh-ZZ' => nil }
151 to_test.each {|lang, expected| assert_equal expected, find_language(lang)}
177 to_test.each {|lang, expected| assert_equal expected, find_language(lang)}
152 end
178 end
153
179
154 def test_fallback
180 def test_fallback
155 ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"})
181 ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"})
156 ::I18n.locale = 'en'
182 ::I18n.locale = 'en'
157 assert_equal "Untranslated string", l(:untranslated)
183 assert_equal "Untranslated string", l(:untranslated)
158 ::I18n.locale = 'fr'
184 ::I18n.locale = 'fr'
159 assert_equal "Untranslated string", l(:untranslated)
185 assert_equal "Untranslated string", l(:untranslated)
160
186
161 ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"})
187 ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"})
162 ::I18n.locale = 'en'
188 ::I18n.locale = 'en'
163 assert_equal "Untranslated string", l(:untranslated)
189 assert_equal "Untranslated string", l(:untranslated)
164 ::I18n.locale = 'fr'
190 ::I18n.locale = 'fr'
165 assert_equal "Pas de traduction", l(:untranslated)
191 assert_equal "Pas de traduction", l(:untranslated)
166 end
192 end
167
193
168 def test_utf8
194 def test_utf8
169 set_language_if_valid 'ja'
195 set_language_if_valid 'ja'
170 str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84"
196 str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84"
171 i18n_ja_yes = l(:general_text_Yes)
197 i18n_ja_yes = l(:general_text_Yes)
172 if str_ja_yes.respond_to?(:force_encoding)
198 if str_ja_yes.respond_to?(:force_encoding)
173 str_ja_yes.force_encoding('UTF-8')
199 str_ja_yes.force_encoding('UTF-8')
174 assert_equal "UTF-8", i18n_ja_yes.encoding.to_s
200 assert_equal "UTF-8", i18n_ja_yes.encoding.to_s
175 end
201 end
176 assert_equal str_ja_yes, i18n_ja_yes
202 assert_equal str_ja_yes, i18n_ja_yes
177 end
203 end
178 end
204 end
General Comments 0
You need to be logged in to leave comments. Login now