administrate
administrate copied to clipboard
Adding I18n translations for polymorphic fields
- What were you trying to do?
Translations for a polymorphic field item. here's an example. Item can be a program or a session and both are translated in the locale.yml file
I have a
GroupItemDashboard:
ATTRIBUTE_TYPES = {
# cannot be made searchable ...
item: Field::Polymorphic.with_options(
classes: [Program, Session],
order: "lms_data ->'name' ASC"
),
group: Field::BelongsTo.with_options(
searchable: false
),
}
FORM_ATTRIBUTES = %i[
item
group
].freeze
In my locale.yml file I have :
activerecord:
models:
item:
one: Session Ou Parcours
other: Sessions ou Parcours
program:
one: Parcours
other: Parcours
session:
one: Session
other: Sessions
group:
one: Groupe
other: Groupes
However, those translations for polymorphic fields are never taken into account. Neither for the input label or within the select options (see here label is "item", and select options refer to "programs" and not "parcours") :

Is there any way to add a translation for a polymorphic field ?
* What did you end up with (logs, or, even better, example apps are great!)?
* What versions are you running?
- Rails 6.0.3
- administrate 0.15.0
Hm, on first inspection, I think the problem stems from us not applying i18n in Field::Polymorphic#associated_resource_grouped_options (specifically the klass.to_s in line 12):
https://github.com/thoughtbot/administrate/blob/b3bb0a3b7833deceff71cfe1687ac36cec12a6e8/lib/administrate/field/polymorphic.rb#L10-L16
Perhaps we should be using #model_name instead of of #to_s. This would break if for some reason the field doesn't use ActiveModel or something compatible though... but perhaps that's not a concern here.