Changing the websites language based on browser

Discussion in 'JavaScript' started by kampbell411, Nov 13, 2007.

  1. #1
    Hi,

    I was wondering if anyone knew of a script or maybe point me in the right direction on how to change the content on a site to the language of the persons browser. For example, if a person in Germany came to my site and their browser settings where in German, my content would be in German and not English. Thanks.
     
    kampbell411, Nov 13, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    You can use something like this:

    
    <html>
    <head>
    <script type="text/javascript">
    
    function f_languageJump()
    {
      var l_lang;
      if (navigator.userLanguage) // For Explorer
        l_lang = navigator.userLanguage;
      else if (navigator.language) // For FireFox
        l_lang = navigator.language;	
      if (l_lang)
        { l_lang = l_lang.substring(0,2); }
      else
        { l_lang="z"; }
        
      // alert( "lang='" + l_lang + "'" );
        
      var page = "http://www.yoursite.com/"; 
        
      switch (l_lang)
      {
        case "es" : // Spanish
          page += "spanish_page.html"; break;
        case "en" : // English
          page += "english_page.html"; break;
        case "fr" : // French
          page += "french_page.html"; break;
        case "de" : // German
        case "it" : // Italian
        case "sv" : // Swedish
        default :
          page += "english_page.html"; break;
      }
      
      // alert( "page='" + page + "'" );
      top.location = page;
    }
      
    </script>
    </head>
      
    <body onload=f_languageJump();>
    </body>
    </html>
    
    Code (markup):
     
    ajsa52, Nov 13, 2007 IP
  3. kampbell411

    kampbell411 Peon

    Messages:
    82
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is there anyway to have it so that the browser detects the users area and changes the page content instead of going to a another page?
     
    kampbell411, Nov 13, 2007 IP
  4. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Yes, you can do it with dynamic html. Best way is with AJAX, but is not for beginners, and also that way is not SEO friendy.
     
    ajsa52, Nov 13, 2007 IP
  5. kampbell411

    kampbell411 Peon

    Messages:
    82
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Excellent. Thanks so much for your help. Ill check it out.
     
    kampbell411, Nov 13, 2007 IP