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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Valery Kvon
2012-Feb-23 20:42 UTC
Re: Accessing array elements directly in Rails translations
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alex Duck
2012-Feb-23 21:12 UTC
Re: Accessing array elements directly in Rails translations
On Feb 23, 9:42 pm, Valery Kvon <adda...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 24.02.2012, at 0:26, Alex Duck wrote: > > > > > phones: > > - (123) 456-7890 > > - +1987654321 > > - 1-890-123-456 > > Try to quote themQuote 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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
2012-Feb-23 21:14 UTC
Re: Accessing array elements directly in Rails translations
On 24.02.2012, at 1:12, Alex Duck wrote:> On Feb 23, 9:42 pm, Valery Kvon <adda...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alex Duck
2012-Feb-24 07:09 UTC
Re: Accessing array elements directly in Rails translations
On Feb 23, 10:14 pm, Valery Kvon <adda...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 24.02.2012, at 1:12, Alex Duck wrote: > > > On Feb 23, 9:42 pm, Valery Kvon <adda...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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
2012-Feb-24 12:10 UTC
Re: Accessing array elements directly in Rails translations
> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.