Skip to content

Fix internationalization of model names in nested_has_many buttons #86

@atzzCokeK

Description

@atzzCokeK

Problem

When using non-English locales, the model names in the "Add" and "Remove" buttons are not correctly translated. The current implementation uses field.associated_class_name.titleize directly, which doesn't go through the Rails translation system.

For example, even when I have defined translations for model names in config/locales/ja.yml:

ja:
  activerecord:
    models:
      app_release: "お知らせ"

The button still displays "Add App Release" instead of "Add お知らせ".

Steps to Reproduce

  1. Set up a non-English locale (e.g., Japanese)
  2. Define translations for model names in the locale files
  3. Use nested_has_many fields in a form
  4. Observe that the model name in the buttons is not translated

Proposed Solution

I propose two possible solutions:

1. Prioritize the name_label option if it's present:

<%= link_to_add_association(
  field.options.try(:[], :name_label).present? ? 
    "#{I18n.t('administrate.fields.nested_has_many.add_with_name', name: field.options[:name_label])}" : 
    I18n.t("administrate.fields.nested_has_many.add", resource: field.associated_class_name.titleize),
  f,
  field.association_name,
  ...
) %>

2. Or use Rails' translation system to get the model name:

<%= link_to_add_association(
  I18n.t("administrate.fields.nested_has_many.add", resource: I18n.t("activerecord.models.#{field.associated_class_name.underscore}", default: field.associated_class_name.titleize)),
  f,
  field.association_name,
  ...
) %>

I'm happy to submit a pull request with this change if it's considered valuable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions