Skip to content

Translation when using with SimpleForm #1160

@eric-hemasystems

Description

@eric-hemasystems

When using Ransack with SimpleForm the translation is not working. If I do:

<%= f.input :status_eq, collection: %w[Active Inactive] %>

Then my label ends up being "Status eq" rather than "Status equal". This is due to not using the translation support in Ransack. This could be an issue for SimpleForm rather than Ransack. But since the existing integration between the two systems has Ransack modifying itself for SimpleForm rather than the other way around it seemed perhaps start with an issue here.

I do have a monkey-patch to fix it up but it involves monkey-patching a SimpleForm method so not sure it is a suitable strategy. If you don't see another strategy from the Ransack side I can try opening a ticket with SimpleForm to see if they would be willing to modify their method.

My monkey-patch:

module SimpleFormRansackTranslate
  # Adapted from:
  #   https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/helpers/form_builder.rb#L35-L37
  #
  # Decorating:
  #   https://github.com/heartcombo/simple_form/blob/master/lib/simple_form/components/labels.rb#L75-L83
  #
  # NOTE: Decorated method is protected so higher risk of breakage on upgrade
  def label_translation
    if object.respond_to? :translate
      object.translate reflection_or_attribute_name.to_s, include_associations: true
    else
      super
    end
  end
end

SimpleForm::Inputs::Base.prepend SimpleFormRansackTranslate

This related to PR #341 where support for SimpleForm was added.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions