提交 a61c4dfa 编写于 作者: D David Heinemeier Hansson

Follow style conventions

上级 1df157cd
...@@ -10,7 +10,7 @@ class Simple ...@@ -10,7 +10,7 @@ class Simple
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml # plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
# for details. # for details.
def load_translations(*filenames) def load_translations(*filenames)
filenames.each {|filename| load_file filename } filenames.each { |filename| load_file(filename) }
end end
# Stores translations for the given locale in memory. # Stores translations for the given locale in memory.
...@@ -23,12 +23,12 @@ def store_translations(locale, data) ...@@ -23,12 +23,12 @@ def store_translations(locale, data)
def translate(locale, key, options = {}) def translate(locale, key, options = {})
raise InvalidLocale.new(locale) if locale.nil? 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 reserved = :scope, :default
count, scope, default = options.values_at(:count, *reserved) count, scope, default = options.values_at(:count, *reserved)
options.delete(:default) options.delete(:default)
values = options.reject{|name, value| reserved.include? name } values = options.reject { |name, value| reserved.include?(name) }
entry = lookup(locale, key, scope) entry = lookup(locale, key, scope)
if entry.nil? if entry.nil?
...@@ -37,8 +37,8 @@ def translate(locale, key, options = {}) ...@@ -37,8 +37,8 @@ def translate(locale, key, options = {})
raise(I18n::MissingTranslationData.new(locale, key, options)) raise(I18n::MissingTranslationData.new(locale, key, options))
end end
end end
entry = pluralize locale, entry, count entry = pluralize(locale, entry, count)
entry = interpolate locale, entry, values entry = interpolate(locale, entry, values)
entry entry
end end
...@@ -170,21 +170,21 @@ def interpolate(locale, string, values = {}) ...@@ -170,21 +170,21 @@ def interpolate(locale, string, values = {})
# for all other file extensions. # for all other file extensions.
def load_file(filename) def load_file(filename)
type = File.extname(filename).tr('.', '').downcase 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 = 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 end
# Loads a plain Ruby translations file. eval'ing the file must yield # Loads a plain Ruby translations file. eval'ing the file must yield
# a Hash containing translation data with locales as toplevel keys. # a Hash containing translation data with locales as toplevel keys.
def load_rb(filename) def load_rb(filename)
eval IO.read(filename), binding, filename eval(IO.read(filename), binding, filename)
end end
# Loads a YAML translations file. The data must have locales as # Loads a YAML translations file. The data must have locales as
# toplevel keys. # toplevel keys.
def load_yml(filename) def load_yml(filename)
YAML::load IO.read(filename) YAML::load(IO.read(filename))
end end
# Deep merges the given translations hash with the existing translations # Deep merges the given translations hash with the existing translations
...@@ -192,16 +192,16 @@ def load_yml(filename) ...@@ -192,16 +192,16 @@ def load_yml(filename)
def merge_translations(locale, data) def merge_translations(locale, data)
locale = locale.to_sym locale = locale.to_sym
translations[locale] ||= {} 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 # 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 } merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
translations[locale].merge! data, &merger translations[locale].merge!(data, &merger)
end end
# Return a new hash with all keys and nested keys converted to symbols. # Return a new hash with all keys and nested keys converted to symbols.
def deep_symbolize_keys(hash) 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 value = deep_symbolize_keys(value) if value.is_a? Hash
result[(key.to_sym rescue key) || key] = value result[(key.to_sym rescue key) || key] = value
result result
...@@ -209,4 +209,4 @@ def deep_symbolize_keys(hash) ...@@ -209,4 +209,4 @@ def deep_symbolize_keys(hash)
end end
end end
end end
end end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册