Translating dynamic content

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 Fri, Mar 26, 2010 at 12:12, Max W. [email protected]
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”

Henrik — wrote:

On Fri, Mar 26, 2010 at 12:12, Max W. [email protected]
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 .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

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 W. [email protected]

some dirty sort of ways along the lines of

“rails-i18n” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected][email protected]
.
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.

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 Fri, Mar 26, 2010 at 14:46, Max W. [email protected]
wrote:

 description: "Description for your {{size}} question quiz"

Oops, yeah, I should have added an “off the top of my head” :slight_smile: Stupid
mistake on my part. Glad you got it working!

Solved it: This part of the I18n documentation showed me how:

The problem was the syntax for string interpolation in the .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

Henrik — wrote:

On Fri, Mar 26, 2010 at 14:46, Max W. [email protected]
wrote:

� � �description: “Description for your {{size}} question quiz”
Oops, yeah, I should have added an “off the top of my head” :slight_smile: Stupid
mistake on my part. Glad you got it working!

No worries, cheers :slight_smile: