@@ -1,6 +1,5 | |||
|
1 | 1 | I18n.default_locale = 'en' |
|
2 | # Adds fallback to default locale for untranslated strings | |
|
3 | I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) | |
|
2 | I18n.backend = Redmine::I18n::Backend.new | |
|
4 | 3 | |
|
5 | 4 | require 'redmine' |
|
6 | 5 |
@@ -67,7 +67,7 module Redmine | |||
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 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 | 71 | end |
|
72 | 72 | |
|
73 | 73 | def find_language(lang) |
@@ -84,5 +84,74 module Redmine | |||
|
84 | 84 | def current_language |
|
85 | 85 | ::I18n.locale |
|
86 | 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 | 156 | end |
|
88 | 157 | end |
General Comments 0
You need to be logged in to leave comments.
Login now