Access YAML list in Rails translation

137 views Asked by At

I have the following translation file:

ru:
  common:
    age:
      - год
      - года
      - лет

and I want to access list items in tranlsation. I tried something like:

t('common.age[2]')

But it doesn't work. How to do it properly?

1

There are 1 answers

0
Paul Fioravanti On BEST ANSWER

You can store an array structure in YAML but you can't iterate over one in YAML-land. YAML is a data serialisation language, and is not meant to contain executable statements (Array#[] is a Ruby method call), only data structures. Executable statements are the responsibility of the programming language you're working with, in your case Ruby.

So, in your case, you need to use t('common.age') to first pull out the array from YAML, then iterate over it in Ruby-land:

array = t('common.age')
# => ["год", "года", "лет"]
array[2]
# => "лет"