# Aggregate similar sentences

**URL:** <https://www.ruby-forum.com/t/aggregate-similar-sentences/182268>\
**Category:** Rails I18n\
**Created:** [January 25, 2010, 7:39pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268 "2010-01-25T19:39:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 25, 2010, 7:39pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/1 "2010-01-25T19:39:55Z")

</div>

Hello,

I want to aggregate some similar sentences, which will work with i18n in  
the en.yml file

Now I have redundancies like:

challenge\_created\_success: Challenge has been successfully created.  
change\_created: Change has been successfully created.

and I need something like that:

\<\> has been successfully created.

I’m new in ruby so sorry if it is an easy question 🙂  
The Item should also be from the en.yml file. So how can I access a  
variable in en.yml?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 25, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/2 "2010-01-25T19:47:42Z")

</div>

On Mon, Jan 25, 2010 at 19:39, Kot M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello,
> 
> I want to aggregate some similar sentences, which will work with i18n in  
> the en.yml file
> 
> Now I have redundancies like:
> 
> challenge\_created\_success: Challenge has been successfully created.  
> change\_created: Change has been successfully created.

en.yml:

```
successfully_created: "{{kind}} has been successfully created."

```

view:

```
t(:successfully_created, :kind => Challenge.human_name)

```

This assumes Challenge is an ActiveRecord model translated per the  
conventions (activerecord.models.challenge).

More generally, you could do something like

```
t(:successfully_created, :kind => t("some.other.key"))

```

---

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 25, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/3 "2010-01-25T20:02:15Z")

</div>

Thank you very much!

can I also use words from a .yml file I defined? not from activerecord?

Kot M. wrote:

> Hello,
> 
> I want to aggregate some similar sentences, which will work with i18n in  
> the en.yml file
> 
> Now I have redundancies like:
> 
> challenge\_created\_success: Challenge has been successfully created.  
> change\_created: Change has been successfully created.
> 
> and I need something like that:
> 
> \<\> has been successfully created.
> 
> I’m new in ruby so sorry if it is an easy question 🙂  
> The Item should also be from the en.yml file. So how can I access a  
> variable in en.yml?
> 
> Thank you in advance!

---

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 25, 2010, 8:14pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/4 "2010-01-25T20:14:15Z")

</div>

Ah, ok. I got it. It was the general thing you mentioned.

Thank you!

---

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 25, 2010, 8:05pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/5 "2010-01-25T20:05:51Z")

</div>

On Mon, Jan 25, 2010 at 19:46, Henrik N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

---

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 25, 2010, 8:34pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/6 "2010-01-25T20:34:55Z")

</div>

How do I use that in the controller after creating a Challenge?

flash[:success] = t(:flash.added, :item =\> Challenge.human\_name) ?

---

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 25, 2010, 8:39pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/7 "2010-01-25T20:39:49Z")

</div>

On Mon, Jan 25, 2010 at 20:34, Kot M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How do I use that in the controller after creating a Challenge?
> 
> flash[:success] = t(:flash.added, :item =\> Challenge.human\_name) ?

The key should be a single symbol or a string. So  
t(:‘flash.added’, …)  
or  
t(‘flash.added’, …)

Have a look at [Rails Internationalization (I18n) API — Ruby on Rails Guides](http://guides.rubyonrails.org/i18n.html) for more  
details on the basics.

---

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 25, 2010, 8:51pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/8 "2010-01-25T20:51:39Z")

</div>

thanks, but I think the model translation doesn’t work.

Is it in the i18n or do I need an another plugin for that?

---

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 31, 2010, 12:54pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/9 "2010-01-31T12:54:26Z")

</div>

Hi Kot,

sorry for the late response here, but …

On Jan 25, 2010, at 7:39 PM, Kot M. wrote:

> \<\> has been successfully created.  
> Why do you think you need that? To remove the redundancies and keep  
> translations dry?

In general redundancies aren’t a bad thing in translations as they are  
in your code. Keep in mind that in your translations you as a developer  
want to give as much control as possible to translators. You as a  
developer might not know many of the languages that your code eventually  
needs to be localized to, so should keep the door open for translators  
to do their thing.

In your example the words “challange” and “change” might need to be  
inflected differently in a particular language so that you’d need to add  
more logic here (in order to evaluate correctly). In English this  
is trivial but in many other languages it’s not.

So if you’re just using the I18n API to abstract strings out of your  
code while your app eventually never will be localized to any other  
language than English then your fine. If not then you’re probably better  
off not to dry up messages like those.

🙂

---

<div class="post-metadata">

**Author:** ![furoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/8e8cbc/32.png) [@furoh](https://www.ruby-forum.com/u/furoh)\
**Post date:** [January 25, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/aggregate-similar-sentences/182268/10 "2010-01-25T21:12:48Z")

</div>

sorry, but the last question is why this doesn’t work

challenge controller:

flash[:success] = t(‘flash.added’, :item =\> t(‘dictionary.challenge’))

en.yml

dictionary:  
challenge: Challenge

flash:  
added: {{item}} has been successfully created.
