Retrieve the user's browser language

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dharyl
    New Member
    • Dec 2007
    • 5

    Retrieve the user's browser language

    Hi Everyone,

    I'm dharyl from Philippines, I had a problem in knowing the user's browser Language. I was asked to have an alert pop up when the user of the site is using other language than "en-us".
    I had this code below just to test what language setting should the page read from the user.
    [CODE=javascript]<script language="JavaS cript">
    var a = navigator.brows erLanguage;
    var b = navigator.userL anguage;
    var c = navigator.syste mLanguage;
    alert("browser language");
    alert(a);
    alert("user language");
    alert(b);
    alert("system language");
    alert(c);
    </script>[/CODE]
    Code:
    navigator.browserLanguage --> en-us
    navigator.systemLanguage --> en-us
    navigator.userLanguage --> en-us
    when I change my browser language from Tools>InternetO ptions>General> Languages
    results are the same:
    Code:
    navigator.browserLanguage --> en-us
    navigator.systemLanguage --> en-us
    navigator.userLanguage --> en-us
    when I change my browser language from
    Start>ControlPa nel>Regionaland LanguageOptions >Regional Options tab
    the value of userLanguage changes just as what I set:
    Code:
    navigator.browserLanguage --> en-us
    navigator.systemLanguage --> en-us
    navigator.userLanguage --> Ja
    That means navigator.userL anguage is for this one
    Start>ControlPa nel>Regionaland LanguageOptions >Regional Options tab

    Now my point is how would I get the value of language setting from this one?
    Tools>InternetO ptions>General> Languages

    Because I guess that's what I need for my task to be done.
    Hope you guys can help me on this.


    Thanks in advance,
    dha
    Last edited by gits; Dec 27 '07, 08:42 AM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Hi, welcome to TSDN!

    Some or all of these properties are not supported by most browsers. See this link.

    Comment

    • dharyl
      New Member
      • Dec 2007
      • 5

      #3
      Originally posted by acoder
      Hi, welcome to TSDN!

      Some or all of these properties are not supported by most browsers. See this link.

      Hi acoder,

      Thanks for the reply. I think all I need for now is to make it work in IE.
      Or are there no more way for my codes to know what browser language the user is using? Coz I really need solution for this problem.


      thanks,
      dharyl

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        If you want an IE-only solution, you could use ActiveX/vbscript instead. This allows you to access the local settings when the correct security level is enabled.

        Comment

        • dharyl
          New Member
          • Dec 2007
          • 5

          #5
          Originally posted by acoder
          If you want an IE-only solution, you could use ActiveX/vbscript instead. This allows you to access the local settings when the correct security level is enabled.


          but I need to do it in JavaScript

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by dharyl
            but I need to do it in JavaScript
            Then sorry, but it's not possible.

            Comment

            • dharyl
              New Member
              • Dec 2007
              • 5

              #7
              Originally posted by acoder
              Then sorry, but it's not possible.

              Hi acoder,

              thanks for your time. if ever I'll be able to find solution for this, I'll send it to you, maybe somebody will need it in the near future.


              Thanks for time,

              dharyl :)

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Yes, that's the spirit! You'll probably need a mixture of JScript (IE version of JavaScript) and vbscript or something along those lines. Good luck!

                Comment

                • dharyl
                  New Member
                  • Dec 2007
                  • 5

                  #9
                  hi acoder,


                  Good news!
                  I have resolved my issue, but not with the help of JavaScript. I used VB.Net, I created a webpart with MOSS2007, the code was VB.Net.
                  I used this line of code below to retreive the user's browser language which is being passed to the server using the HTTP header.

                  HttpContext.Cur rent.requestSer verVariables("H TTP_ACCEPT_LANG UAGE")

                  Hope this would help someone in the future.



                  And by the way I had a new issue encountered in one of my pages.
                  When I use the word search as search term, the page returns error.
                  Do you have any thoughts on this?


                  Thanks,

                  dha

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by dharyl
                    Good news!
                    I have resolved my issue, but not with the help of JavaScript. I used VB.Net, I created a webpart with MOSS2007, the code was VB.Net.
                    I used this line of code below to retreive the user's browser language which is being passed to the server using the HTTP header.

                    HttpContext.Cur rent.requestSer verVariables("H TTP_ACCEPT_LANG UAGE")
                    I'm glad you got it working.
                    Originally posted by dharyl
                    Hope this would help someone in the future.
                    I'm sure it will. Thanks for posting your solution.

                    Note that I've changed the thread title to make it easier for others with a similar problem to find this thread.
                    Originally posted by dharyl
                    And by the way I had a new issue encountered in one of my pages.
                    When I use the word search as search term, the page returns error.
                    Do you have any thoughts on this?
                    If it's a JavaScript error, start a new thread for this mentioning the error message.

                    Comment

                    • vishwaskulkarni
                      New Member
                      • Mar 2008
                      • 1

                      #11
                      Other Than VB.NET solution

                      Other Than VB.NET solution

                      If you are using JAVA or PHP or ASP, following is the solution

                      In ASP, you can use the ServerVariables collection of the Request object to retrieve the value of HTTP headers. You can choose either VBScript or JScript

                      $accept_languag e = $ENV{"HTTP_ACCE PT_LANGUAGE"};

                      Retrieving HTTP Headers with PHP

                      In PHP, the value of HTTP headers are stored in the $_SERVER array. Here is the code for retrieving HTTP headers:

                      $accept_languag e = $_SERVER["HTTP_ACCEPT_LA NGUAGE"];

                      Retrieving HTTP Headers with Java Servlet / JSP

                      String accept_language = request.getHead er("accept-language");

                      As you can see, to retrieve the value of an HTTP header whose name is x in Java Servlet or JSP, we use request.getHead er("x"), where request is an instance of the javax.servlet.h ttp.HttpServlet Request class.
                      Last edited by vishwaskulkarni; Mar 20 '08, 10:46 AM. Reason: For completion and spell check

                      Comment

                      Working...