I love using i18n and yml. I want my own yaml file that do similar thing. That is accessing variable in yaml file. Something like this
name:
address: "%{city} %{street}"
add variable could pass something like some_method('name.address', :city => 'my city', :street => 'my street')
In i18n we can do
en:
message:
welcome: "Hello %{username}"
To call this we can use t("message.welcome", :username => 'admin')
How can I implement it?
It's replace after the call. By example.
Yaml.load_file('locale/en.yml')['en']['message']['welcome'].gsub('%{username}', username)
So in method it can be :
def t(key, changes)
result = yaml_locale['en']
key.split('.').each |k|
result = result[k]
end
changes.each_keys do |k|
result.gsub!("%{#{k}}%", changes[k])
end
result
end
Refactor it a little after but the idea is like that.
The original method is here : https://github.com/svenfuchs/i18n/blob/master/lib/i18n.rb#L143 Manage a lot of think I don't :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With