2

The docs are sketchy on how to do it properly. I tried this:

class ApplicationController < ActionController::Base
  # ...
  before_action :set_locale
  def set_locale
    session[:locale] = I18n.locale = params.delete(:locale) || session[:locale] || I18n.default_locale
  end
end

And this suffices for a lot of things; however, my fields are configured like this (following the Blacklight guide):

class CatalogController < ApplicationController
  include Blacklight::Catalog

  configure_blacklight do |config|
    # ...
    config.add_facet_field 'date', label: 'Date', single: false
    # ...
  end
end

This configuration happens before the request is processed, so if I try to use I18n.t('Date') for label, it will not respond to changes in locale, and will always serve the labels corresponding to the default locale.

What is the "correct" way to do this?

EDIT: Found the solution for the individual fields. Still searching for the "proper" solution for the search fields (config.add_search_field). It seems those are just displaying their label if present, and #labelize-d key if not. As a quick stop-gap measure, I made this class:

class Localized
  def initialize(key)
    @key = key
  end

  def to_s
    I18n.t(key)
  end
end

and configured the search field with

... label: Localized.new('blacklight.search.general.all_fields')
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • When you do `include Blacklight::Catalog` you change the ancestor's chain for `CatalogController `, which would typically be `CatalogController -> Blacklight::Catalog -> ApplicationController`, so when `set_locale` before action callback is called, it gets executed after `configure_backlight` method. Probably moving before action `set_locale` to `CatalogController` will fix it. – Surya Feb 13 '17 at 08:59
  • @Surya: Just tried, still stuck at `en.Date`. I've verified (by the advanced debugging technique of `puts "YO"`) that `configure_blacklight` block is only ever executed once (at the first request), not once per request. – Amadan Feb 13 '17 at 09:09
  • 1
    Looks like it has to invoked [via helper methods](https://github.com/projectblacklight/blacklight/blob/1cfced619f9582a0655d7151a5ec1c01dc99db97/spec/helpers/blacklight/search_history_constraints_helper_behavior_spec.rb#L85-L103): [`render_search_to_s_filters`](https://github.com/projectblacklight/blacklight/blob/291d0c08bb58c10bba9f2b35c9030945fabf0d53/app/helpers/blacklight/search_history_constraints_helper_behavior.rb#L30), pass `:f` key with respect to locale that is selected. – Surya Feb 13 '17 at 09:30
  • @Surya: Not quite the answer, but it gave me the vital clue. Thank you! – Amadan Feb 14 '17 at 00:39
  • @Amadan Where did you add the Localized class? I am struggling with a similar issue (https://stackoverflow.com/questions/43317700/page-needs-to-be-refreshed-after-switching-locale-for-blacklight-label-to-transl). The `configure_blacklight` method gets executed before the `set_locale` method, and moving `set_locale` to `CatalogController` did not work for me either. I would really appreciate your help in this. Thank you! – humairatasnim Apr 16 '17 at 09:29
  • @humairatasnim: `lib/localized.rb`. – Amadan Apr 17 '17 at 04:17

1 Answers1

1

Thanks to Surya, I saw where to look. The helper methods are already being invoked by the Blacklight's default templates, as long as one uses the correct keys. So, to localise the date field label in English, one needs the key named en.blacklight.search.fields.date.

Amadan
  • 191,408
  • 23
  • 240
  • 301