I want to manipulate the following locales .yml file in Ruby On Rails:
fr:
back: Retour
signup: Inscription
helpers:
submit:
create: "Créer votre %{model}"
hello:
world: "Bonjour le monde"
So I parse it in my controller like :
@yml_hash = YAML.load_file("config/locales/en.yml")
Then I process it with the following helper in my html.erb file :
module TranslationHelper
def render_hash(hash)
return ihash(hash)
end
def ihash(h, key = [], result = [])
h.each_pair do |k,v|
if v.is_a?(Hash)
key << k
ihash(v, key, result)
else
result << {full_key: key, key: k, value: v}
end
end
result
end
end
And it's return the output:
[{:full_key=>["fr", "helpers", "submit", "hello"], :key=>"back",
:value=>"Retour"},
{:full_key=>["fr", "helpers", "submit", "hello"], :key=>"signup",
:value=>"Inscription"},
{:full_key=>["fr", "helpers", "submit", "hello"], :key=>"create",
:value=>"Créer votre %{model}"},
{:full_key=>["fr", "helpers", "submit", "hello"], :key=>"world",
:value=>"Bonjour le monde"}]
But actually my goal is to have the full key of a value in the hash like this:
[{:full_key=>["fr"], :key=>"back", :value=>"Retour"},
{:full_key=>["fr"], :key=>"signup", :value=>"Inscription"},
{:full_key=>["fr", "helpers", "submit"], :key=>"create", :value=>"Créer votre %{model}"},
{:full_key=>["fr", "hello"], :key=>"world", :value=>"Bonjour le monde"}]
Is there any way to handle hash in my function in order to have those kind of results?
Thanks.