Browse files

merge with dhh's style changes

  • Loading branch information...
1 parent 36dd2bd commit 3696c9206551a0b7b85fa1bbd230040e0e6951bf @svenfuchs committed Oct 28, 2008
Showing with 28 additions and 22 deletions.
  1. +12 −5 lib/i18n.rb
  2. +16 −17 lib/i18n/backend/simple.rb
View
17 lib/i18n.rb
@@ -68,6 +68,13 @@ def load_path=(load_path)
@@load_path = load_path
end
+ # Tells the backend to reload translations. Used in situations like the
+ # Rails development environment. Backends can implement whatever strategy
+ # is useful.
+ def reload!
+ backend.reload!
+ end
+
# Translates, pluralizes and interpolates a given key using a given locale,
# scope, and default, as well as interpolation values.
#
@@ -150,10 +157,10 @@ def load_path=(load_path)
# I18n.t [:foo, :bar], :scope => :baz
def translate(key, options = {})
locale = options.delete(:locale) || I18n.locale
- backend.translate locale, key, options
+ backend.translate(locale, key, options)
rescue I18n::ArgumentError => e
raise e if options[:raise]
- send @@exception_handler, e, locale, key, options
+ send(@@exception_handler, e, locale, key, options)
end
alias :t :translate
@@ -180,8 +187,8 @@ def default_exception_handler(exception, locale, key, options)
# keys are Symbols.
def normalize_translation_keys(locale, key, scope)
keys = [locale] + Array(scope) + [key]
- keys = keys.map{|k| k.to_s.split(/\./) }
- keys.flatten.map{|k| k.to_sym}
+ keys = keys.map { |k| k.to_s.split(/\./) }
+ keys.flatten.map { |k| k.to_sym }
end
end
-end
+end
View
33 lib/i18n/backend/simple.rb
@@ -10,7 +10,7 @@ class Simple
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
# for details.
def load_translations(*filenames)
- filenames.each {|filename| load_file filename }
+ filenames.each { |filename| load_file(filename) }
end
# Stores translations for the given locale in memory.
@@ -23,12 +23,12 @@ def store_translations(locale, data)
def translate(locale, key, options = {})
raise InvalidLocale.new(locale) if locale.nil?
- return key.map{|k| translate locale, k, options } if key.is_a? Array
+ return key.map { |k| translate(locale, k, options) } if key.is_a? Array
reserved = :scope, :default
count, scope, default = options.values_at(:count, *reserved)
options.delete(:default)
- values = options.reject{|name, value| reserved.include? name }
+ values = options.reject { |name, value| reserved.include?(name) }
entry = lookup(locale, key, scope)
if entry.nil?
@@ -37,8 +37,8 @@ def translate(locale, key, options = {})
raise(I18n::MissingTranslationData.new(locale, key, options))
end
end
- entry = pluralize locale, entry, count
- entry = interpolate locale, entry, values
+ entry = pluralize(locale, entry, count)
+ entry = interpolate(locale, entry, values)
entry
end
@@ -68,14 +68,13 @@ def localize(locale, object, format = :default)
def initialized?
@initialized ||= false
end
-
+
def reload!
@initialized = false
@translations = nil
end
protected
-
def init_translations
load_translations(*I18n.load_path)
@initialized = true
@@ -93,7 +92,7 @@ def translations
def lookup(locale, key, scope = [])
return unless key
init_translations unless initialized?
- keys = I18n.send :normalize_translation_keys, locale, key, scope
+ keys = I18n.send(:normalize_translation_keys, locale, key, scope)
keys.inject(translations) do |result, k|
if (x = result[k.to_sym]).nil?
return nil
@@ -175,43 +174,43 @@ def interpolate(locale, string, values = {})
# for all other file extensions.
def load_file(filename)
type = File.extname(filename).tr('.', '').downcase
- raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}"
+ raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}")
data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
- data.each{|locale, d| merge_translations locale, d }
+ data.each { |locale, d| merge_translations(locale, d) }
end
# Loads a plain Ruby translations file. eval'ing the file must yield
# a Hash containing translation data with locales as toplevel keys.
def load_rb(filename)
- eval IO.read(filename), binding, filename
+ eval(IO.read(filename), binding, filename)
end
# Loads a YAML translations file. The data must have locales as
# toplevel keys.
def load_yml(filename)
- YAML::load IO.read(filename)
+ YAML::load(IO.read(filename))
end
# Deep merges the given translations hash with the existing translations
# for the given locale
def merge_translations(locale, data)
locale = locale.to_sym
translations[locale] ||= {}
- data = deep_symbolize_keys data
+ data = deep_symbolize_keys(data)
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
- merger = proc{|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
- translations[locale].merge! data, &merger
+ merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
+ translations[locale].merge!(data, &merger)
end
# Return a new hash with all keys and nested keys converted to symbols.
def deep_symbolize_keys(hash)
- hash.inject({}){|result, (key, value)|
+ hash.inject({}) { |result, (key, value)|
value = deep_symbolize_keys(value) if value.is_a? Hash
result[(key.to_sym rescue key) || key] = value
result
}
end
end
end
-end
+end

1 comment on commit 3696c92

@bai

much better! thanks!

Please sign in to comment.