SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detecting Browser Language

    Hi:

    Is it possible to detect Browser Language with Javascript??

    What i want to be able to do is, Redirect user to appropriate language version for a site, based on javascript (if i have that language).
    ---------------------------
    Errors = Improved Programming.
    My Site

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    EU/UK+DK
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea how cross-browser or not this is, but it works in IE5-7, Firefox 1.5, and Opera 8.54 (and later, presumably) on Windows:
    Code:
    // Detect browser language
    
    if ( window.navigator.language ) // Firefox and Opera
    {
    	var lang = window.navigator.language;
    }
    else if ( navigator.browserLanguage ) // Internet Explorer, no surprises there...
    {
    	var lang = navigator.browserLanguage;
    }
    else // We don't know
    {
    	var lang = null;
    }
    You may want to restrict any string comparisons to the first two characters, and make them the same case first, since some browsers will say "en", some "en-US", and others "en-us", for example.

    Personally, though, I'd examine the HTTP headers sent by the browser (with server-side code, e.g. in PHP) for the accept-language value(s). It doesn't rely on JavaScript, and affords you more flexibility by not sending unnecessary markup to start with.

    Regardless, I hope this helps.
    The plus sign (+) is valid within an email address; please do not
    write or suggest code that precludes its use, as many use it as a
    "label" to filter incoming mail. </crusade>


Bookmarks

Posting Permissions

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