Forum: Rails I18n Aggregate similar sentences

Posted by Kot Matroskin (furoh)
on 2010-01-25 19:39
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:

<<Item>> 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!
Posted by Henrik --- (malesca)
on 2010-01-25 19:47
(Received via mailing list)
On Mon, Jan 25, 2010 at 19:39, Kot Matroskin <lists@ruby-forum.com> 
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"))
Posted by Kot Matroskin (furoh)
on 2010-01-25 20:02
Thank you very much!

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








Kot Matroskin 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:
> 
> <<Item>> 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!
Posted by Henrik --- (malesca)
on 2010-01-25 20:05
(Received via mailing list)
On Mon, Jan 25, 2010 at 19:46, Henrik Nyh <henrik@nyh.se> wrote:
Posted by Kot Matroskin (furoh)
on 2010-01-25 20:14
Ah, ok. I got it. It was the general thing you mentioned.

Thank you!


Posted by Kot Matroskin (furoh)
on 2010-01-25 20:34
How do I use that in the controller after creating a Challenge?

flash[:success] = t(:flash.added, :item => Challenge.human_name) ?
Posted by Henrik --- (malesca)
on 2010-01-25 20:39
(Received via mailing list)
On Mon, Jan 25, 2010 at 20:34, Kot Matroskin <lists@ruby-forum.com> 
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 http://guides.rubyonrails.org/i18n.html for more
details on the basics.
Posted by Kot Matroskin (furoh)
on 2010-01-25 20:51
thanks, but I think the model translation doesn't work.

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

Posted by Kot Matroskin (furoh)
on 2010-01-25 21:12
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.
Posted by Sven Fuchs (Guest)
on 2010-01-31 12:54
(Received via mailing list)
Hi Kot,

sorry for the late response here, but ...

On Jan 25, 2010, at 7:39 PM, Kot Matroskin wrote:
> <<Item>> 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 <Item> 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.

:)
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
No account? Register here.