I made a language change and it works, but sometimes after several page updates, the language changes to another, then back - please help, I don't know what to do here
Method for lang change:
def switch_locale
locale = params[:format].to_sym
I18n.locale = locale
redirect_to request.referrer
end
buttons for lang change:
.min-h-screen.flex.flex-col.items-center.justify-start.fixed.top-0.left-0.w-full.pb-12.pt-32
= button_to("rus", switch_locale_path('ru'), class: "btn font-bold btn-main rounded-lg fixed top-0", style: "right: 0.25rem")
= button_to("en", switch_locale_path('en'), class: "btn font-bold btn-main rounded-lg fixed top-0", style: "right: 4rem")
I suggest you to put locale in cookies so it can be persisted in user's browser (more about it here). Adding
|| root_pathis important, as user can accessswitch_localedirectly in their browser's address bar, so user will not have error when they do that.then use
before_actiononApplicationControllerto set locale from cookies, so user will have same experience when access any page after switch language, and if cookies does not exist we can use default locale (you can replace:enwithI18n.default_localeif you want to use default fromI18nconfig)