I am learning Rails and I want to setup localisation for my app. I can do it without any issue for each user (i.e. each user can select their language and the app changes to the selected language) but I want to add the option to change language even before login so everyone can understand how the app works or what does it offers, contact and about pages and so on.
I have added a dropdown menu with the list of languages and the each button will call an action on my "staticpages" controller. Here is how the dropdown looks like:
<ul class="dropdown-menu">
<% get_languages_list.each do |lan| %>
<li> <%= link_to lan[0], {controller: "static_pages", action: "change_locale", locale: lan[1]}, method: "post" %></li>
<%end%>
</ul>
where get_languages_list is a helper that returns and array of arrays with the languages my app support with a human name, for example [["English","en"],["Español"],["es"],...]. The action that it calls is this one:
def change_locale
if params.include? :locale
I18n.default_locale = lan locale
end
redirect_to root_path
end
I thought that with this it would be enough but it seems that I18n doesn't remember what the locale is. With my user model, in order to change the locale what I do is a before_action filter and set the locale to the locale of the user but in this case, when I don't have any session opened how can I change the locale to the specific one selected by the user?