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!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Valery Kvon at Feb 23, 2012 at 8:42 pm

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

    phones:
    - (123) 456-7890
    - +1987654321
    - 1-890-123-456
    Try to quote them

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Alex Duck at Feb 23, 2012 at 9:12 pm

    On Feb 23, 9:42 pm, Valery Kvon 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?

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Valery Kvon at Feb 23, 2012 at 9:15 pm

    On 24.02.2012, at 1:12, Alex Duck wrote:
    On Feb 23, 9:42 pm, Valery Kvon 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?

    something like:

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Alex Duck at Feb 24, 2012 at 7:10 am

    On Feb 23, 10:14 pm, Valery Kvon wrote:
    On 24.02.2012, at 1:12, Alex Duck wrote:
    On Feb 23, 9:42 pm, Valery Kvon 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?
    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"

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Valery Kvon at Feb 24, 2012 at 12:11 pm

    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)

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 23, '12 at 8:26p
activeFeb 24, '12 at 12:11p
posts6
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Valery Kvon: 3 posts Alex Duck: 3 posts

People

Translate

site design / logo © 2022 Grokbase