質問

How can I pass variables to a YAML array? The way I imagined it to work was like passing it to normal strings, like this (general example):

en:
  names:
    - First %{name}
    - Second %{name}
    - Third %{name}

 

t('names', name: 'foo')[0]

But this will render the text First %{name}. I know I could use explicitly indexed keys instead, like name_0:, name_1:, etc., but I see the YAML array as a much cleaner way and would like to make use of it.

役に立ちましたか?

解決

If you look at the I18n gem interpolate function, you will see that you can't do this by passing values, because if output is not string interpolate will no work.

But nothing prevents you to do the following:

t('names').map{|s| I18n.interpolate(s, name: 'foo')}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top