From dbdc9b9da7564eafd9d74aa9d664642193d9791d Mon Sep 17 00:00:00 2001 From: Etienne Massip Date: Sat, 16 Jun 2012 21:45:47 +0000 Subject: [PATCH] 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 --- app/views/settings/_display.html.erb | 6 +++--- lib/redmine/i18n.rb | 11 ++++++++--- test/unit/lib/redmine/i18n_test.rb | 28 +++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/views/settings/_display.html.erb b/app/views/settings/_display.html.erb index 3299ff10b..2eb0e2560 100644 --- a/app/views/settings/_display.html.erb +++ b/app/views/settings/_display.html.erb @@ -6,10 +6,10 @@

<%= setting_select :default_language, lang_options_for_select(false) %>

<%= setting_select :start_of_week, [[day_name(1),'1'], [day_name(6),'6'], [day_name(7),'7']], :blank => :label_language_based %>

+<% locale = User.current.language.blank? ? ::I18n.locale : User.current.language %> +

<%= setting_select :date_format, Setting::DATE_FORMATS.collect {|f| [::I18n.l(Date.today, :locale => locale, :format => f), f]}, :blank => :label_language_based %>

-

<%= setting_select :date_format, Setting::DATE_FORMATS.collect {|f| [Date.today.strftime(f), f]}, :blank => :label_language_based %>

- -

<%= setting_select :time_format, Setting::TIME_FORMATS.collect {|f| [Time.now.strftime(f), f]}, :blank => :label_language_based %>

+

<%= setting_select :time_format, Setting::TIME_FORMATS.collect {|f| [::I18n.l(Time.now, :locale => locale, :format => f), f]}, :blank => :label_language_based %>

<%= setting_select :user_format, @options[:user_format] %>

diff --git a/lib/redmine/i18n.rb b/lib/redmine/i18n.rb index a377d408c..fade9a9ae 100644 --- a/lib/redmine/i18n.rb +++ b/lib/redmine/i18n.rb @@ -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) diff --git a/test/unit/lib/redmine/i18n_test.rb b/test/unit/lib/redmine/i18n_test.rb index 0b7ee20f0..85d9a1312 100644 --- a/test/unit/lib/redmine/i18n_test.rb +++ b/test/unit/lib/redmine/i18n_test.rb @@ -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