active_type
active_type copied to clipboard
human_attribute_name will does not work on ActiveType objects when using gettext_i18n_rails
ActiveType imitates regular Rails models almost perfectly. However, I've run into a situation where that is not the case:
-
active_type (2.5.0)
-
gettext_i18n_rails (1.12.0)
While MyModel.human_attribute_name(:my_attr)
works fine on regular models configured the same way, the translation is not actually looked up, but "My attr" is printed instead.
Presented the choice whether to report this issue to gettext or active_type, I went the latter, as active_type AFAIK aims to imitate ActiveModel and the behavior differs from a regular model.
Thanks a lot in advance for looking into this! Let me know if I can help detailing this better.
Both gems certainly hook into translations for their own reasons, I'm not suprised this leads to issues.
However, since none of us use Gettext, it's not likely we will look into this anytime soon; if it turns out we can reasonably fix this in ActiveType we would of course accept a PR for this.
Thank you for your reply! I'll see if I can figure out something useful in the coming weeks.
Hey, @kratob!
What's your translation approach when you generate forms with ActiveType objects?
Best regards!
@kdiogenes When using regular i18n
you can either set translations on the base model or on the form model as usual. In general, I prefer to just translate on the base model.
Expanding a bit on @kratob's comment, because it isn't well documented in Rails:
If you have class Cat < ActiveType::Record[Animal]
, the call to Cat.human_attribute_name(:name)
will look in the following places:
-
activerecord.attributes.cat.name
-
activerecord.attributes.animal.name
-
activerecord.attributes.name
(discouraged)
I was thinking about using ActiveType::Object
as the base class for the form object.