How to check HTTP_ACCEPT_LANGUAGE using Javascript?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edo van der Zouwen

    How to check HTTP_ACCEPT_LANGUAGE using Javascript?

    It's quite easy in Php:

    <?php

    $language = $_SERVER['HTTP_ACCEPT_LA NGUAGE'];
    $language_short = substr($taal,0, 2);

    echo $language_short ;
    ?>


    But would love to do it in Javascript..... .

    Anybody?

    Many thanks in advance!!


    Edo.
  • Janwillem Borleffs

    #2
    Re: How to check HTTP_ACCEPT_LAN GUAGE using Javascript?


    "Edo van der Zouwen" <ezouwen@haalwe g_removthis_and this_xs4all.nl> schreef
    in bericht news:Xns93FDA5C 9EAB43Edovander Zouwen@216.168. 3.44...[color=blue]
    >
    > But would love to do it in Javascript..... .
    >
    > Anybody?[/color]

    IE extends the navigator object with two properties you could reference:

    navigator.syste mLanguage
    navigator.userL anguage

    Doesn't work in other browsers and I'm not shure whether this would work on
    a mac or not...


    JW



    Comment

    • Stephen

      #3
      Re: How to check HTTP_ACCEPT_LAN GUAGE using Javascript?

      Janwillem Borleffs wrote:
      [color=blue]
      > "Edo van der Zouwen" <ezouwen@haalwe g_removthis_and this_xs4all.nl> schreef
      > in bericht news:Xns93FDA5C 9EAB43Edovander Zouwen@216.168. 3.44...
      >[color=green]
      >>But would love to do it in Javascript..... .
      >>
      >>Anybody?[/color]
      >
      >
      > IE extends the navigator object with two properties you could reference:
      >
      > navigator.syste mLanguage
      > navigator.userL anguage
      >
      > Doesn't work in other browsers and I'm not shure whether this would work on
      > a mac or not...
      >[/color]

      Gecko based browsers look like they use

      navigator.langu age

      My reference says this goes back to NN4.

      .... this doesn't seem to give exactly what the ACCEPT_LANGUAGE header
      would have, but it might be close enough ...

      Regards,
      Stephen
      [color=blue]
      >
      > JW
      >
      >
      >[/color]

      Comment

      • Edo van der Zouwen

        #4
        Re: How to check HTTP_ACCEPT_LAN GUAGE using Javascript?

        Stephen <ssansom@austin .rr.com> wrote in
        news:mRkbb.5833 5$z32.41740@twi ster.austin.rr. com:
        [color=blue]
        > Janwillem Borleffs wrote:
        >[color=green]
        >> "Edo van der Zouwen" <ezouwen@haalwe g_removthis_and this_xs4all.nl>
        >> schreef in bericht
        >> news:Xns93FDA5C 9EAB43Edovander Zouwen@216.168. 3.44...
        >>[color=darkred]
        >>>But would love to do it in Javascript..... .
        >>>
        >>>Anybody?[/color]
        >>
        >>
        >> IE extends the navigator object with two properties you could
        >> reference:
        >>
        >> navigator.syste mLanguage
        >> navigator.userL anguage
        >>
        >> Doesn't work in other browsers and I'm not shure whether this would
        >> work on a mac or not...
        >>[/color]
        >
        > Gecko based browsers look like they use
        >
        > navigator.langu age
        >
        > My reference says this goes back to NN4.
        >
        > ... this doesn't seem to give exactly what the ACCEPT_LANGUAGE header
        > would have, but it might be close enough ...
        >
        > Regards,
        > Stephen
        >[color=green]
        >>
        >> JW
        >>
        >>
        >>[/color]
        >[/color]

        Thanks for both replies. However. the .userlanguage .browserlanguag e or
        ..systemlanguag e properties give the language of the browser or system.

        In a browser, you can however also set the 'preferred language' in which
        you can see a webpage.

        For example, many people outside the US or UK have an english browser,
        but would like to see webpages in their home language. This can not be
        solved by using the properties above.

        Any more ideas?

        Thanks,


        Edo.


        P.S. By the way, for those interested, this is the code for checking the
        browser language:

        (example for dutch, polish and english users, which will be directed to
        index_en.html, index_pl.html or index_nl.html)

        <SCRIPT LANGUAGE="JavaS cript1.2"><!--
        // The following only works in JavaScript 1.2 or greater:
        function showpage(code) {

        location = ("index_" + code + ".html");
        }


        if (navigator.appN ame == 'Netscape')
        var language = navigator.langu age;
        else
        var language = navigator.brows erLanguage;

        var code = language.substr ing(0,2);

        if (code == 'pl' || code == 'nl' || code == 'en')
        showpage(code);
        else
        showpage('en');
        //--></SCRIPT>

        Comment

        • Stephen

          #5
          Re: How to check HTTP_ACCEPT_LAN GUAGE using Javascript?

          Edo van der Zouwen wrote:[color=blue]
          >
          > [...snip suggestions to use navigator.userL anguage, navigator.langu age, etc. ...]
          >
          >
          > Thanks for both replies. However. the .userlanguage .browserlanguag e or
          > .systemlanguage properties give the language of the browser or system.
          >
          > In a browser, you can however also set the 'preferred language' in which
          > you can see a webpage.
          >
          > For example, many people outside the US or UK have an english browser,
          > but would like to see webpages in their home language. This can not be
          > solved by using the properties above.
          >
          > Any more ideas?
          >[/color]

          As far as I know, the HTTP request & headers sent with the request are
          not accessible to javascript. This is probably (my guess) because these
          are formed immediately before transmission and too late for server-side
          scripting to make use of.

          I believe I mentioned before, my prefered solution would be to use the
          content-negotiation capabilities of HTTP 1.1 and configure the web
          server to handle the content decision-making. How to do this is beyond
          the scope of this NG, which is fortunate for me because it is presently
          also beyond the scope of my knowledge. :-)

          Sorry I've no further suggestions.
          Stephen

          [color=blue]
          >
          >
          > P.S. By the way, for those interested, this is the code for checking the
          > browser language:
          >
          >[...snip...][/color]

          Comment

          • Edo van der Zouwen

            #6
            Re: How to check HTTP_ACCEPT_LAN GUAGE using Javascript?

            Stephen <ssansom@austin .rr.com> wrote in
            news:aNGbb.7109 6$834.15479@twi ster.austin.rr. com:[color=blue]
            >
            > As far as I know, the HTTP request & headers sent with the request are
            > not accessible to javascript. This is probably (my guess) because
            > these are formed immediately before transmission and too late for
            > server-side scripting to make use of.
            >
            > I believe I mentioned before, my prefered solution would be to use the
            > content-negotiation capabilities of HTTP 1.1 and configure the web
            > server to handle the content decision-making. How to do this is beyond
            > the scope of this NG, which is fortunate for me because it is
            > presently also beyond the scope of my knowledge. :-)
            >
            > Sorry I've no further suggestions.
            > Stephen
            >
            >[color=green]
            >>
            >>[/color][/color]


            Thanks for your input.

            Cheers,


            Edo.

            Comment

            • Edo van der Zouwen

              #7
              Re: How to check HTTP_ACCEPT_LAN GUAGE using Javascript?

              I have found the following solution, which is a combination of php and
              Javascript.

              Look at how the variable from php is passed onto Javascript.

              This code is based on the following input for the propertie:


              and the following for passing the variable:





              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
              <html>
              <head>
              <title>Untitl ed Document</title>
              <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
              </head>

              <body>

              <?php

              $preflanguage = $_SERVER['HTTP_ACCEPT_LA NGUAG E'];
              #Check the property


              $lang = substr($preflan guage,0,2);
              #Take the first 2 characters

              if ( $lang == "nl" or $lang == "en" or $lang =="pl")
              {
              $filename = "index_" . $lang . ".html";
              #create the filename to go to later

              }

              else

              {
              $lang = "en";
              $filename = "index_en.html" ;
              #create the filename to go to later

              }

              ?>

              <SCRIPT LANGUAGE=JAVASC RIPT>
              location = ("<?php echo $filename; ?>");
              //Let javascript redirect, using the php variable

              </script>


              </body>
              </html>



              I'm sure it isn't the most beautiful solution, but it works!

              Edo.

              Comment

              • Stephen

                #8
                Re: How to check HTTP_ACCEPT_LAN GUAGE using Javascript?

                Edo van der Zouwen wrote:
                [color=blue]
                > Stephen <ssansom@austin .rr.com> wrote in
                > news:aNGbb.7109 6$834.15479@twi ster.austin.rr. com:
                >[color=green]
                >>As far as I know, the HTTP request & headers sent with the request are
                >>not accessible to javascript. This is probably (my guess) because
                >>these are formed immediately before transmission and too late for
                >>server-side scripting to make use of.[/color][/color]

                I saw your solution, but just to correct my misstatement here: I meant
                "too late for *client*-side scripting to make use of" ...
                S.
                [color=blue][color=green]
                >>
                >>I believe I mentioned before, my prefered solution would be to use the
                >>content-negotiation capabilities of HTTP 1.1 and configure the web
                >>server to handle the content decision-making. How to do this is beyond
                >>the scope of this NG, which is fortunate for me because it is
                >>presently also beyond the scope of my knowledge. :-)
                >>
                >>Sorry I've no further suggestions.
                >>Stephen
                >>
                >>
                >>[color=darkred]
                >>>[/color][/color]
                >
                >
                > Thanks for your input.
                >
                > Cheers,
                >
                >
                > Edo.[/color]

                Comment

                • Stephen

                  #9
                  Re: How to check HTTP_ACCEPT_LAN GUAGE using Javascript?

                  Edo van der Zouwen wrote:
                  [color=blue]
                  > I have found the following solution, which is a combination of php and
                  > Javascript.
                  >
                  > Look at how the variable from php is passed onto Javascript.
                  >
                  > This code is based on the following input for the propertie:
                  > http://www.weijers.net/sitemap.html
                  >
                  > and the following for passing the variable:
                  > http://www.sitepointforums.com/showt...hreadid=106385
                  >
                  >
                  > [...snip the code sample...]
                  >
                  >
                  > I'm sure it isn't the most beautiful solution, but it works!
                  >[/color]

                  A very acceptable solution, here. Good digging.
                  This also prompts me to believe that using server-side includes you
                  could achieve the same thing:

                  <script type="text/javascript">
                  var acceptLang="<!--#echo var="HTTP_ACCEP T_LANGUAGE"-->"
                  // figure out which language by parsing "acceptLang "
                  switch(myLangua ge) {
                  case "nl":
                  ...
                  break;
                  case "en":
                  ...
                  break;
                  case "pl":
                  ...
                  break;
                  default:
                  ...
                  break;
                  }
                  // or something similar
                  </script>

                  Very like your PHP approach.

                  Stephen


                  Comment

                  Working...