9

I am trying to create an Arabic version of a Rails app which is based on Blacklight. Here's the problem I am facing:

When I switch the language, everything translates perfectly EXCEPT the Blacklight labels. It's only when I refresh the page that the labels get translated.

This is the label I want to display: "ترتيب حسب عام" (Translation: Sort by Year)

But this is what I am getting currently: "Year ترتيب حسب"

The same issue happens when switching back to English from Arabic - the label displays in Arabic until I refresh the page.

This is what my Blacklight configuration looks like:

class CatalogController < ApplicationController
  include Blacklight::Catalog

  configure_blacklight do |config|
    # ...
    config.add_sort_field 'pub_date_sort desc, title_sort asc', :label => I18n.t('sortby.year')
    # ...
  end
end

Here's how I am setting the locale in my Application controller:

class ApplicationController < ActionController::Base

 before_filter :set_locale
 def set_locale
   I18n.locale = params[:locale] || I18n.default_locale
 end

 # ...
end

I am not sure what's happening and I would really appreciate your help in this.

2
  • you can set the arabic locale in rails console and check the value of I18n.t('sortby.year') Apr 10, 2017 at 10:37
  • @JagdeepSingh Yes, the value of I18n.t('sortby.year') is showing up correctly. It shows up correctly on the page as well once the page is refreshed. Apr 10, 2017 at 11:44

1 Answer 1

4
+25

I think the problem that the correct locale is not taken into account on the first page render lies in the fact that configure_blacklight is a class method that is run when the CatalogController class gets interpreted, i.e. before the before_filter for locale switching is run. (Actually I don't quite understand why it actually works after a page reload as I would expect the before filter always to be run after setting the blacklight configuration.)

Nevertheless, support for I18n-ing search and sort fields has recently been added to the Blacklight project - see Pull Request #1566. It seems that this change is not part of a public release yet so you need to use the master branch version to be able to actually use this feature.

Blacklight in general, as well as this new feature, uses Rails I18n API for localized labels so instead of manually specifying the labels you should use the I18n dictionary YAML files (see Rails Guides for more info and options).

In your particular case, the label for a sort field is looked up under the following key:

"blacklight.search.fields.sort.#{key}"

where key is the value of the first parameter that you pass to the sort field definition. Thus, for your particular field, you should define something like the following in your dictionaries:

en:    
  blacklight:
    search:
      fields:
        sort:
          pub_date_sort desc, title_sort asc: "Sort by Year"

(this would be for the English version). I am amazed that YAML has no problems with spaces and commas in the key definitions, but indeed I tested that such key works OK when used by the I18n API.

2
  • Thank you for the answer. I am using an older version of Blacklight which does not have support for localized labels, so this does not work for me. Apr 16, 2017 at 9:35
  • I see, I'm a bit skeptical that it will be possible on an older version of Blacklight, but perhaps I'm wrong and someone will find out. Good luck! Apr 17, 2017 at 16:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.