# Translating dynamic content

**URL:** <https://www.ruby-forum.com/t/translating-dynamic-content/185803>\
**Category:** Rails I18n\
**Created:** [March 26, 2010, 12:12pm UTC](https://www.ruby-forum.com/t/translating-dynamic-content/185803 "2010-03-26T12:12:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 26, 2010, 12:12pm UTC](https://www.ruby-forum.com/t/translating-dynamic-content/185803/1 "2010-03-26T12:12:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 26, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/translating-dynamic-content/185803/2 "2010-03-26T13:01:18Z")

</div>

On Fri, Mar 26, 2010 at 12:12, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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”

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 26, 2010, 2:16pm UTC](https://www.ruby-forum.com/t/translating-dynamic-content/185803/3 "2010-03-26T14:16:38Z")

</div>

Henrik — wrote:

> On Fri, Mar 26, 2010 at 12:12, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 26, 2010, 2:25pm UTC](https://www.ruby-forum.com/t/translating-dynamic-content/185803/4 "2010-03-26T14:25:35Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > some dirty sort of ways along the lines of  
> > > …
> 
> “rails-i18n” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rails-i18n?hl=en](http://groups.google.com/group/rails-i18n?hl=en).

–  
Experiencia es lo que obtienes, cuando no obtienes lo que quieres.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 26, 2010, 2:39pm UTC](https://www.ruby-forum.com/t/translating-dynamic-content/185803/5 "2010-03-26T14:39:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 26, 2010, 3:14pm UTC](https://www.ruby-forum.com/t/translating-dynamic-content/185803/6 "2010-03-26T15:14:10Z")

</div>

On Fri, Mar 26, 2010 at 14:46, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 26, 2010, 2:46pm UTC](https://www.ruby-forum.com/t/translating-dynamic-content/185803/7 "2010-03-26T14:46:03Z")

</div>

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

> **[Rails Internationalization (I18n) API — Ruby on Rails Guides](https://guides.rubyonrails.org/i18n.html#interpolation)**
>
> Rails Internationalization (I18n) APIThe Ruby I18n (shorthand for internationalization) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single...

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

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 26, 2010, 3:15pm UTC](https://www.ruby-forum.com/t/translating-dynamic-content/185803/8 "2010-03-26T15:15:34Z")

</div>

Henrik — wrote:

> On Fri, Mar 26, 2010 at 14:46, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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 🙂
