##// END OF EJS Templates
include ::I18n::Backend::Pluralization (#21856)...
Toshi MARUYAMA -
r14899:7bd38343dcf2
parent child
Show More
@@ -1,204 +1,205
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 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 module Redmine
18 module Redmine
19 module I18n
19 module I18n
20 def self.included(base)
20 def self.included(base)
21 base.extend Redmine::I18n
21 base.extend Redmine::I18n
22 end
22 end
23
23
24 def l(*args)
24 def l(*args)
25 case args.size
25 case args.size
26 when 1
26 when 1
27 ::I18n.t(*args)
27 ::I18n.t(*args)
28 when 2
28 when 2
29 if args.last.is_a?(Hash)
29 if args.last.is_a?(Hash)
30 ::I18n.t(*args)
30 ::I18n.t(*args)
31 elsif args.last.is_a?(String)
31 elsif args.last.is_a?(String)
32 ::I18n.t(args.first, :value => args.last)
32 ::I18n.t(args.first, :value => args.last)
33 else
33 else
34 ::I18n.t(args.first, :count => args.last)
34 ::I18n.t(args.first, :count => args.last)
35 end
35 end
36 else
36 else
37 raise "Translation string with multiple values: #{args.first}"
37 raise "Translation string with multiple values: #{args.first}"
38 end
38 end
39 end
39 end
40
40
41 def l_or_humanize(s, options={})
41 def l_or_humanize(s, options={})
42 k = "#{options[:prefix]}#{s}".to_sym
42 k = "#{options[:prefix]}#{s}".to_sym
43 ::I18n.t(k, :default => s.to_s.humanize)
43 ::I18n.t(k, :default => s.to_s.humanize)
44 end
44 end
45
45
46 def l_hours(hours)
46 def l_hours(hours)
47 hours = hours.to_f
47 hours = hours.to_f
48 l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
48 l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
49 end
49 end
50
50
51 def l_hours_short(hours)
51 def l_hours_short(hours)
52 l(:label_f_hour_short, :value => ("%.2f" % hours.to_f))
52 l(:label_f_hour_short, :value => ("%.2f" % hours.to_f))
53 end
53 end
54
54
55 def ll(lang, str, arg=nil)
55 def ll(lang, str, arg=nil)
56 options = arg.is_a?(Hash) ? arg : {:value => arg}
56 options = arg.is_a?(Hash) ? arg : {:value => arg}
57 locale = lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" }
57 locale = lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" }
58 ::I18n.t(str.to_s, options.merge(:locale => locale))
58 ::I18n.t(str.to_s, options.merge(:locale => locale))
59 end
59 end
60
60
61 # Localizes the given args with user's language
61 # Localizes the given args with user's language
62 def lu(user, *args)
62 def lu(user, *args)
63 lang = user.try(:language).presence || Setting.default_language
63 lang = user.try(:language).presence || Setting.default_language
64 ll(lang, *args)
64 ll(lang, *args)
65 end
65 end
66
66
67 def format_date(date)
67 def format_date(date)
68 return nil unless date
68 return nil unless date
69 options = {}
69 options = {}
70 options[:format] = Setting.date_format unless Setting.date_format.blank?
70 options[:format] = Setting.date_format unless Setting.date_format.blank?
71 ::I18n.l(date.to_date, options)
71 ::I18n.l(date.to_date, options)
72 end
72 end
73
73
74 def format_time(time, include_date=true, user=nil)
74 def format_time(time, include_date=true, user=nil)
75 return nil unless time
75 return nil unless time
76 user ||= User.current
76 user ||= User.current
77 options = {}
77 options = {}
78 options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
78 options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
79 time = time.to_time if time.is_a?(String)
79 time = time.to_time if time.is_a?(String)
80 zone = user.time_zone
80 zone = user.time_zone
81 local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
81 local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
82 (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
82 (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
83 end
83 end
84
84
85 def day_name(day)
85 def day_name(day)
86 ::I18n.t('date.day_names')[day % 7]
86 ::I18n.t('date.day_names')[day % 7]
87 end
87 end
88
88
89 def day_letter(day)
89 def day_letter(day)
90 ::I18n.t('date.abbr_day_names')[day % 7].first
90 ::I18n.t('date.abbr_day_names')[day % 7].first
91 end
91 end
92
92
93 def month_name(month)
93 def month_name(month)
94 ::I18n.t('date.month_names')[month]
94 ::I18n.t('date.month_names')[month]
95 end
95 end
96
96
97 def valid_languages
97 def valid_languages
98 ::I18n.available_locales
98 ::I18n.available_locales
99 end
99 end
100
100
101 # Returns an array of languages names and code sorted by names, example:
101 # Returns an array of languages names and code sorted by names, example:
102 # [["Deutsch", "de"], ["English", "en"] ...]
102 # [["Deutsch", "de"], ["English", "en"] ...]
103 #
103 #
104 # The result is cached to prevent from loading all translations files
104 # The result is cached to prevent from loading all translations files
105 # unless :cache => false option is given
105 # unless :cache => false option is given
106 def languages_options(options={})
106 def languages_options(options={})
107 options = if options[:cache] == false
107 options = if options[:cache] == false
108 valid_languages.
108 valid_languages.
109 select {|locale| ::I18n.exists?(:general_lang_name, locale)}.
109 select {|locale| ::I18n.exists?(:general_lang_name, locale)}.
110 map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.
110 map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.
111 sort {|x,y| x.first <=> y.first }
111 sort {|x,y| x.first <=> y.first }
112 else
112 else
113 ActionController::Base.cache_store.fetch "i18n/languages_options/#{Redmine::VERSION}" do
113 ActionController::Base.cache_store.fetch "i18n/languages_options/#{Redmine::VERSION}" do
114 languages_options :cache => false
114 languages_options :cache => false
115 end
115 end
116 end
116 end
117 options.map {|name, lang| [name.force_encoding("UTF-8"), lang.force_encoding("UTF-8")]}
117 options.map {|name, lang| [name.force_encoding("UTF-8"), lang.force_encoding("UTF-8")]}
118 end
118 end
119
119
120 def find_language(lang)
120 def find_language(lang)
121 @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
121 @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
122 @@languages_lookup[lang.to_s.downcase]
122 @@languages_lookup[lang.to_s.downcase]
123 end
123 end
124
124
125 def set_language_if_valid(lang)
125 def set_language_if_valid(lang)
126 if l = find_language(lang)
126 if l = find_language(lang)
127 ::I18n.locale = l
127 ::I18n.locale = l
128 end
128 end
129 end
129 end
130
130
131 def current_language
131 def current_language
132 ::I18n.locale
132 ::I18n.locale
133 end
133 end
134
134
135 # Custom backend based on I18n::Backend::Simple with the following changes:
135 # Custom backend based on I18n::Backend::Simple with the following changes:
136 # * lazy loading of translation files
136 # * lazy loading of translation files
137 # * available_locales are determined by looking at translation file names
137 # * available_locales are determined by looking at translation file names
138 class Backend
138 class Backend
139 (class << self; self; end).class_eval { public :include }
139 (class << self; self; end).class_eval { public :include }
140
140
141 module Implementation
141 module Implementation
142 include ::I18n::Backend::Base
142 include ::I18n::Backend::Base
143 include ::I18n::Backend::Pluralization
143
144
144 # Stores translations for the given locale in memory.
145 # Stores translations for the given locale in memory.
145 # This uses a deep merge for the translations hash, so existing
146 # This uses a deep merge for the translations hash, so existing
146 # translations will be overwritten by new ones only at the deepest
147 # translations will be overwritten by new ones only at the deepest
147 # level of the hash.
148 # level of the hash.
148 def store_translations(locale, data, options = {})
149 def store_translations(locale, data, options = {})
149 locale = locale.to_sym
150 locale = locale.to_sym
150 translations[locale] ||= {}
151 translations[locale] ||= {}
151 data = data.deep_symbolize_keys
152 data = data.deep_symbolize_keys
152 translations[locale].deep_merge!(data)
153 translations[locale].deep_merge!(data)
153 end
154 end
154
155
155 # Get available locales from the translations filenames
156 # Get available locales from the translations filenames
156 def available_locales
157 def available_locales
157 @available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
158 @available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
158 end
159 end
159
160
160 # Clean up translations
161 # Clean up translations
161 def reload!
162 def reload!
162 @translations = nil
163 @translations = nil
163 @available_locales = nil
164 @available_locales = nil
164 super
165 super
165 end
166 end
166
167
167 protected
168 protected
168
169
169 def init_translations(locale)
170 def init_translations(locale)
170 locale = locale.to_s
171 locale = locale.to_s
171 paths = ::I18n.load_path.select {|path| File.basename(path, '.*') == locale}
172 paths = ::I18n.load_path.select {|path| File.basename(path, '.*') == locale}
172 load_translations(paths)
173 load_translations(paths)
173 translations[locale] ||= {}
174 translations[locale] ||= {}
174 end
175 end
175
176
176 def translations
177 def translations
177 @translations ||= {}
178 @translations ||= {}
178 end
179 end
179
180
180 # Looks up a translation from the translations hash. Returns nil if
181 # Looks up a translation from the translations hash. Returns nil if
181 # eiher key is nil, or locale, scope or key do not exist as a key in the
182 # eiher key is nil, or locale, scope or key do not exist as a key in the
182 # nested translations hash. Splits keys or scopes containing dots
183 # nested translations hash. Splits keys or scopes containing dots
183 # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
184 # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
184 # <tt>%w(currency format)</tt>.
185 # <tt>%w(currency format)</tt>.
185 def lookup(locale, key, scope = [], options = {})
186 def lookup(locale, key, scope = [], options = {})
186 init_translations(locale) unless translations.key?(locale)
187 init_translations(locale) unless translations.key?(locale)
187 keys = ::I18n.normalize_keys(locale, key, scope, options[:separator])
188 keys = ::I18n.normalize_keys(locale, key, scope, options[:separator])
188
189
189 keys.inject(translations) do |result, _key|
190 keys.inject(translations) do |result, _key|
190 _key = _key.to_sym
191 _key = _key.to_sym
191 return nil unless result.is_a?(Hash) && result.has_key?(_key)
192 return nil unless result.is_a?(Hash) && result.has_key?(_key)
192 result = result[_key]
193 result = result[_key]
193 result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
194 result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
194 result
195 result
195 end
196 end
196 end
197 end
197 end
198 end
198
199
199 include Implementation
200 include Implementation
200 # Adds fallback to default locale for untranslated strings
201 # Adds fallback to default locale for untranslated strings
201 include ::I18n::Backend::Fallbacks
202 include ::I18n::Backend::Fallbacks
202 end
203 end
203 end
204 end
204 end
205 end
General Comments 0
You need to be logged in to leave comments. Login now