Rails i18n 同时显示两种语言
Rails i18n is showing two languages at the same time
我的网站使用两种语言,英语和丹麦语。
1) 我有一个 link 的列表,如下所示:
<%= link_to html_viewer_url(activity.course, activity), target: '_blank' do %>
<%= t('.show') %>
<i class="fa fa-angle-right" aria-hidden="true"></i>
<% end %>
翻译 t('.show')
大多数时候会显示正确的语言,但有时,如果语言设置为英语,那么其中一两个 link 会以丹麦语显示,反之亦然。
2) 我有一个 link 到 http://localhost:3000/da/users/edit
。当我在此页面上按 link 切换语言时,URL 更改为 ...:3000/en/...
,但语言仍为丹麦语。如果我随后单击最初将我带到 /users/edit
的相同 link,语言将切换为英语。每次点击,它都会在英语和丹麦语之间交替。但是 URL 保持不变,包含 /en/
。这只发生在 .../users/edit
页面上,没有其他地方。
要切换语言,我使用以下link。 default_locale
是 da
。
<% if I18n.locale == I18n.default_locale %>
<%= link_to "English", { :locale=>'en' } %>
<% else %>
<%= link_to "Dansk", { :locale=>'da' } %>
<%end%>
在我的 routes.rb
中,我有以下用户的 URL:
scope "/:locale" do
resources :users, except: [:new, :create, :edit]
end
这与其他语言更改工作的页面相同。
知道是什么原因造成的吗?
编辑:
这是呈现列表时的外观示例。
如您所见,第 3 行是英语,其他行是丹麦语。并且选择的语言是英语。
您使用的 Rails 是什么版本?
我的猜测是您正在使用 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 将解决您的问题。
我的网站使用两种语言,英语和丹麦语。
1) 我有一个 link 的列表,如下所示:
<%= link_to html_viewer_url(activity.course, activity), target: '_blank' do %>
<%= t('.show') %>
<i class="fa fa-angle-right" aria-hidden="true"></i>
<% end %>
翻译 t('.show')
大多数时候会显示正确的语言,但有时,如果语言设置为英语,那么其中一两个 link 会以丹麦语显示,反之亦然。
2) 我有一个 link 到 http://localhost:3000/da/users/edit
。当我在此页面上按 link 切换语言时,URL 更改为 ...:3000/en/...
,但语言仍为丹麦语。如果我随后单击最初将我带到 /users/edit
的相同 link,语言将切换为英语。每次点击,它都会在英语和丹麦语之间交替。但是 URL 保持不变,包含 /en/
。这只发生在 .../users/edit
页面上,没有其他地方。
要切换语言,我使用以下link。 default_locale
是 da
。
<% if I18n.locale == I18n.default_locale %>
<%= link_to "English", { :locale=>'en' } %>
<% else %>
<%= link_to "Dansk", { :locale=>'da' } %>
<%end%>
在我的 routes.rb
中,我有以下用户的 URL:
scope "/:locale" do
resources :users, except: [:new, :create, :edit]
end
这与其他语言更改工作的页面相同。
知道是什么原因造成的吗?
编辑:
这是呈现列表时的外观示例。
如您所见,第 3 行是英语,其他行是丹麦语。并且选择的语言是英语。
您使用的 Rails 是什么版本?
我的猜测是您正在使用 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 将解决您的问题。