Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables to a YAML array

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.

like image 779
zykadelic Avatar asked May 11 '26 08:05

zykadelic


1 Answers

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')}
like image 52
ole Avatar answered May 14 '26 13:05

ole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!