Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allows to store translations without escaping separator.

  • Loading branch information...
commit 7f3d4d7a3188c32c74e0eb55c180c1beac3c8068 1 parent 67cb16c
@josevalim josevalim authored
View
3  lib/i18n/backend/active_record.rb
@@ -20,7 +20,8 @@ def available_locales
end
def store_translations(locale, data, options = {})
- flatten_translations(locale, data).each do |key, value|
+ escape = options.fetch(:escape, true)
+ flatten_translations(locale, data, escape, false).each do |key, value|
Translation.locale(locale).lookup(expand_keys(key)).delete_all
Translation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
end
View
10 lib/i18n/backend/flatten.rb
@@ -51,12 +51,12 @@ def links
# >> { "a" => { "b" => { "c" => "d", "e" => "f" }, "g" => "h" }, "i" => "j"}.wind
# => { "a.b.c" => "d", "a.b.e" => "f", "a.g" => "h", "i" => "j" }
#
- def flatten_keys(hash, prev_key = nil, &block)
+ def flatten_keys(hash, escape, prev_key=nil, &block)
hash.each_pair do |key, value|
- key = escape_default_separator(key)
+ key = escape_default_separator(key) if escape
curr_key = [prev_key, key].compact.join(FLATTEN_SEPARATOR).to_sym
yield curr_key, value
- flatten_keys(value, curr_key, &block) if value.is_a?(Hash)
+ flatten_keys(value, escape, curr_key, &block) if value.is_a?(Hash)
end
end
@@ -66,9 +66,9 @@ def flatten_keys(hash, prev_key = nil, &block)
#
# Nested hashes are included in the flattened hash just if subtree
# is true and Symbols are automatically stored as links.
- def flatten_translations(locale, data, subtree=false)
+ def flatten_translations(locale, data, escape, subtree)
hash = {}
- flatten_keys(data) do |key, value|
+ flatten_keys(data, escape) do |key, value|
if value.is_a?(Hash)
hash[key] = value if subtree
else
View
3  lib/i18n/backend/key_value.rb
@@ -60,7 +60,8 @@ def initialize(store, subtrees=true)
end
def store_translations(locale, data, options = {})
- flatten_translations(locale, data, @subtrees).each do |key, value|
+ escape = options.fetch(:escape, true)
+ flatten_translations(locale, data, escape, @subtrees).each do |key, value|
key = "#{locale}.#{key}"
case value
View
6 test/backend/key_value_test.rb
@@ -12,8 +12,8 @@ def setup_backend!(subtree=true)
store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' })
end
- def assert_flattens(expected, nested)
- assert_equal expected, I18n.backend.flatten_translations("en", nested, true)
+ def assert_flattens(expected, nested, escape=true, subtree=true)
+ assert_equal expected, I18n.backend.flatten_translations("en", nested, escape, subtree)
end
test "hash flattening works" do
@@ -24,6 +24,8 @@ def assert_flattens(expected, nested)
)
assert_flattens({:a=>{:b =>['a', 'b']}, :"a.b"=>['a', 'b']}, {:a=>{:b =>['a', 'b']}})
assert_flattens({:"a\001b" => "c"}, {:"a.b" => "c"})
+ assert_flattens({:"a.b"=>['a', 'b']}, {:a=>{:b =>['a', 'b']}}, true, false)
+ assert_flattens({:"a.b" => "c"}, {:"a.b" => "c"}, false)
end
test "store_translations handle subtrees by default" do
Please sign in to comment.
Something went wrong with that request. Please try again.