<% if I18n.locale == 'es' %> 不起作用
<% if I18n.locale == 'es' %> doesn't work
在我的 head.html.erb 我有这个
Language: <%= I18n.locale %>
<% if I18n.locale == 'es' %>This is Spanish
<% else % >This is NOT Spanish
<% end %>
但我明白了
Language: es
This is not Spanish
怎么可能?!
我尝试使用 <% if I18n.locale = 'es' %>,但如果 I18n.locale 是 de、en 等,也会显示消息“This is Spanish” .
I18n.locale
returns 一个符号,不是一个字符串。您需要与 :es
进行比较,而不是 'es'
.
.irb(main):001:0> I18n.locale
=> :en
irb(main):002:0> I18n.locale == 'en'
=> false
irb(main):003:0> I18n.locale == :en
=> true
在我的 head.html.erb 我有这个
Language: <%= I18n.locale %>
<% if I18n.locale == 'es' %>This is Spanish
<% else % >This is NOT Spanish
<% end %>
但我明白了
Language: es
This is not Spanish
怎么可能?!
我尝试使用 <% if I18n.locale = 'es' %>,但如果 I18n.locale 是 de、en 等,也会显示消息“This is Spanish” .
I18n.locale
returns 一个符号,不是一个字符串。您需要与 :es
进行比较,而不是 'es'
.
.irb(main):001:0> I18n.locale
=> :en
irb(main):002:0> I18n.locale == 'en'
=> false
irb(main):003:0> I18n.locale == :en
=> true