Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    need help to read browser locale

    hi all,

    Can someone provide me code to read browser's locale settings in Javascript??

    Thanx in advance

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Not possible.

    JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server, access a database or the client's operating system, or alter the default behaviour of the browser.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,189
    Thanks
    10
    Thanked 569 Times in 550 Posts
    I have to (respectfully) disagree with Philip's cut and pasting here.

    you can get date and number formats by examining .toLocaleString().
    The navigator object provided some bits as well, ala navigator.language ...





    what info are you trying to find out?
    Last edited by rnd me; 05-11-2009 at 10:01 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • Users who have thanked rnd me for this post:

    shivark (05-12-2009)

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi, I agree with you.

    I need to read the browser locale settings using a javascript function and load the page depending on the locale.
    Can you provide a sample code which will do that?

    Thanks in advance

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I interpreted the request as attempting to identify the default language of the browser. That is, French, German etc. The locale of my browser is "en-gb".

    I don't think this can be done with Javascript. With Java, yes.

    Navigator.language (Firefox) and navigator.browserLanguage (IE) are not particularly useful since it really doesn't tell you anything about the person using the Web browser. You never know if it is a French user working with a German browser locale using a keyboard with Italian layout in Switzerland.

    The most reliable way of determining user preferences is to simply ask them, and store the result in a cookie.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,189
    Thanks
    10
    Thanked 569 Times in 550 Posts
    i agree that asking is the most reliable way.
    tomsharware.com is one example of this.
    flags might be better than words, although expats might grumble...

    there is some information to be had via javascript, and i don't think it's pointless to try to look. i would be willing to bet that the majority of users have their browsers setup to their native languages. Just because there's the occasional "French user working with a German browser locale using a keyboard with Italian layout in Switzerland", doesn't mean that our navigator.language is wrong that we both speak English, despite the different locale codes ("en-US" here). in this sample of 2, javascript is 100% accurate.



    shivark:
    what information are you looking for?
    a language code?
    that's been pointed out.
    still not sure exactly what you need/want.


    Philip:
    but i was wondering, what can java do that js can't ?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #7
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I need to read the List of languages in order of preference.
    (ex: In mozilla, tools->options->content->Languages)

    php sample code to achieve this is:
    <?php

    foreach($_SERVER as $KEY => $VALUE)
    if($KEY == "HTTP_ACCEPT_LANGUAGE")
    $x = $VALUE;
    $token = strtok($x, ",");
    $i=0;
    while ($token)
    {
    $a[$i]=$token;
    $token = strtok(",");
    $i++;
    }
    foreach ($a as $value)
    {
    $b = strtok($value,";");
    echo $b."<br>";
    }
    ?>

    If we can do the same using javascript, i need equivalent code in javascript for the above code snippet.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    See Post#2.

  • #9
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    you might be able to in FireFox dont know about others but if you could get an iframe to open the settings page, about:settings or something like that and get javascript to read the contents of the iframe and post it to your server with ajax it might work but i cant remeber what the firefox call is?

    Just google firefox advanced settings or some thing like that not sure how you will read source but i know its pos as Firebug and Apple Safari Developer tools can read iframes source have a look thats the only why i think it is pos
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by barkermn01 View Post
    you might be able to in FireFox dont know about others but if you could get an iframe to open the settings page, about:settings or something like that and get javascript to read the contents of the iframe and post it to your server with ajax it might work but i cant remeber what the firefox call is?

    Just google firefox advanced settings or some thing like that not sure how you will read source but i know its pos as Firebug and Apple Safari Developer tools can read iframes source have a look thats the only why i think it is pos
    You obviously have no real idea what you are talking about, and would have done better to remember the adage "It is a great misfortune neither to have enough wit to talk well nor enough judgement to be silent."

    But I have to say I that I would strong discourage any attempts to interrogate or interfere with the user's browser in any way. This is often the prelude to some sort of hacking or malware attack.

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,189
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by shivark View Post
    I need to read the List of languages in order of preference.
    (ex: In mozilla, tools->options->content->Languages)

    If we can do the same using javascript, i need equivalent code in javascript for the above code snippet.
    in that case, no, it probably cannot be done using javascript alone.
    you can set the accept-language in javascript according to the spec, (http://www.w3.org/TR/XMLHttpRequest/#xmlhttprequest), but you cannot read the defaults. a server must be used for this.

    so, you are limited to either using the primary language as discussed, or echoing back the accept-language request header info from php to your script.

    philosophically, i would tend to think that this is probably a task better suited for the server than for client.
    It's already to late by the time the client even has a chance...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #12
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks "Philip M" and "rnd me" for your valuable suggestions.
    Finally, i did this by echoing back the accept-language request header info from php to my script.

  • #13
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    about:config

    There go that to page in firefox, and it shows all the config settings if you open an iframe and read the source it might have all the config settings in it.

    Philip M - stop harasing me it took one google of firefox advanced config and i found it
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #14
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,189
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by barkermn01 View Post
    about:config

    There go that to page in firefox, and it shows all the config settings if you open an iframe and read the source it might have all the config settings in it.

    Philip M - stop harasing me it took one google of firefox advanced config and i found it
    frames are supposed to block access to other domains and protocols.
    I wouldn't think that http: would be able to access about: ...

    let us know if you can whip up a working demo.
    I for one would be very impressed.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

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