Skip to content

Commit

Permalink
Follow style conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Oct 28, 2008
1 parent 1df157c commit a61c4df
Showing 1 changed file with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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?
Expand All @@ -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

Expand Down Expand Up @@ -170,43 +170,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

0 comments on commit a61c4df

Please sign in to comment.