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?

On Mar 3, 2012, at 12:50 PM, Fresh M. wrote:

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 D. wrote in post #1049966:

On Mar 3, 2012, at 12:50 PM, Fresh M. wrote:

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!

On Mar 3, 2012, at 2:36 PM, Fresh M. wrote:

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 D. wrote in post #1049973:

On Mar 3, 2012, at 2:36 PM, Fresh M. wrote:

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
in english

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

So why “if (I18n.locale != ‘en’)” is true ?

Walter D. wrote in post #1049976:

On Mar 3, 2012, at 3:17 PM, Fresh M. wrote:

in english

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

On Mar 3, 2012, at 3:17 PM, Fresh M. wrote:

in english

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

On Mar 3, 8:17pm, Fresh M. [email protected] wrote:

returns this html:

en
in english

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

On Mar 3, 2012, at 3:41 PM, Fresh M. wrote:

Have you followed these steps:
extract_locale_from_accept_language_header
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs