Passing variables in yaml file

I like to use i18n and yml. I want my own yaml file to do this. This is access to the variable in the yaml file. Something like that

name: address: "%{city} %{street}" 

add a variable can pass something like some_method('name.address', :city => 'my city', :street => 'my street')

In i18n we can do

 en: message: welcome: "Hello %{username}" 

To cause this, we can use t("message.welcome", :username => 'admin')

How can I implement it?

+4
source share
1 answer

It is replaced after a call. For instance.

 Yaml.load_file('locale/en.yml')['en']['message']['welcome'].gsub('%{username}', username) 

So in a method, it could 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 

Reformat it again, but the idea is.

The original method is here: https://github.com/svenfuchs/i18n/blob/master/lib/i18n.rb#L143 Manage many thoughts, I do not do this :)

+4
source

Source: https://habr.com/ru/post/1347062/


All Articles