0

I want to get the language of non sign in user from the browser for this i have this code in my application controller

  def set_locale
    if user_signed_in? && !current_user.language.blank?
        I18n.locale = current_user.language
    else
      I18n.locale = extract_locale_from_accept_language_header

      if user_signed_in? && current_user.language.blank?
        current_user.language = I18n.locale
        current_user.save
      end
    end
  end

  def default_url_options(options={})
    { :locale => I18n.locale }
  end

  private

  def extract_locale_from_accept_language_header
    preferred_language = request.env['HTTP_ACCEPT_LANGUAGE'] || ''
    preferred_language = preferred_language.scan(/^[a-z]{2}/).first
    available_locales= ("en" "fr")
    if available_locales.include?(preferred_language)
      preferred_language
    else
      "en"
    end
  end

Suppose that my Browser language is french this give me http://localhost:3000/fr but the problem is when a non sign in user change language to English and go to another page for example http://localhost:3000/en/users/sign_up the language will change to French so i get http://localhost:3000/fr/users/sign_up and not http://localhost:3000/en/users/sign_up so I'm wondering how can i solve this problem

This is my route file

Update

scope :path => ":locale" do
.......
 end

# Catch all requests without a locale and redirect to the default...
  get '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }
  get '', to: redirect("/#{I18n.default_locale}")
2
  • 48
    Please do not deface your posts. Content on Stack Overflow is intended to help everyone, not just you. Commented Feb 6, 2015 at 18:25
  • 1
    Further, if you wish to be disassociated from your content (for whatever reason) you can either flag your post for moderator attention, using the 'custom'/'other' flag (who can't disassociate the content, but they can contact a developer/SE employee to have them do so), or you can contact the team directly, to have them do so on your behalf. Commented Feb 9, 2015 at 16:58

1 Answer 1

2

I think this would fix your issue:

  def set_locale
    if user_signed_in? && !current_user.language.blank?
        I18n.locale = current_user.language
    else
      I18n.locale = if params[:locale].present?
                      params[:locale] # Here you might want to do some checking to allow only your desired locales
                    else
                      extract_locale_from_accept_language_header
                    end

      if user_signed_in? && current_user.language.blank?
        current_user.language = I18n.locale
        current_user.save
      end
    end
  end

This will give precedence to a specified locale in the URL (en | fr) over the HTTP_ACCEPT_LANGUAGE header.

3
  • But this will always give me localhost:3000/en when trying to access localhost:3000 even if my Browser language is French
    – Loenvpy
    Commented Jan 3, 2015 at 13:23
  • It shouldn't. If you access localhost:3000, then params[:locale] won't be present, and then it will run the extract_locale method giving you french if your browser is set to use such language.
    – sjaime
    Commented Jan 3, 2015 at 14:05
  • If you get redirected to /en, then there is something that is setting the locale param elsewhere, for example the routes.rb file.
    – sjaime
    Commented Jan 3, 2015 at 14:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.