Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails i18n is showing two languages at the same time

I use two languages for a site, English and Danish.

1) I have a list of links that look like this:

<%= link_to html_viewer_url(activity.course, activity), target: '_blank' do %>
    <%= t('.show') %>
    <i class="fa fa-angle-right" aria-hidden="true"></i>
<% end %>

Translation t('.show') shows the correct language most of the time, but every now and then, if the language is set to English, then one or two of those links are shown in Danish, and vice versa.

2) I have a link to http://localhost:3000/da/users/edit. When I am on this page, and press the link to switch language, the URL changes to ...:3000/en/..., but the language remains in Danish. If I then click the same link that originally took me to /users/edit, the language switches to English. On each click, it alternates between English and Danish. But the URL remains the same, containing /en/. This happens only on the .../users/edit page, no where else.

To switch language, I use the following link. default_locale is da.

<% if I18n.locale == I18n.default_locale %>
    <%= link_to "English", { :locale=>'en' } %>
<% else %>
    <%= link_to "Dansk", { :locale=>'da' } %>
<%end%>

In my routes.rb, I have the following for the user's URL:

scope "/:locale" do
    resources :users, except: [:new, :create, :edit]
end

which is same with other pages where the language changing work.

Any ideas what might cause this?

EDIT: Here is an example of what it can look like when rendering a list. enter image description here

As you can see the 3rd row is in English and the others in Danish. And the selected language is English.

like image 871
just_user Avatar asked Jul 31 '18 10:07

just_user


1 Answers

What version of Rails are you using?

My guess is that you are using fragment caching (perhaps by default) but are not including the locale in the cache key. I haven't tried it, but perhaps the cache_with_locale gem will fix your problem.

like image 192
Old Pro Avatar answered Nov 14 '22 19:11

Old Pro