@@ -1,16 +1,15 | |||||
1 | I18n.default_locale = 'en' |
|
1 | I18n.default_locale = 'en' | |
2 | # Adds fallback to default locale for untranslated strings |
|
2 | I18n.backend = Redmine::I18n::Backend.new | |
3 | I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) |
|
|||
4 |
|
3 | |||
5 | require 'redmine' |
|
4 | require 'redmine' | |
6 |
|
5 | |||
7 | # Load the secret token from the Redmine configuration file |
|
6 | # Load the secret token from the Redmine configuration file | |
8 | secret = Redmine::Configuration['secret_token'] |
|
7 | secret = Redmine::Configuration['secret_token'] | |
9 | if secret.present? |
|
8 | if secret.present? | |
10 | RedmineApp::Application.config.secret_token = secret |
|
9 | RedmineApp::Application.config.secret_token = secret | |
11 | end |
|
10 | end | |
12 |
|
11 | |||
13 | Redmine::Plugin.load |
|
12 | Redmine::Plugin.load | |
14 | unless Redmine::Configuration['mirror_plugins_assets_on_startup'] == false |
|
13 | unless Redmine::Configuration['mirror_plugins_assets_on_startup'] == false | |
15 | Redmine::Plugin.mirror_assets |
|
14 | Redmine::Plugin.mirror_assets | |
16 | end |
|
15 | end |
@@ -1,88 +1,157 | |||||
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 | options = {} |
|
40 | options = {} | |
41 | options[:format] = Setting.date_format unless Setting.date_format.blank? |
|
41 | options[:format] = Setting.date_format unless Setting.date_format.blank? | |
42 | options[:locale] = User.current.language unless User.current.language.blank? |
|
42 | options[:locale] = User.current.language unless User.current.language.blank? | |
43 | ::I18n.l(date.to_date, options) |
|
43 | ::I18n.l(date.to_date, options) | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def format_time(time, include_date = true) |
|
46 | def format_time(time, include_date = true) | |
47 | return nil unless time |
|
47 | return nil unless time | |
48 | options = {} |
|
48 | options = {} | |
49 | options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format) |
|
49 | options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format) | |
50 | options[:locale] = User.current.language unless User.current.language.blank? |
|
50 | options[:locale] = User.current.language unless User.current.language.blank? | |
51 | time = time.to_time if time.is_a?(String) |
|
51 | time = time.to_time if time.is_a?(String) | |
52 | zone = User.current.time_zone |
|
52 | zone = User.current.time_zone | |
53 | local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time) |
|
53 | local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time) | |
54 | (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options) |
|
54 | (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options) | |
55 | end |
|
55 | end | |
56 |
|
56 | |||
57 | def day_name(day) |
|
57 | def day_name(day) | |
58 | ::I18n.t('date.day_names')[day % 7] |
|
58 | ::I18n.t('date.day_names')[day % 7] | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | def day_letter(day) |
|
61 | def day_letter(day) | |
62 | ::I18n.t('date.abbr_day_names')[day % 7].first |
|
62 | ::I18n.t('date.abbr_day_names')[day % 7].first | |
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | def month_name(month) |
|
65 | def month_name(month) | |
66 | ::I18n.t('date.month_names')[month] |
|
66 | ::I18n.t('date.month_names')[month] | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def valid_languages |
|
69 | def valid_languages | |
70 | @@valid_languages ||= Dir.glob(File.join(Rails.root, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym) |
|
70 | ::I18n.available_locales | |
71 | end |
|
71 | end | |
72 |
|
72 | |||
73 | def find_language(lang) |
|
73 | def find_language(lang) | |
74 | @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k } |
|
74 | @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k } | |
75 | @@languages_lookup[lang.to_s.downcase] |
|
75 | @@languages_lookup[lang.to_s.downcase] | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | def set_language_if_valid(lang) |
|
78 | def set_language_if_valid(lang) | |
79 | if l = find_language(lang) |
|
79 | if l = find_language(lang) | |
80 | ::I18n.locale = l |
|
80 | ::I18n.locale = l | |
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def current_language |
|
84 | def current_language | |
85 | ::I18n.locale |
|
85 | ::I18n.locale | |
86 | end |
|
86 | end | |
|
87 | ||||
|
88 | # Custom backend based on I18n::Backend::Simple with the following changes: | |||
|
89 | # * lazy loading of translation files | |||
|
90 | # * available_locales are determined by looking at translation file names | |||
|
91 | class Backend | |||
|
92 | (class << self; self; end).class_eval { public :include } | |||
|
93 | ||||
|
94 | module Implementation | |||
|
95 | include ::I18n::Backend::Base | |||
|
96 | ||||
|
97 | # Stores translations for the given locale in memory. | |||
|
98 | # This uses a deep merge for the translations hash, so existing | |||
|
99 | # translations will be overwritten by new ones only at the deepest | |||
|
100 | # level of the hash. | |||
|
101 | def store_translations(locale, data, options = {}) | |||
|
102 | locale = locale.to_sym | |||
|
103 | translations[locale] ||= {} | |||
|
104 | data = data.deep_symbolize_keys | |||
|
105 | translations[locale].deep_merge!(data) | |||
|
106 | end | |||
|
107 | ||||
|
108 | # Get available locales from the translations filenames | |||
|
109 | def available_locales | |||
|
110 | @available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*').to_sym}.uniq.sort | |||
|
111 | end | |||
|
112 | ||||
|
113 | # Clean up translations | |||
|
114 | def reload! | |||
|
115 | @translations = nil | |||
|
116 | @available_locales = nil | |||
|
117 | super | |||
|
118 | end | |||
|
119 | ||||
|
120 | protected | |||
|
121 | ||||
|
122 | def init_translations(locale) | |||
|
123 | locale = locale.to_s | |||
|
124 | paths = ::I18n.load_path.select {|path| File.basename(path, '.*') == locale} | |||
|
125 | load_translations(paths) | |||
|
126 | translations[locale] ||= {} | |||
|
127 | end | |||
|
128 | ||||
|
129 | def translations | |||
|
130 | @translations ||= {} | |||
|
131 | end | |||
|
132 | ||||
|
133 | # Looks up a translation from the translations hash. Returns nil if | |||
|
134 | # eiher key is nil, or locale, scope or key do not exist as a key in the | |||
|
135 | # nested translations hash. Splits keys or scopes containing dots | |||
|
136 | # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as | |||
|
137 | # <tt>%w(currency format)</tt>. | |||
|
138 | def lookup(locale, key, scope = [], options = {}) | |||
|
139 | init_translations(locale) unless translations.key?(locale) | |||
|
140 | keys = ::I18n.normalize_keys(locale, key, scope, options[:separator]) | |||
|
141 | ||||
|
142 | keys.inject(translations) do |result, _key| | |||
|
143 | _key = _key.to_sym | |||
|
144 | return nil unless result.is_a?(Hash) && result.has_key?(_key) | |||
|
145 | result = result[_key] | |||
|
146 | result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol) | |||
|
147 | result | |||
|
148 | end | |||
|
149 | end | |||
|
150 | end | |||
|
151 | ||||
|
152 | include Implementation | |||
|
153 | # Adds fallback to default locale for untranslated strings | |||
|
154 | include ::I18n::Backend::Fallbacks | |||
|
155 | end | |||
87 | end |
|
156 | end | |
88 | end |
|
157 | end |
General Comments 0
You need to be logged in to leave comments.
Login now