Skip to content
This repository
Browse code

merge with dhh's style changes

  • Loading branch information...
commit 3696c9206551a0b7b85fa1bbd230040e0e6951bf 1 parent 36dd2bd
Sven Fuchs authored

Showing 2 changed files with 28 additions and 22 deletions. Show diff stats Hide diff stats

  1. +12 5 lib/i18n.rb
  2. +16 17 lib/i18n/backend/simple.rb
17 lib/i18n.rb
@@ -68,6 +68,13 @@ def load_path=(load_path)
68 68 @@load_path = load_path
69 69 end
70 70
  71 + # Tells the backend to reload translations. Used in situations like the
  72 + # Rails development environment. Backends can implement whatever strategy
  73 + # is useful.
  74 + def reload!
  75 + backend.reload!
  76 + end
  77 +
71 78 # Translates, pluralizes and interpolates a given key using a given locale,
72 79 # scope, and default, as well as interpolation values.
73 80 #
@@ -150,10 +157,10 @@ def load_path=(load_path)
150 157 # I18n.t [:foo, :bar], :scope => :baz
151 158 def translate(key, options = {})
152 159 locale = options.delete(:locale) || I18n.locale
153   - backend.translate locale, key, options
  160 + backend.translate(locale, key, options)
154 161 rescue I18n::ArgumentError => e
155 162 raise e if options[:raise]
156   - send @@exception_handler, e, locale, key, options
  163 + send(@@exception_handler, e, locale, key, options)
157 164 end
158 165 alias :t :translate
159 166
@@ -180,8 +187,8 @@ def default_exception_handler(exception, locale, key, options)
180 187 # keys are Symbols.
181 188 def normalize_translation_keys(locale, key, scope)
182 189 keys = [locale] + Array(scope) + [key]
183   - keys = keys.map{|k| k.to_s.split(/\./) }
184   - keys.flatten.map{|k| k.to_sym}
  190 + keys = keys.map { |k| k.to_s.split(/\./) }
  191 + keys.flatten.map { |k| k.to_sym }
185 192 end
186 193 end
187   -end
  194 +end
33 lib/i18n/backend/simple.rb
@@ -10,7 +10,7 @@ class Simple
10 10 # plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
11 11 # for details.
12 12 def load_translations(*filenames)
13   - filenames.each {|filename| load_file filename }
  13 + filenames.each { |filename| load_file(filename) }
14 14 end
15 15
16 16 # Stores translations for the given locale in memory.
@@ -23,12 +23,12 @@ def store_translations(locale, data)
23 23
24 24 def translate(locale, key, options = {})
25 25 raise InvalidLocale.new(locale) if locale.nil?
26   - return key.map{|k| translate locale, k, options } if key.is_a? Array
  26 + return key.map { |k| translate(locale, k, options) } if key.is_a? Array
27 27
28 28 reserved = :scope, :default
29 29 count, scope, default = options.values_at(:count, *reserved)
30 30 options.delete(:default)
31   - values = options.reject{|name, value| reserved.include? name }
  31 + values = options.reject { |name, value| reserved.include?(name) }
32 32
33 33 entry = lookup(locale, key, scope)
34 34 if entry.nil?
@@ -37,8 +37,8 @@ def translate(locale, key, options = {})
37 37 raise(I18n::MissingTranslationData.new(locale, key, options))
38 38 end
39 39 end
40   - entry = pluralize locale, entry, count
41   - entry = interpolate locale, entry, values
  40 + entry = pluralize(locale, entry, count)
  41 + entry = interpolate(locale, entry, values)
42 42 entry
43 43 end
44 44
@@ -68,14 +68,13 @@ def localize(locale, object, format = :default)
68 68 def initialized?
69 69 @initialized ||= false
70 70 end
71   -
  71 +
72 72 def reload!
73 73 @initialized = false
74 74 @translations = nil
75 75 end
76 76
77 77 protected
78   -
79 78 def init_translations
80 79 load_translations(*I18n.load_path)
81 80 @initialized = true
@@ -93,7 +92,7 @@ def translations
93 92 def lookup(locale, key, scope = [])
94 93 return unless key
95 94 init_translations unless initialized?
96   - keys = I18n.send :normalize_translation_keys, locale, key, scope
  95 + keys = I18n.send(:normalize_translation_keys, locale, key, scope)
97 96 keys.inject(translations) do |result, k|
98 97 if (x = result[k.to_sym]).nil?
99 98 return nil
@@ -175,21 +174,21 @@ def interpolate(locale, string, values = {})
175 174 # for all other file extensions.
176 175 def load_file(filename)
177 176 type = File.extname(filename).tr('.', '').downcase
178   - raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}"
  177 + raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}")
179 178 data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
180   - data.each{|locale, d| merge_translations locale, d }
  179 + data.each { |locale, d| merge_translations(locale, d) }
181 180 end
182 181
183 182 # Loads a plain Ruby translations file. eval'ing the file must yield
184 183 # a Hash containing translation data with locales as toplevel keys.
185 184 def load_rb(filename)
186   - eval IO.read(filename), binding, filename
  185 + eval(IO.read(filename), binding, filename)
187 186 end
188 187
189 188 # Loads a YAML translations file. The data must have locales as
190 189 # toplevel keys.
191 190 def load_yml(filename)
192   - YAML::load IO.read(filename)
  191 + YAML::load(IO.read(filename))
193 192 end
194 193
195 194 # Deep merges the given translations hash with the existing translations
@@ -197,16 +196,16 @@ def load_yml(filename)
197 196 def merge_translations(locale, data)
198 197 locale = locale.to_sym
199 198 translations[locale] ||= {}
200   - data = deep_symbolize_keys data
  199 + data = deep_symbolize_keys(data)
201 200
202 201 # deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
203   - merger = proc{|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
204   - translations[locale].merge! data, &merger
  202 + merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
  203 + translations[locale].merge!(data, &merger)
205 204 end
206 205
207 206 # Return a new hash with all keys and nested keys converted to symbols.
208 207 def deep_symbolize_keys(hash)
209   - hash.inject({}){|result, (key, value)|
  208 + hash.inject({}) { |result, (key, value)|
210 209 value = deep_symbolize_keys(value) if value.is_a? Hash
211 210 result[(key.to_sym rescue key) || key] = value
212 211 result
@@ -214,4 +213,4 @@ def deep_symbolize_keys(hash)
214 213 end
215 214 end
216 215 end
217   -end
  216 +end

1 comment on commit 3696c92

much better! thanks!

Please sign in to comment.
Something went wrong with that request. Please try again.