active_type icon indicating copy to clipboard operation
active_type copied to clipboard

human_attribute_name will does not work on ActiveType objects when using gettext_i18n_rails

Open kalsan opened this issue 11 months ago • 6 comments

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.

kalsan avatar Mar 15 '24 16:03 kalsan

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.

kratob avatar Mar 18 '24 08:03 kratob

Thank you for your reply! I'll see if I can figure out something useful in the coming weeks.

kalsan avatar Mar 18 '24 08:03 kalsan

Hey, @kratob!

What's your translation approach when you generate forms with ActiveType objects?

Best regards!

kdiogenes avatar Jul 10 '24 00:07 kdiogenes

@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.

kratob avatar Jul 10 '24 07:07 kratob

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)

triskweline avatar Jul 10 '24 09:07 triskweline

I was thinking about using ActiveType::Object as the base class for the form object.

kdiogenes avatar Jul 10 '24 11:07 kdiogenes