Display dates using user's locale if available (#2190).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9838 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Etienne Massip 2012-06-16 21:45:47 +00:00
parent 2c07b478bb
commit dbdc9b9da7
3 changed files with 38 additions and 7 deletions

View File

@ -6,10 +6,10 @@
<p><%= setting_select :default_language, lang_options_for_select(false) %></p>
<p><%= setting_select :start_of_week, [[day_name(1),'1'], [day_name(6),'6'], [day_name(7),'7']], :blank => :label_language_based %></p>
<% locale = User.current.language.blank? ? ::I18n.locale : User.current.language %>
<p><%= setting_select :date_format, Setting::DATE_FORMATS.collect {|f| [::I18n.l(Date.today, :locale => locale, :format => f), f]}, :blank => :label_language_based %></p>
<p><%= setting_select :date_format, Setting::DATE_FORMATS.collect {|f| [Date.today.strftime(f), f]}, :blank => :label_language_based %></p>
<p><%= setting_select :time_format, Setting::TIME_FORMATS.collect {|f| [Time.now.strftime(f), f]}, :blank => :label_language_based %></p>
<p><%= setting_select :time_format, Setting::TIME_FORMATS.collect {|f| [::I18n.l(Time.now, :locale => locale, :format => f), f]}, :blank => :label_language_based %></p>
<p><%= setting_select :user_format, @options[:user_format] %></p>

View File

@ -37,16 +37,21 @@ module Redmine
def format_date(date)
return nil unless date
Setting.date_format.blank? ? ::I18n.l(date.to_date) : date.strftime(Setting.date_format)
options = {}
options[:format] = Setting.date_format unless Setting.date_format.blank?
options[:locale] = User.current.language unless User.current.language.blank?
::I18n.l(date.to_date, options)
end
def format_time(time, include_date = true)
return nil unless time
options = {}
options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
options[:locale] = User.current.language unless User.current.language.blank?
time = time.to_time if time.is_a?(String)
zone = User.current.time_zone
local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
(include_date ? "#{format_date(local)} " : "") +
(Setting.time_format.blank? ? ::I18n.l(local, :format => :time) : local.strftime(Setting.time_format))
(include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
end
def day_name(day)

View File

@ -22,7 +22,7 @@ class Redmine::I18nTest < ActiveSupport::TestCase
include ActionView::Helpers::NumberHelper
def setup
@hook_module = Redmine::Hook
User.current.language = nil
end
def test_date_format_default
@ -39,6 +39,16 @@ class Redmine::I18nTest < ActiveSupport::TestCase
assert_equal today.strftime('%d %m %Y'), format_date(today)
end
def test_date_format_default_with_user_locale
set_language_if_valid 'es'
today = now = Time.parse('2011-02-20 14:00:00')
Setting.date_format = '%d %B %Y'
User.current.language = 'fr'
assert_equal "20 f\u00E9vrier 2011", format_date(today)
User.current.language = nil
assert_equal '20 Febrero 2011', format_date(today)
end
def test_date_and_time_for_each_language
Setting.date_format = ''
valid_languages.each do |lang|
@ -99,6 +109,22 @@ class Redmine::I18nTest < ActiveSupport::TestCase
end
end
def test_time_format_default_with_user_locale
set_language_if_valid 'en'
User.current.language = 'fr'
now = Time.parse('2011-02-20 15:45:22')
with_settings :time_format => '' do
with_settings :date_format => '' do
assert_equal '20/02/2011 15:45', format_time(now)
assert_equal '15:45', format_time(now, false)
end
with_settings :date_format => '%Y-%m-%d' do
assert_equal '2011-02-20 15:45', format_time(now)
assert_equal '15:45', format_time(now, false)
end
end
end
def test_time_format
set_language_if_valid 'en'
now = Time.now