@@ -1,79 +1,79 | |||||
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 | Setting.date_format.blank? ? ::I18n.l(date.to_date) : date.strftime(Setting.date_format) | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | def format_time(time, include_date = true) |
|
43 | def format_time(time, include_date = true) | |
44 | return nil unless time |
|
44 | return nil unless time | |
45 | time = time.to_time if time.is_a?(String) |
|
45 | time = time.to_time if time.is_a?(String) | |
46 | zone = User.current.time_zone |
|
46 | zone = User.current.time_zone | |
47 | local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time) |
|
47 | local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time) | |
48 | Setting.time_format.blank? ? ::I18n.l(local, :format => (include_date ? :default : :time)) : |
|
48 | (include_date ? "#{format_date(local)} " : "") + | |
49 | ((include_date ? "#{format_date(time)} " : "") + "#{local.strftime(Setting.time_format)}") |
|
49 | (Setting.time_format.blank? ? ::I18n.l(local, :format => :time) : local.strftime(Setting.time_format)) | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | def day_name(day) |
|
52 | def day_name(day) | |
53 | ::I18n.t('date.day_names')[day % 7] |
|
53 | ::I18n.t('date.day_names')[day % 7] | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def month_name(month) |
|
56 | def month_name(month) | |
57 | ::I18n.t('date.month_names')[month] |
|
57 | ::I18n.t('date.month_names')[month] | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def valid_languages |
|
60 | 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) |
|
61 | @@valid_languages ||= Dir.glob(File.join(RAILS_ROOT, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym) | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def find_language(lang) |
|
64 | def find_language(lang) | |
65 | @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k } |
|
65 | @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k } | |
66 | @@languages_lookup[lang.to_s.downcase] |
|
66 | @@languages_lookup[lang.to_s.downcase] | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def set_language_if_valid(lang) |
|
69 | def set_language_if_valid(lang) | |
70 | if l = find_language(lang) |
|
70 | if l = find_language(lang) | |
71 | ::I18n.locale = l |
|
71 | ::I18n.locale = l | |
72 | end |
|
72 | end | |
73 | end |
|
73 | end | |
74 |
|
74 | |||
75 | def current_language |
|
75 | def current_language | |
76 | ::I18n.locale |
|
76 | ::I18n.locale | |
77 | end |
|
77 | end | |
78 | end |
|
78 | end | |
79 | end |
|
79 | end |
@@ -1,126 +1,149 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2009 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2009 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 | @hook_module = Redmine::Hook | |
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_and_time_for_each_language |
|
42 | def test_date_and_time_for_each_language | |
43 | Setting.date_format = '' |
|
43 | Setting.date_format = '' | |
44 | valid_languages.each do |lang| |
|
44 | valid_languages.each do |lang| | |
45 | set_language_if_valid lang |
|
45 | set_language_if_valid lang | |
46 | assert_nothing_raised "#{lang} failure" do |
|
46 | assert_nothing_raised "#{lang} failure" do | |
47 | format_date(Date.today) |
|
47 | format_date(Date.today) | |
48 | format_time(Time.now) |
|
48 | format_time(Time.now) | |
49 | format_time(Time.now, false) |
|
49 | format_time(Time.now, false) | |
50 | assert_not_equal 'default', ::I18n.l(Date.today, :format => :default), "date.formats.default missing in #{lang}" |
|
50 | assert_not_equal 'default', ::I18n.l(Date.today, :format => :default), "date.formats.default missing in #{lang}" | |
51 | assert_not_equal 'time', ::I18n.l(Time.now, :format => :time), "time.formats.time missing in #{lang}" |
|
51 | assert_not_equal 'time', ::I18n.l(Time.now, :format => :time), "time.formats.time missing in #{lang}" | |
52 | end |
|
52 | end | |
53 | assert l('date.day_names').is_a?(Array) |
|
53 | assert l('date.day_names').is_a?(Array) | |
54 | assert_equal 7, l('date.day_names').size |
|
54 | assert_equal 7, l('date.day_names').size | |
55 |
|
55 | |||
56 | assert l('date.month_names').is_a?(Array) |
|
56 | assert l('date.month_names').is_a?(Array) | |
57 | assert_equal 13, l('date.month_names').size |
|
57 | assert_equal 13, l('date.month_names').size | |
58 | end |
|
58 | end | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
|
61 | def test_time_format | |||
|
62 | set_language_if_valid 'en' | |||
|
63 | now = Time.parse('2011-02-20 15:45:22') | |||
|
64 | with_settings :time_format => '%H:%M' do | |||
|
65 | with_settings :date_format => '' do | |||
|
66 | assert_equal '02/20/2011 15:45', format_time(now) | |||
|
67 | assert_equal '15:45', format_time(now, false) | |||
|
68 | end | |||
|
69 | ||||
|
70 | with_settings :date_format => '%Y-%m-%d' do | |||
|
71 | assert_equal '2011-02-20 15:45', format_time(now) | |||
|
72 | assert_equal '15:45', format_time(now, false) | |||
|
73 | end | |||
|
74 | end | |||
|
75 | end | |||
|
76 | ||||
61 | def test_time_format_default |
|
77 | def test_time_format_default | |
62 | set_language_if_valid 'en' |
|
78 | set_language_if_valid 'en' | |
63 | now = Time.now |
|
79 | now = Time.parse('2011-02-20 15:45:22') | |
64 |
|
|
80 | with_settings :time_format => '' do | |
65 |
|
|
81 | with_settings :date_format => '' do | |
66 |
assert_equal |
|
82 | assert_equal '02/20/2011 03:45 pm', format_time(now) | |
67 |
assert_equal |
|
83 | assert_equal '03:45 pm', format_time(now, false) | |
|
84 | end | |||
|
85 | ||||
|
86 | with_settings :date_format => '%Y-%m-%d' do | |||
|
87 | assert_equal '2011-02-20 03:45 pm', format_time(now) | |||
|
88 | assert_equal '03:45 pm', format_time(now, false) | |||
|
89 | end | |||
|
90 | end | |||
68 | end |
|
91 | end | |
69 |
|
92 | |||
70 | def test_time_format |
|
93 | def test_time_format | |
71 | set_language_if_valid 'en' |
|
94 | set_language_if_valid 'en' | |
72 | now = Time.now |
|
95 | now = Time.now | |
73 | Setting.date_format = '%d %m %Y' |
|
96 | Setting.date_format = '%d %m %Y' | |
74 | Setting.time_format = '%H %M' |
|
97 | Setting.time_format = '%H %M' | |
75 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now) |
|
98 | assert_equal now.strftime('%d %m %Y %H %M'), format_time(now) | |
76 | assert_equal now.strftime('%H %M'), format_time(now, false) |
|
99 | assert_equal now.strftime('%H %M'), format_time(now, false) | |
77 | end |
|
100 | end | |
78 |
|
101 | |||
79 | def test_utc_time_format |
|
102 | def test_utc_time_format | |
80 | set_language_if_valid 'en' |
|
103 | set_language_if_valid 'en' | |
81 | now = Time.now.utc |
|
104 | now = Time.now.utc | |
82 | Setting.date_format = '%d %m %Y' |
|
105 | Setting.date_format = '%d %m %Y' | |
83 | Setting.time_format = '%H %M' |
|
106 | Setting.time_format = '%H %M' | |
84 | assert_equal Time.now.strftime('%d %m %Y %H %M'), format_time(now) |
|
107 | assert_equal Time.now.strftime('%d %m %Y %H %M'), format_time(now) | |
85 | assert_equal Time.now.strftime('%H %M'), format_time(now, false) |
|
108 | assert_equal Time.now.strftime('%H %M'), format_time(now, false) | |
86 | end |
|
109 | end | |
87 |
|
110 | |||
88 | def test_number_to_human_size_for_each_language |
|
111 | def test_number_to_human_size_for_each_language | |
89 | valid_languages.each do |lang| |
|
112 | valid_languages.each do |lang| | |
90 | set_language_if_valid lang |
|
113 | set_language_if_valid lang | |
91 | assert_nothing_raised "#{lang} failure" do |
|
114 | assert_nothing_raised "#{lang} failure" do | |
92 | number_to_human_size(1024*1024*4) |
|
115 | number_to_human_size(1024*1024*4) | |
93 | end |
|
116 | end | |
94 | end |
|
117 | end | |
95 | end |
|
118 | end | |
96 |
|
119 | |||
97 | def test_valid_languages |
|
120 | def test_valid_languages | |
98 | assert valid_languages.is_a?(Array) |
|
121 | assert valid_languages.is_a?(Array) | |
99 | assert valid_languages.first.is_a?(Symbol) |
|
122 | assert valid_languages.first.is_a?(Symbol) | |
100 | end |
|
123 | end | |
101 |
|
124 | |||
102 | def test_valid_language |
|
125 | def test_valid_language | |
103 | to_test = {'fr' => :fr, |
|
126 | to_test = {'fr' => :fr, | |
104 | 'Fr' => :fr, |
|
127 | 'Fr' => :fr, | |
105 | 'zh' => :zh, |
|
128 | 'zh' => :zh, | |
106 | 'zh-tw' => :"zh-TW", |
|
129 | 'zh-tw' => :"zh-TW", | |
107 | 'zh-TW' => :"zh-TW", |
|
130 | 'zh-TW' => :"zh-TW", | |
108 | 'zh-ZZ' => nil } |
|
131 | 'zh-ZZ' => nil } | |
109 |
|
132 | |||
110 | to_test.each {|lang, expected| assert_equal expected, find_language(lang)} |
|
133 | to_test.each {|lang, expected| assert_equal expected, find_language(lang)} | |
111 | end |
|
134 | end | |
112 |
|
135 | |||
113 | def test_fallback |
|
136 | def test_fallback | |
114 | ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"}) |
|
137 | ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"}) | |
115 | ::I18n.locale = 'en' |
|
138 | ::I18n.locale = 'en' | |
116 | assert_equal "Untranslated string", l(:untranslated) |
|
139 | assert_equal "Untranslated string", l(:untranslated) | |
117 | ::I18n.locale = 'fr' |
|
140 | ::I18n.locale = 'fr' | |
118 | assert_equal "Untranslated string", l(:untranslated) |
|
141 | assert_equal "Untranslated string", l(:untranslated) | |
119 |
|
142 | |||
120 | ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"}) |
|
143 | ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"}) | |
121 | ::I18n.locale = 'en' |
|
144 | ::I18n.locale = 'en' | |
122 | assert_equal "Untranslated string", l(:untranslated) |
|
145 | assert_equal "Untranslated string", l(:untranslated) | |
123 | ::I18n.locale = 'fr' |
|
146 | ::I18n.locale = 'fr' | |
124 | assert_equal "Pas de traduction", l(:untranslated) |
|
147 | assert_equal "Pas de traduction", l(:untranslated) | |
125 | end |
|
148 | end | |
126 | end |
|
149 | end |
General Comments 0
You need to be logged in to leave comments.
Login now