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