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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With