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 6582137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:14:35+00:00 2026-05-25T16:14:35+00:00

I’ve my site set up so I just need to add ?lang=en or ?lang=es

  • 0

I’ve my site set up so I just need to add “?lang=en” or “?lang=es” to change languages English / Spanish.

When I enter the site with, for ex, “http://domain.com/something/something_else?lang=es”, a cookie is set so I continue to navigate the site in that language.

I would like to redirect my users first by the “Accept-Language” value of their browser, but then allow them to continue to navigate the site in other language if they want to.

What would be the best way to do it? Would .htaccess work along with the cookie that’s set when the language is chosen?

EDIT: Here’s my updated code with Paul answer:

EDIT2: Oh, I just have “en” and “es” languages. I’m not sure on how this code wpuld choose only between this two or set the default… :/

    if (isset($_GET["lang"]))
        $this->setLanguage($_GET["lang"]);
    elseif (isset($_COOKIE["language"]))
        $this->setLanguage($_COOKIE["language"]);
    elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
        {
        // Parse the Accept-Language according to:
        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
        preg_match_all(
           '/([a-z]{1,8})' . // First part of language e.g en
           '(-[a-z]{1,8})*\s*' . // other parts of language e.g -us
           // Optional quality factor
           '(;\s*q\s*=\s*((1(\.0{0,3}))|(0(\.[0-9]{0,3}))))?/i',
           $_SERVER['HTTP_ACCEPT_LANGUAGE'],
           $langParse);

        $langs = $langParse[1];
        $quals = $langParse[4];

        $numLanguages = count($langs);
        $langArr = array();

        for ($num = 0; $num < $numLanguages; $num++)
        {
           $newLang = strtoupper($langs[$num]);
           $newQual = isset($quals[$num]) ?
              (empty($quals[$num]) ? 1.0 : floatval($quals[$num])) : 0.0;

           // Choose whether to upgrade or set the quality factor for the
           // primary language.
           $langArr[$newLang] = (isset($langArr[$newLang])) ?
              max($langArr[$newLang], $newQual) : $newQual;
        }

        // sort list based on value
        arsort($langArr, SORT_NUMERIC);
        $acceptedLanguages = array_keys($langArr);
        $preferredLanguage = reset($acceptedLanguages);

        $this->setLanguage($preferredLanguage);
     }
     else
        $this->setLanguage("en");
  • 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-05-25T16:14:36+00:00Added an answer on May 25, 2026 at 4:14 pm

    I do this in PHP. Accept-Language is a complex thing. A browser can suggest more than one language that it would like to accept (each with a quality factor that shows which it prefers). For my site I have a default language to display (which is shown when none of the Accept-Languages is in my translation list). Otherwise if there is no language set (setLang) I choose it based on the most acceptable for the browser by parsing the Accept-Language. The function I use is below (it contains my session manager for setting cookies – but you could reimplement that with direct calls to $_SESSION[etc] = $foo;).

    Edit: Unfortunately my website only has translations for the primary languages (EN, ES, FR) rather than (en_US, en_GB, es_MX, es_ES) so I choose the highest quality factor specified in these for the primary language.

       public function setLanguage($setLang='')
       {
          if (!empty($setLang))
          {
             $this->setup['Session']->set($this->setup['Lang_Key'], $setLang);
          }
          else
          {
             if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
             {
                // Parse the Accept-Language according to:
                //    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
                preg_match_all(
                   '/([a-z]{1,8})' . // First part of language e.g en
                   '(-[a-z]{1,8})*\s*' . // other parts of language e.g -us
                   // Optional quality factor
                   '(;\s*q\s*=\s*((1(\.0{0,3}))|(0(\.[0-9]{0,3}))))?/i',
                   $_SERVER['HTTP_ACCEPT_LANGUAGE'],
                   $langParse);
    
                $langs = $langParse[1];
                $quals = $langParse[4];
    
                $numLanguages = count($langs);
                $langArr = array();
    
                for ($num = 0; $num < $numLanguages; $num++)
                {
                   $newLang = strtoupper($langs[$num]);
                   $newQual = isset($quals[$num]) ?
                      (empty($quals[$num]) ? 1.0 : floatval($quals[$num])) : 0.0;
    
                   // Choose whether to upgrade or set the quality factor for the
                   // primary language.
                   $langArr[$newLang] = (isset($langArr[$newLang])) ?
                      max($langArr[$newLang], $newQual) : $newQual;
                }
    
                // sort list based on value
                arsort($langArr, SORT_NUMERIC);
                $acceptedLanguages = array_keys($langArr);
                $preferredLanguage = reset($acceptedLanguages);
    
                $this->setup['Session']->set(
                   $this->setup['Lang_Key'], $preferredLanguage);
             }
             else
             {
                $this->setup['Session']->set(
                   $this->setup['Lang_Key'], $this->setup['Default_Language']);
             }
          }
    
          return $this->setup['Session']->get($this->setup['Lang_Key']);
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to

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.