Permalink
Browse files

make merge_translations merge a given value or hash over a previously…

… set nil value. should probably raise a TypeError instead because the given translation data seems flawed
  • Loading branch information...
1 parent ed86aaa commit f2b929a1d0805759902539265be316e5becdf4a7 Sven Fuchs committed Jan 10, 2010
Showing with 6 additions and 2 deletions.
  1. +6 −2 lib/i18n/backend/base.rb
View
@@ -227,7 +227,7 @@ def interpolate_lambda?(object, string, key)
def load_file(filename)
type = File.extname(filename).tr('.', '').downcase
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) }
end
@@ -251,7 +251,11 @@ def merge_translations(locale, 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 }
+ merger = proc do |key, v1, v2|
+ # TODO should probably be:
+ # raise TypeError.new("can't merge #{v1.inspect} and #{v2.inspect}") unless Hash === v1 && Hash === v2
+ Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : (v2 || v1)
+ end
translations[locale].merge!(data, &merger)
end
end

0 comments on commit f2b929a

Please sign in to comment.