Why this doesn't work?

Why this doesn't work?

<%= link_to 'in english', :locale => 'en' if I18n.locale != 'en' %>

And how to make it work?

Why this doesn't work?

<%= link_to 'in english', :locale => 'en' if I18n.locale != 'en' %>

And how to make it work?

The answer to both is parentheses.

Walter

Walter Davis wrote in post #1049966:

Walter Davis wrote in post #1049966:

Why this doesn't work?

<%= link_to 'in english', :locale => 'en' if I18n.locale != 'en' %>

And how to make it work?

The answer to both is parentheses.

Walter

<%= link_to('in english', :locale => 'en') if (I18n.locale != 'en') %> ?

Doesn't work!

Then please define doesn't work. The former would throw a concatenation error. The new version does exactly what that you don't expect? Another hint -- what is the precise value of the locale (value AND format)?

Walter

Walter Davis wrote in post #1049973:

Walter

<%= link_to('in english', :locale => 'en') if (I18n.locale != 'en') %> ?

Doesn't work!

Then please define doesn't work.

Ok, this code:

<%= I18n.locale %>
<%= link_to('in english', :locale => 'en') if (I18n.locale != 'en') %>

returns this html:

en
<a href="/welcome/index?locale=en">in english</a>

Why? If I18n.locale == 'en' there should not be link.

So why "if (I18n.locale != 'en')" is true ?

Walter Davis wrote in post #1049973:

Walter

<%= link_to('in english', :locale => 'en') if (I18n.locale != 'en') %> ?

Doesn't work!

Then please define doesn't work.

Ok, this code:

<%= I18n.locale %>
<%= link_to('in english', :locale => 'en') if (I18n.locale != 'en') %>

returns this html:

en
<a href="/welcome/index?locale=en">in english</a>

Why? If I18n.locale == 'en' there should not be link.

So why "if (I18n.locale != 'en')" is true ?

Have you followed these steps:

http://guides.rubyonrails.org/i18n.html#setting-and-passing-the-locale

I think that you're missing the part where you take the params[:locale] and do something with it.

Walter

Walter Davis wrote in post #1049976:

<a href="/welcome/index?locale=en">in english</a>

Why? If I18n.locale == 'en' there should not be link.

So why "if (I18n.locale != 'en')" is true ?

Have you followed these steps:

I made it like this:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_locale

  def set_locale
    locale = params[:locale] || session[:locale] ||
extract_locale_from_accept_language_header
    if(locale.match /^(ru|en)$/)
      I18n.locale = locale
      session[:locale] = locale
    end
  end

  private

  def extract_locale_from_accept_language_header
      request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
  end

end

Walter Davis wrote in post #1049976:

<a href="/welcome/index?locale=en">in english</a>

Why? If I18n.locale == 'en' there should not be link.

So why "if (I18n.locale != 'en')" is true ?

Have you followed these steps:

I made it like this:

class ApplicationController < ActionController::Base
protect_from_forgery

before_filter :set_locale

def set_locale
   locale = params[:locale] || session[:locale] ||
extract_locale_from_accept_language_header
   if(locale.match /^(ru|en)$/)
     I18n.locale = locale
     session[:locale] = locale
   end
end

private

def extract_locale_from_accept_language_header
     request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end

end

----

And if <%= I18n.locale %> prints on page "en" so I18n.locale is set

Anything you print all by itself inside an erb block gets an implicit conversion to string. But a comparison or concatenation isn't going to do the same for you. 'en' != :en, for example. You could try <%= I18n.locale == 'en' ? 'true' : 'false' %> and compare what you get with <%= I18n.locale.to_s == 'en' ? 'true' : 'false' %> to see if this is your issue.

Walter

returns this html:

en
<a href="/welcome/index?locale=en">in english</a>

Why? If I18n.locale == 'en' there should not be link.

So why "if (I18n.locale != 'en')" is true ?

I18n.locale is a symbol and :en is not == to 'en'

Fred