blacklight icon indicating copy to clipboard operation
blacklight copied to clipboard

Allow translation on search, sort field labels

Open hackartisan opened this issue 7 years ago • 2 comments

just needs to be backported to 6x release branch: https://github.com/projectblacklight/blacklight/commit/57c6bbf869fe2114282f3b745f8f595640d36877

example of use: https://github.com/projectblacklight/demo.projectblacklight.org/blob/master/config/locales/fr.yml#L3

hackartisan avatar Nov 20 '17 20:11 hackartisan

Ran into a bug in app/helpers/blacklight/configuration_helper_behavior.rb. The sort_field_label doesn't work properly.

In 6.15.0 en release-6.x branch:

def sort_field_label(key)
  field_config = blacklight_config.sort_fields[key]
  field_config ||= Blacklight::Configuration::NullField.new(key: key)

  field_label(
    :"blacklight.search.fields.sort.#{key}",
    (field_config.label if field_config),
    key.to_s.humanize
  )
end

The string "blacklight.search.fields.sort.#{key}" should be "blacklight.search.fields.sort.#{config_field.key}" just like label_for_search_field in order to find the proper key in the locale YML files.

However, this is still not going to work properly. Given a sort field in catalog_controller.rb like this:

config.add_sort_field 'score desc', label: 'relevance'

You'd end up with this lookup in the YAML file: "blacklight.search.fields.sort.score desc" which won't do anything. So, instead, I propose to change the template in the method to this: "blacklight.search.fields.sort.#{config_field.label}". Now the lookup would look like this instead: "blacklight.search.fields.sort.relevance

The other option would be foregoing the desc and asc modifiers when you add a sort field. But dropping functionality because of a code constraint doesn't seem very logical.

Final code would look like this:

  def sort_field_label(key)
    field_config = blacklight_config.sort_fields[key]
    field_config ||= Blacklight::Configuration::NullField.new(key: key)

    field_label(
      :"blacklight.search.fields.sort.#{field_config.label}",
      (field_config.label if field_config),
      key.to_s.humanize
    )
  end

netsensei avatar May 28 '18 20:05 netsensei

It looks like this was originally a backport issue and the backport was done. Then a bug was reported to this issue. If this bug is still valid (which it looks like it may be) perhaps we need a new issue for the bug? In the meantime I changed the labels.

hackartisan avatar Oct 05 '22 20:10 hackartisan