In my app I've made switching locales with sessions. The logic keeps in controller:
class SetLanguageController < ApplicationController
def russian
I18n.locale = :ru
set_session_and_redirect
end
def english
I18n.locale = :en
set_session_and_redirect
end
private
def set_session_and_redirect
session[:locale] = I18n.locale
redirect_to :back
rescue ActionController::RedirectBackError
redirect_to :root
end
end
switching works with links:
link_to_unless I18n.locale == :ru, "Русский", rus_locale_path
link_to_unless I18n.locale == :en, "English", eng_locale_path
code for locales in routes (unnecessary for question, but if you interesting)
get 'rus_locale' => 'set_language#russian'
get 'eng_locale' => 'set_language#english'
It works in development perfectly, but on production
application.rb
config.i18n.load_path += Dir[Rails.root.join('config','locales', '*.yml').to_s]
config.i18n.default_locale = :ru
How can I make it working on production? Thanks
I solved problem with setting a before_filter
in application controller like
before_filter :set_locale
def set_locale
I18n.locale = session[:locale] ? session[:locale] : I18n.default_locale
end
But this solution is dummy because it doesn't solve essence of problem actually - previous code worked in development, but in production. If you know how to fix it more smartly you are wellcome
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.