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