Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8472237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:00:30+00:00 2026-06-10T17:00:30+00:00

I have the following code that is working to assign a locale based first

  • 0

I have the following code that is working to assign a locale based first on a param, then a previously set cookie and then by requesting the HTTP_ACCEPT_LANGUAGE.

def set_locale
  if params[:locale] 
    I18n.locale = params[:locale]
  elsif cookies[:locale]
    I18n.locale = cookies[:locale]
  else 
    I18n.locale = sanitizeLocale(request.env["HTTP_ACCEPT_LANGUAGE"]) # e.g. "en,es;q=0.8,de-de;q=0.5,en-us;q=0.3"
  end
  cookies[:locale] = I18n.locale
end

def sanitizeLocale(locale)
  @locale, lang_weight = cookies[:locale] || begin
    if accept_lang = request.env["HTTP_ACCEPT_LANGUAGE"] # e.g. "en,es;q=0.8,de-de;q=0.5,en-us;q=0.3"
      accept_lang.to_s.split(',').collect {|l| x,y = l.split(';q='); [x.split('-').first, (y||1).to_f]}.sort_by(&:last).reverse.find {|lang, lang_weight| %w[es en it fr de].include?(lang)}
      # returns ["en", 1.0]
    end
  end || 'en' # default
  return @locale
end

My question is how can I check both the params[:locale] given and cookies[:locale] against the sanitizeLocale. I’m interesting in only get a valid value such as es en it fr de or default to en based on the input.

I been trying to just pass its values to sanitizeLocale similarly I’m doing for HTTP_ACCEPT_LANGUAGE for some reason I can’t understand is not working and is always defaulting to en even when params[:locale] given values are valid like it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:00:31+00:00Added an answer on June 10, 2026 at 5:00 pm

    First, we can simplify your Accept-Language parsing a bit:

    def acceptable_langs
      extract_langs = lambda do |l|
        x, y = l.split(';q=')
        [x.split('-').first, -(y || 1).to_f] # the -1 makes sort_by nicer
      end
      known_langs = lambda { |lang| %w[es en it fr de].include?(lang.first) }
      quality = :last
      language = :first
      langs = request.env['HTTP_ACCEPT_LANGUAGE'].to_s
                     .split(',')
                     .map(&extract_langs)
                     .select(&known_langs)
                     .sort_by(&quality)
                     .collect(&:language)
      langs + ['en']
    end
    

    The to_s will take care of nils and the lambdas and extra variables are just there to clarify the logic. You don’t have to do this, all you really need is a method that returns the language codes that are acceptable to both the user and your software ordered by the user’s preference.

    Now back to set_locale. You say that you need the language to match Accept-Langauge (with 'en' as a fallback) regardless of what the URL or cookies have to say. That means that you have to parse Accept-Language no matter what and then check params[:locale] and cookies[:locale] against that list:

    def set_locale
      # Figure out what they're asking for with English as the
      # default. Note that `acceptable_langs` always forces English
      # to be in the list.
      asking_for = params[:locale] || cookies[:locale] || 'en'
    
      # acceptable_langs is always in preference order so we just
      # want the first one that matches our `lang`.
      lang = acceptable_langs.find { |lang| asking_for == lang }
    
      # Now we have an acceptable language in `lang` (thanks to all
      # the `'en'` fallbacks we sprinkled about).
      I18n.locale = cookies[:locale] = lang
    end
    

    You could also add a little “I’ve already done all this work” flag to the cookies so that you could simply trust cookies[:locale] if the flag is set; there’s probably no reason to prematurely optimize set_locale though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that is working fine in jsfiddle - http://jsfiddle.net/darkajax/FHZBy/ I've
I have the following code that is working (clicking on the link will show
I have the following code that is not working correctly and I need to
I have the following code snippet that is working find but am trying to
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code that does not work due to a being a value
I have following code that does not work: I never get to goToFoodDetail .
I have following code snippet that i use to compile class at the run
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When
I have the following code that gets the MAC address of an interface: static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.