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