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