Hi all. I have a custom validation which produces an error message with
dynamic content, like so:
errors.add_to_base("Must have exactly #{self.size} questions for a
#{self.size}-question sequenced quiz")
Can anyone tell me the cleanest way to localise this? I can think of
some dirty sort of ways along the lines of
errors.add_to_base("#{I18n.t(:'quiz.errors.sequential.wrong_question_number_1')}
#{self.size}
#{I18n.t(:'quiz.errors.sequential.wrong_question_number_2')}
#{self.size}
#{I18n.t(:'quiz.errors.sequential.wrong_question_number_3')}")
But this seems nasty like i say, and also assumes that in all languages
there will be three text chunks with the size in between, which might
not be the case. Can i somehow incorporate variables into my locale.yml
files? or some other similar approach?
on 2010-03-26 12:12
on 2010-03-26 13:01
On Fri, Mar 26, 2010 at 12:12, Max Williams <lists@ruby-forum.com> wrote: > Hi all. I have a custom validation which produces an error message with > dynamic content, like so: > > errors.add_to_base("Must have exactly #{self.size} questions for a > #{self.size}-question sequenced quiz") > > Can anyone tell me the cleanest way to localise this? I can think of > some dirty sort of ways along the lines of You could do something like errors.add_to_base(I18n.t(:'activerecord.errors.quiz.wrong_question_number', :count => self.size)) with a key like ... wrong_question_number: one: "Must have #{count} question" other: "Must have #{count} questions"
on 2010-03-26 14:16
Henrik --- wrote: > On Fri, Mar 26, 2010 at 12:12, Max Williams <lists@ruby-forum.com> > wrote: >> Hi all. �I have a custom validation which produces an error message with >> dynamic content, like so: >> >> �errors.add_to_base("Must have exactly #{self.size} questions for a >> #{self.size}-question sequenced quiz") >> >> Can anyone tell me the cleanest way to localise this? �I can think of >> some dirty sort of ways along the lines of > > You could do something like > > errors.add_to_base(I18n.t(:'activerecord.errors.quiz.wrong_question_number', > :count => self.size)) > > > with a key like > > ... > wrong_question_number: > one: "Must have #{count} question" > other: "Must have #{count} questions" Thanks Henrik. So you can pass variables into the <locale>.yml file then? (tries it...) Hmm, i just get the string without the interpolation: eg, trying it with a simple example, just called from the view template rather than a model validation: #in template = t(:'quiz.attributes.description', :size => "#{@quiz.size}") #in .config/locales/en.yml quiz: attributes: description: "Description for your #{size} question quiz" #rendered out Description for your #{size} question quiz Do i need to change my setup to enable this variable-passing? I'm just using all of the basic I18n in rails 2.3.4. thanks, max
on 2010-03-26 14:25
You have to change this: t(:'quiz.attributes.description', :size => @quiz.size) In the view you pass the varible without direct to hash. 2010/3/26 Max Williams <lists@ruby-forum.com> > >> some dirty sort of ways along the lines of > > ... > > > "rails-i18n" group. > To post to this group, send email to rails-i18n@googlegroups.com. > To unsubscribe from this group, send email to > rails-i18n+unsubscribe@googlegroups.com<rails-i18n%2Bunsubscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/rails-i18n?hl=en. > > -- Experiencia es lo que obtienes, cuando no obtienes lo que quieres.
on 2010-03-26 14:39
Thanks Andres, but that gives the same result.
= t(:'quiz.attributes.description', :size => @quiz.size)
gives
Description for your #{size} question quiz
Surely it wouldn't make any difference anyway? The string interpolation
would happen before the t method is called, right? So it would just be
like passing a string through. And if it can take integers dynamically
then surely it can take strings dynamically. Seems my problem is that
it's not taking anything dynamically.
on 2010-03-26 14:46
Solved it: This part of the I18n documentation showed me how: http://guides.rubyonrails.org/i18n.html#interpolation The problem was the syntax for string interpolation in the <locale>.yml file. You *don't* do this: description: "Description for your #{size} question quiz" But instead, do this: description: "Description for your {{size}} question quiz" Thanks all. Max
on 2010-03-26 15:14
On Fri, Mar 26, 2010 at 14:46, Max Williams <lists@ruby-forum.com>
wrote:
> description: "Description for your {{size}} question quiz"
Oops, yeah, I should have added an "off the top of my head" :) Stupid
mistake on my part. Glad you got it working!
on 2010-03-26 15:15
Henrik --- wrote: > On Fri, Mar 26, 2010 at 14:46, Max Williams <lists@ruby-forum.com> > wrote: >> � � �description: "Description for your {{size}} question quiz" > Oops, yeah, I should have added an "off the top of my head" :) Stupid > mistake on my part. Glad you got it working! No worries, cheers :)
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.