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.
As you can see the 3rd row is in English and the others in Danish. And the selected language is English.
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.
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