www.webdeveloper.com
Results 1 to 12 of 12

Thread: Browser Language Redirect

Hybrid View

  1. #1
    Join Date
    Oct 2007
    Posts
    38

    Browser Language Redirect

    Ahoy!

    I want to put this code in the head of my main index page so that it redirects to my translated pages only if the browser language is French, Spanish or German. If the browser language is anything else (including English) then I want it to remain on my main index page. I can't get it to work. It seems to want to redirect even if the language isn't "fr", "es" or "de". I'd really appreciate any help.

    Code:
    <script>
    var langcodes=new Array("fr", "es", "de")
    
    var langredirects=new Array("french.html", "spanish.html", "german.html")
    
    var languageinfo=navigator.language? navigator.language : navigator.userLanguage
    
    function redirectpage(dest){
    if (window.location.replace)
    window.location.replace(dest)
    else
    window.location=dest
    }
    
    for (i=0;i<langcodes.length-1;i++){
    if (languageinfo.substr(0,2)==langcodes[i]){
    redirectpage(langredirects[i])
    
    break
    }
    }
    </script>
    Thanks

  2. #2
    Join Date
    Feb 2008
    Posts
    47

  3. #3
    Join Date
    Oct 2007
    Posts
    38
    Thanks for replying. Trouble is I don't want it to redirect away from my main index page. I've tried variations of the script you've linked to but they all went into infinite loops. I need a script that redirects if and only if "fr", "es" and "de" are the browser languages. The script has to go in the head of my main (English) index page. Is this possible?

    (I know there are better server-side solutions to this but unfortunately they're not an option for me.)

  4. #4
    Join Date
    Oct 2007
    Posts
    38
    Forgot to mention I'd also rather use
    Code:
    window.location.replace
    than
    Code:
    document.location.href
    Thanks

  5. #5
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Is http://javascript.about.com/library/bllang.htm what you are looking for?
    Stephen

  6. #6
    Join Date
    Oct 2007
    Posts
    38
    Ahoy Stephen

    Thanks for replying. That's a clever script and I think it's just what I'm looking for but I can't get it to work. My site essentially only has one page (my index.html page). If a French, Spanish or German browser view my site I want to redirect them to mysite.com/fr.html, mysite.com/es.html or mysite.com/de.html. I want my default (English) page to continue to show mysite.com and not mysite.com/index.html. When I tried your script with a French browser it redirected to mysite-fr.com?? What am I doing wrong or is what I'm looking for not possible?

    Thanks again.

  7. #7
    Join Date
    Oct 2007
    Posts
    38
    Sorry when I said I want my default page to continue to show mysite.com and not mysite.com/index.html I meant and not mysite.com/en.html.

    Oops.
    Last edited by iGirl; 02-27-2008 at 05:29 PM.

  8. #8
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    The following part is what changes the file name.

    dest = dest.substr(0,dest.lastIndexOf('.')) + '-' + lang.substr(0,2) + dest.substr(dest.lastIndexOf('.'));

    Try changing that to:

    dest = dest.substr(0,dest.lastIndexOf('/')) + lang.substr(0,2) + dest.substr(dest.lastIndexOf('.'));

    and it should hopefully do what you want.
    Stephen

  9. #9
    Join Date
    Oct 2007
    Posts
    38
    I tried that and it redirected the French browser to www.mysite.comfr.com/ ?? Any suggestions?

  10. #10
    Join Date
    Aug 2006
    Location
    England
    Posts
    665
    Quote Originally Posted by iGirl
    I tried that and it redirected the French browser to www.mysite.comfr.com/ ?? Any suggestions?
    I think this should do it:
    Code:
    <script type='text/javascript'>
    
    var langPage;
     
    if( (langPage={fr:"french",es:"spanish",de:"german"}[ (navigator.language||navigator.userLanguage).substring(0,2) ]) )
     location.replace(langPage+".html");
    
    </script>
    Having said that, it's important to remember that the browser's language setting does not necessarily represent the language that the current user prefers to read. You may want to find yourself some flag graphics...
    Scripterlative.com

    PM = 'PayPal Mode'

  11. #11
    Join Date
    Dec 2012
    Posts
    1
    Quote Originally Posted by Arty Effem View Post
    I think this should do it:
    Code:
    <script type='text/javascript'>
    
    var langPage;
     
    if( (langPage={fr:"french",es:"spanish",de:"german"}[ (navigator.language||navigator.userLanguage).substring(0,2) ]) )
     location.replace(langPage+".html");
    
    </script>
    Having said that, it's important to remember that the browser's language setting does not necessarily represent the language that the current user prefers to read. You may want to find yourself some flag graphics...
    i want to use this script but i wish to do something like, if the browser lang is other than english, present another page.How do i do that?

  12. #12
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    You have all the elements !
    Make a logical plan, collect the information, then redirect according to your wishes!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles