Skip to content

Commit

Permalink
No need to unwind keys when storing translations. This allows to stor…
Browse files Browse the repository at this point in the history
…es keys in any format without a need to specify a separator.
  • Loading branch information
josevalim committed Apr 28, 2010
1 parent 0e6daef commit 4405bc2
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/i18n/backend/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def lookup(locale, key, scope = [], options = {})
hash[r.key.slice(chop_range)] = r.value
hash
end
deep_symbolize_keys(unwind_keys(result, separator))
deep_symbolize_keys(result)
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ def load_yml(filename)
def merge_translations(locale, data, options = {})
locale = locale.to_sym
translations[locale] ||= {}
separator = options[:separator] || I18n.default_separator
data = unwind_keys(data, separator)

data = deep_symbolize_keys(data)

# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
Expand Down
23 changes: 0 additions & 23 deletions lib/i18n/backend/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,6 @@ def escape_default_separator(key, separator=nil)
def unescape_default_separator(key, separator=nil)
key.to_s.tr(SEPARATOR_ESCAPE_CHAR, separator || I18n.default_separator).to_sym
end

# Expand keys chained by the the given separator through nested Hashes
# >> { "a.b.c" => "d", "a.b.e" => "f", "a.g" => "h", "i" => "j" }.unwind
# => { "a" => { "b" => { "c" => "d", "e" => "f" }, "g" => "h" }, "i" => "j"}
def unwind_keys(hash, separator = ".")
result = {}
hash.each do |key, value|
keys = key.to_s.split(separator)
curr = result
curr = curr[keys.shift] ||= {} while keys.size > 1
curr[keys.shift] = value
end
result
end

# # Flatten the given array once
# def flatten_once(array)
# result = []
# for element in array # a little faster than each
# result.push(*element)
# end
# result
# end
end
end
end
6 changes: 0 additions & 6 deletions test/backend/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ def setup
assert_equal expected, @backend.wind_keys(hash)
end

test "unwind_keys" do
hash = { "a.b.c" => "d", :"a.b.e" => "f", :"a.g" => "h", "i" => "j" }
expected = { "a" => { "b" => { "c" => "d", "e" => "f" }, "g" => "h" }, "i" => "j"}
assert_equal expected, @backend.unwind_keys(hash)
end

test "deep_symbolize_keys" do
result = @backend.deep_symbolize_keys('foo' => { 'bar' => { 'baz' => 'bar' } })
expected = {:foo => {:bar => {:baz => 'bar'}}}
Expand Down

0 comments on commit 4405bc2

Please sign in to comment.