common attribute translation in multiple models

1,380 views
Skip to first unread message

Csiszár Attila

unread,
Nov 23, 2008, 7:28:54 AM11/23/08
to rails-i18n
Hi!
===
Preface: While I playing around I18n's model translation I found that
there is no way to define common attribute name translations if two or
more model has same one. I have to define attribute translation per
models.
===

Example:
Lets assume that I have two model: category and product and both have
name attribute, and both attribute translation is same for my
application.

Now I have to do so:
===============
activerecord:
attributes:
category:
name: "Blah"
product:
name: "Blah"

I wonder something like that could be possible:
==========
activerecord:
attributes:
name: "Blah" <==common for all models!
user:
name: "I dont like blah" <== I can override for a model

Then:
Product.human_attribute_name("name") => "Blah"
Category.human_attribute_name("name") => "Blah"
...but...
User.human_attribute_name("name") => "I dont like blah"

====
ps.: Anyway, I18n is a great effort, very fun and elegant way to use
translations in Rails!:)

Carlos Júnior

unread,
Nov 23, 2008, 9:44:27 AM11/23/08
to rails...@googlegroups.com

You can do:

attribute_commons: &commons

foo: "bar"

activerecord:

attributes:

category:

<<: *commons

other_attribute: "outro atributo"

product:

<<: *commons

price: "preço"

Yaroslav Markin

unread,
Nov 23, 2008, 11:55:00 AM11/23/08
to rails...@googlegroups.com
I believe we have a ticket on this but it is somewhat stalled


On Sun, Nov 23, 2008 at 3:28 PM, Csiszár Attila <csisz...@gmail.com> wrote:

Preface: While I playing around I18n's model translation I found that
there is no way to define common attribute name translations if two or
more model has same one. I have to define attribute translation per
models.

 
--
Yaroslav Markin

Csiszár Attila

unread,
Nov 23, 2008, 12:09:19 PM11/23/08
to rails-i18n
Yeah I see.
I would be great if this feature is included in I18n API by default
because I think lots of people need this one.

On nov. 23, 17:55, "Yaroslav Markin" <yaroslav.mar...@gmail.com>
wrote:
> I believe we have a ticket on this but it is somewhat stalledhttp://i18n.lighthouseapp.com/projects/14948/tickets/15-improvement-a...

Diego Carrion

unread,
Nov 25, 2008, 4:19:11 PM11/25/08
to rails-i18n
Hi, i just created a plugin with this behaviour. The plugin is here:

http://github.com/dcrec1/activerecord_i18n_defaults/tree/master

hope it helps,

Diego
http://www.diegocarrion.com
http://www.mouseoverstudio.com/blog/

Sven Fuchs

unread,
Nov 25, 2008, 4:40:31 PM11/25/08
to rails...@googlegroups.com
cool :)

maybe have a somewhat shorter namespace name than :global-attributes ?

what about something like :_all ?

Carlos Júnior

unread,
Nov 25, 2008, 5:03:50 PM11/25/08
to rails...@googlegroups.com

Sven, I think that would be really nice and useful if the translations could be inheritable.

So, take this example:

pt-BR:

name: Nome

activerecord:

attributes:

user:

name: Nome

if you translated something once in a toplevel, you shouldn't need to translate it again, but you can replace it:

pt-BR:

name: Nome

activerecord:

attributes:

user:

name: Nome completo

But, if you don't want, this would be enough:

pt-BR:

name: Nome

This is quite simple:

I18n.t "activerecord.attributes.user.name" would try:

[:activerecord][:attributes][:user][:name], then, if miss:

[:activerecord][:attributes][:name], then, if miss:

[:activerecord][:name], then, if miss

[:name], and it would finally find the translation, or, if it miss:

then I18n.t would return the translation missing string

This approach would be very welcome, if you think that "name" would be used in many places of the applications, such as a lot of translations.

What do you think?

I can implement this as a plugin or as a patch if you want.

Sven Fuchs

unread,
Nov 25, 2008, 5:34:46 PM11/25/08
to rails...@googlegroups.com
Isn't that similar to what Diego's plugin also does?

The problem with this:

> [:activerecord][:attributes][:user][:name], then, if miss:
> [:activerecord][:attributes][:name], then, if miss:

is that there could be a model Name. That's what I understand Diego
solves with the :global-attributes namespace.

Or am I missing something?

