0

I've spent the last few hours trying everything possible to make this work, googled, redesigned, tested, etc. - but somehow doesn't get it working.

ok, I would like to set the I18n.locale - fairly simple.

Basically I implemented what is written on the guide here:

http://guides.rubyonrails.org/i18n.html

Application_controller.rb

before_action :set_locale

def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
end

In fact mine looks like this:

class ApplicationController < ActionController::Base
   logger.info "Point A..."
   before_action :set_locale

def set_locale
  logger.info "Point B..."
  I18n.locale = extract_locale_from_accept_language_header
end

 private
    def extract_locale_from_accept_language_header
        ....

This Problem is that the locale gets set before I can set the locale, when first accessing the page. I would like to read browser settings of user first, if no locale is yet set. To give the user a chance to get to their localized site. If they change the locale subsequently (change language - fine, then the locale is set and that's what they are using until they decide to switch again).

I placed two lines in the code above and this is the output:

Point A...
Processing by StaticPagesController#home as HTML
  Parameters: {"locale"=>"en"}
Point B...

I tried the following: - removed default locale in application.rb: #config.i18n.default_locale = :en - disabled all gems that could possible interfere (restarted the server each time)

Any help would be appreciated. Thanks, G

Georg Keferböck
  • 1,967
  • 26
  • 43

1 Answers1

0

Could you try and see if this works for you?

def set_locale 
  @locale = extract_locale_from_accept_language_header
  I18n.locale = @locale 
end

UPDATE:

After a lot of comments and the logs you paste it looks like you are getting the locale via routes. This is why you see in the log locale: en . But what you actually want to do is different, set the locale based on the headers and not the url. Remove the locale references from routes files and see if that works for you.

rodeleon
  • 362
  • 3
  • 18
  • Hi rodeleon, Thanks for your response. I tried, but it doesn't change anything. The issue is that the locale is set elsewhere before I set it in set_locale.... THIS MEANS: If the user doesn't access the site with browser language preference en, but lets say de - he/she will see the homepage in english instead of german. Once he/she clicks a link it will use the locale I had set previously in set_locale. – Georg Keferböck Apr 19 '14 at 02:25
  • It's just all delayed by one step ... I want to set the locale - I don't want that the locale gets set earlier. And according to the documentation that's what I am doing. It just doesn't do it. – Georg Keferböck Apr 19 '14 at 02:25
  • Could you update the answer and paste the code of extract_locale_from_accept_language_header ? Are you sure you are not setting there actually the wrong locale? – rodeleon Apr 19 '14 at 02:32
  • The locale is set correctly - that's why it switches once a link on the page has been clicked. If you look at the log output - between POINT A & POINT B the locale is set - not by me. And this is the issue. It's set elsewhere - I then set it but at this point it is too late. Once the user clicks on a link - the locale I defined is used. – Georg Keferböck Apr 19 '14 at 02:46
  • No, In the log you paste when you see Parameters: {"locale"=>"en"} doesn't mean the locale is set to English, it is that the request is sending as parameter locale: en. This could be because is passing in the url(mysite.com/?locale=en) or you have something in the view that is setting the locale to english. – rodeleon Apr 19 '14 at 03:01
  • Could you try changing in the config/application.rb the default locale to something other than english to see if it is the default behaviour that is set the first time you enter the page, try config.i18n.default_locale = :de and restart the server – rodeleon Apr 19 '14 at 03:02
  • That is indeed valuable information. I set the default language to :de and the site shows up as localhost:3000/de after restart. – Georg Keferböck Apr 19 '14 at 03:06
  • If I set the default language again to :en and do the folloiwng: def set_locale logger.info "Point B..." I18n.locale = :de #@locale = extract_locale_from_accept_language_header #I18n.locale = @locale end – Georg Keferböck Apr 19 '14 at 03:07
  • it sets it again to :en.... meaning that set_locale is ignored the first time round. – Georg Keferböck Apr 19 '14 at 03:08
  • Another perhaps interesting finding is: If I take english out of the available variables: config.i18n.available_locales = [:de, :cz] and do NOT define a default variable - it still uses :en after restarting server – Georg Keferböck Apr 19 '14 at 03:18
  • In rake routes - :en is being displayed alongside :cz and :de (although I thought I had removed en completely) – Georg Keferböck Apr 19 '14 at 03:20
  • Ok, now i got confused, what do you mean by rake routes is displaying locales? What you are trying to implement here is to not use the url to set the locale and instead use the headers. You should check your routes files and remove the references to locale. I think you are setting via routes the locale of the index page to english – rodeleon Apr 19 '14 at 03:27
  • And this should be the reason why you are reciving as parameter locale: en in the log you paste – rodeleon Apr 19 '14 at 03:29
  • Well, with reference I meant: root GET /:locale(.:format) static_pages#home {:locale=>/de|en/} – Georg Keferböck Apr 19 '14 at 03:31
  • And the resources that need to be available in different languages are scoped like this: scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do – Georg Keferböck Apr 19 '14 at 03:32
  • I followed http://guides.rubyonrails.org/i18n.html step-by-step to make sure I am doing things right. – Georg Keferböck Apr 19 '14 at 03:34
  • Could it be that these two lines in the routes.rb are ill-configured? get '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" } get '', to: redirect("/#{I18n.default_locale}/") – Georg Keferböck Apr 19 '14 at 03:42
  • Ok but you should see that the guides also say, "In specific cases, it would make sense to set the locale from client-supplied information, i.e. **not from the URL.**" so what you are trying to do here is both. What you have in your routes file is to get the locale from the url, and what you want to do is different, get the locale from headers.. – rodeleon Apr 19 '14 at 03:43
  • Ok, that all does make way more sense now. Could you please copy your last statement into the answer box so I can tick it off as answer. Thanks for all the help! – Georg Keferböck Apr 19 '14 at 03:54