11

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?

2 Answers 2

28

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

Sign up to request clarification or add additional context in comments.

Comments

3

In my case

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

did the trick.

1 Comment

I think this might be preferred: if I18n.locale.to_s == :'zh-CN'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.