Sorry ... I'm only following this peripherally cause I'm up to my ears
in other stuff :-/

Carlos Júnior

unread,
Nov 25, 2008, 5:38:45 PM11/25/08
to rails...@googlegroups.com

Well... I'm thinking about everything, not just activerecord things.... and I'm thinking about inheritance, not about a namespace for global translations...

Diego Carrion

unread,
Nov 25, 2008, 6:28:03 PM11/25/08
to rails-i18n
Hi,

i think _all can be a better namespace than global-attributes, good
idea (:

Does everybody thinks the same?

Diego Carrion

unread,
Nov 25, 2008, 7:42:46 PM11/25/08
to rails-i18n
Hi Carlos,

I think it can be a good idea in some cases, why dont you write a
plugin? Some other people may like it :)

I just recomend you to check if the parent key contains a text or some
other childs.

Jean-Philippe Moal

unread,
Nov 28, 2008, 10:30:29 AM11/28/08
to rails...@googlegroups.com
Diego Carrion a écrit :
> Hi,
>
> i think _all can be a better namespace than global-attributes, good
> idea (:
>
> Does everybody thinks the same?

Hello,
what about using 'default' ?

By the way I have an issue with your plugin:
>> Page.human_attribute_name 'created_at'
won't work for example.

It seems I have problems with default already defined to the column_name
humanized, and with the scope [:activerecord, :attributes]

I pasted a gist of a version which seems to be working :
http://gist.github.com/30002
It certainly is really ugly, I'm checking the scope and if the default option is
equal to the humanized column name, but it seems to be working.

Jean-Philippe

unread,
Nov 28, 2008, 10:56:07 AM11/28/08
to rails-i18n
On 26 nov, 00:28, Diego Carrion <dc.r...@gmail.com> wrote:
> Hi,
>
> i think _all can be a better namespace than global-attributes, good
> idea (:
>
> Does everybody thinks the same?


Hello,
what about using 'default' ?

By the way I have an issue with your plugin:
>> >> Page.human_attribute_name 'created_at'
won't work for example.

It seems I have problems with default already defined to the
column_name
humanized, and with the scope [:activerecord, :attributes]

I pasted a gist of a version which seems to be working :
http://gist.github.com/30002
It certainly is really ugly, I'm checking the scope and if the default
option is
equal to the humanized column name, but it seems to be working.
The second rev of this gist is cleaner but the default was lost.

PS: Sorry if this message is sent twice, my first mail may be lost in
spam

stonefield

unread,
Nov 29, 2008, 7:38:53 PM11/29/08
to rails-i18n
First thing first.
a) It is mandatory that attribute names/translations can be inherited.
Secondly...
b) It us a good idea that it is possible to define common names that
are used across models. Whether default, common or _all is used, is
less important than the feature it selves. Personally I would prefer
common-attributes: ...

Thanks for bringing up this topic guys.

Diego Carrion

unread,
Dec 1, 2008, 8:38:21 AM12/1/08
to rails-i18n
Hi, now the plugin (thanks to Charles Bedard for the tip) do his work
at the human_attribute_name method from ActiveRecord::Base. This way
the I18n.translate method stays intact and don't have to veryfy if the
key contains activerecord.attributes on every call.

Anybody sees a problem with this?

At other hand, I have been receiving emails about the common attribute
and as suggested I changed it to _all .

Is that ok for everyone?

stonefield

unread,
Dec 2, 2008, 2:34:59 PM12/2/08
to rails-i18n
Diego,

If you mean:
"no-BM":
attributes:
_all:
name: Navn
comment: Kommentar
Is the structure? That is fine!

- Will it also support this?
class Vehicle < ActiveRecord::Base
end
class Car < Vehicle
end

"no-BM":
attributes:
vehicle:
name: Navn

Car.human_attribute_name(:name) => Navn

Diego Carrion

unread,
Dec 3, 2008, 8:47:06 AM12/3/08
to rails-i18n
Hi,

actually this is not supported, the way would be to put the common
attribute in _all .

Will try to implement this :)

Sven Fuchs

unread,
Dec 7, 2008, 3:52:56 PM12/7/08
to rails...@googlegroups.com
FYI, I've added the plugin to the wiki :)

http://rails-i18n.org/wiki


On 25.11.2008, at 22:19, Diego Carrion wrote:

>
Reply all
Reply to author
Forward
0 new messages