Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 326
sergserg Avatar asked Jun 14 '13 05:06

sergserg


2 Answers

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

like image 54
Yevgeniy Anfilofyev Avatar answered Oct 06 '22 00:10

Yevgeniy Anfilofyev


In my case

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

did the trick.

like image 30
iampkuhz Avatar answered Oct 06 '22 01:10

iampkuhz