How to check if current locale matches string

I am trying to determine whether to show the Spanish or English button in my web application.

 <% if I18n.locale == 'es' %> <a href="<%= set_english_path %>" class="thin">English</a> <% else %> <a href="<%= set_spanish_path %>" class="thin">Spanish</a> <% end %> 

The if condition always fails, and the Spanish button is always displayed.

RubyMine shows this when checking (during debugging):

enter image description here

So why is the failure of comparison?

+8
ruby symbols ruby-on-rails ruby-on-rails-3 internationalization symbol
source share
3 answers

You should use a character instead of a string when searching / comparing / setting the locale. Try:

 <% if I18n.locale == :es %> 

The documentation for I18n is http://guides.rubyonrails.org/i18n.html

+24
source share

In my case

 if I18n.locale.to_s == 'zh-CN' ... 

did the trick.

+1
source share

% i (es es-CO) .include? I18n.locale

check more than one locale.

0
source share

All Articles