Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

human_attribute_name will does not work on ActiveType objects when using gettext_i18n_rails #188

Open
kalsan opened this issue Mar 15, 2024 · 6 comments

Comments

@kalsan
Copy link

kalsan commented Mar 15, 2024

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.

@kratob
Copy link
Member

kratob commented Mar 18, 2024

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.

@kalsan
Copy link
Author

kalsan commented Mar 18, 2024

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

@kdiogenes
Copy link
Contributor

Hey, @kratob!

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

Best regards!

@kratob
Copy link
Member

kratob commented Jul 10, 2024

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

@triskweline
Copy link
Member

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)

@kdiogenes
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants