sergserg
sergserg

Reputation: 22264

How to check if current locale is equal to a string

I'm trying to determine whether to show the Spanish or English button on 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 show this upon inspection (during debugging):

enter image description here

So why is the comparison failing?

Upvotes: 11

Views: 8799

Answers (2)

iampkuhz
iampkuhz

Reputation: 31

In my case

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

did the trick.

Upvotes: 3

Yevgeniy Anfilofyev
Yevgeniy Anfilofyev

Reputation: 4847

You should use symbol instead of string when searching/comparing/setting locale. Try:

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

Documentation for I18n is there http://guides.rubyonrails.org/i18n.html

Upvotes: 28

Related Questions