Accessing array elements directly in Rails translations

Is it possible to access array elements directly with Rails’s t()
helper?

For example, if I have this translation defined in my YAML file

en:
contact_page:
title: Contact us
phones:
- (123) 456-7890
- +1987654321
- 1-890-123-456

Then in my view, how can I get to international phone number (second
array item) directly?

I tried all of following, but non of them works.

<%= t ‘contact_page.phones[1]’ %>
<%= t ‘contact_page.phones.1’ %>
<%= t ‘contact_page.phones.[1]’ %>

Some google results suggest first form should work, but it doesn’t
work for me. However, code below does work as it should, but its a bit
cumbersome to use, so I was hoping there is a way to get to array
elements directly by specifying their index in the string identifier
somehow.

<%= t(‘contact_page.phones’)[1] %>

Thanks for your help!

On 24.02.2012, at 0:26, Alex Duck wrote:

phones:
- (123) 456-7890
- +1987654321
- 1-890-123-456

Try to quote them

On Feb 23, 9:42pm, Valery K. [email protected] wrote:

On 24.02.2012, at 0:26, Alex Duck wrote:

phones:

  • (123) 456-7890
  • +1987654321
  • 1-890-123-456

Try to quote them

Quote them how? Can you give an example please?

So given this YAML, how can I get to any specific element in the array
by calling Rail’s t() helper only?

en:
contact_page:
title: Contact us
phones:
- “(123) 456-7890”
- “+1987654321”
- “1-890-123-456”

Rails way is:

I18n.t(‘contact_page.phones’)[N] # where N is an index of Array

I18n already get your locale set from ‘config/application.rb’ file:

config.i18n.default_locale accessor =

(:en by default)

On 24.02.2012, at 1:12, Alex Duck wrote:

Try to quote them

Quote them how? Can you give an example please?

something like:

phones:
- “(123) 456-7890”
- “+1987654321”
- “1-890-123-456”

On Feb 23, 10:14pm, Valery K. [email protected] wrote:

Try to quote them

Quote them how? Can you give an example please?

something like:

phones:

  • “(123) 456-7890”
  • “+1987654321”
  • “1-890-123-456”

Sorry, I made a mistake in my example. You are correct, of course. In
my above example, quoting is needed, of course.

So given this YAML, how can I get to any specific element in the array
by calling Rail’s t() helper only?

en:
contact_page:
title: Contact us
phones:
- “(123) 456-7890”
- “+1987654321”
- “1-890-123-456”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs