Language Switching

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • edgy

    Language Switching

    Hello,

    I am a beginner with PHP, and I have made a language switcher on a site
    that I am looking for your feedback on.

    First of all, the page is http://www.mankar.ca
    My question regarding usage is - well, does it work? Can you continue
    browsing in your selected language? I am especially interested in people
    using an IE browser with a locale setting that is not english or french.

    I'll post the code below as well - I'm the first to admit it isn't
    pretty, and I'll try to explain the result I am trying to achieve:

    1)if they are already on my site and have chosen a language, use that
    language.

    2) barring that, try to serve the page in the language of their locale.

    3) If I don't have their language, serve them English.

    4) Regardless of language, they would be surfing the same web address.

    So first I had only this:


    if (isset($HTTP_GE T_VARS['lang'])) {//check to see if they came from a
    page where they set the language - like if we are switching languages on
    the same page
    $lang=$HTTP_GET _VARS[lang];
    }
    elseif(isset($_ SESSION['lang'])){ //see if they already chose a language
    $lang = $_SESSION['lang'];
    }
    elseif (isset($_SERVER["HTTP_ACCEPT_LA NGUAGE"])) {//check second to see
    if they've been nice and set the language
    $langs=explode( ",",$_SERVE R["HTTP_ACCEPT_LA NGUAGE"]);//grab all
    the languages


    foreach ($langs as $value) {//start going through each one

    //select only the first two letters
    $choice=substr( $value,0,2);

    //redirect to the different language page
    //based on their first chosen language
    switch ($choice) {
    case "fr":
    $lang = 'fr';
    case "en":
    $lang = 'en';
    }
    }
    }
    else{
    $lang = 'en';
    }

    Which seemed to work, but had the problem that people with IE with a
    different locale couldn't use the site (this code triggers a number of
    includes), so I added this to the beginning:

    if ($lang == 'fr') {
    $lang = 'fr';
    }
    else{
    $lang = 'en';
    }

    Which seems strange, but perhaps it works? It seems to work for me with
    Firefox in various languages, as well as IE. Does anyone have any
    suggestions to improve this code?

    Here it is completely:

    <?php
    session_start() ;
    if ($lang == 'fr') {
    $lang = 'fr';
    }
    else{
    $lang = 'en';
    }
    if (isset($HTTP_GE T_VARS['lang'])) {//check to see if they came from a
    page where they set the language - like if we are switching languages on
    the same page
    $lang=$HTTP_GET _VARS[lang];
    }
    elseif(isset($_ SESSION['lang'])){ //see if they already chose a language
    $lang = $_SESSION['lang'];
    }
    elseif (isset($_SERVER["HTTP_ACCEPT_LA NGUAGE"])) {//check second to see
    if they've been nice and set the language
    $langs=explode( ",",$_SERVE R["HTTP_ACCEPT_LA NGUAGE"]);//grab all
    the languages


    foreach ($langs as $value) {//start going through each one

    //select only the first two letters
    $choice=substr( $value,0,2);

    //redirect to the different language page
    //based on their first chosen language
    switch ($choice) {
    case "fr":
    $lang = 'fr';
    case "en":
    $lang = 'en';
    }
    }
    }
    else{
    $lang = 'en';
    }
    $_SESSION['lang'] = $lang;
    ?>
  • -Lost

    #2
    Re: Language Switching

    Response to edgy <hotmail@hotmai l.com>:
    I am a beginner with PHP, and I have made a language switcher on
    a site that I am looking for your feedback on.
    >
    First of all, the page is http://www.mankar.ca
    My question regarding usage is - well, does it work?
    <snip>

    This is where test-driven development comes in handy. It is also
    most beneficial to a beginner as you must completely understand the
    problem before deriving a solution.

    SimpleTest [URL:http://simpletest.org/] is a simple unit testing
    framework.

    This perhaps not so obviously removes the need to rely on other's
    opinions of the application immediately as I'm sure you'll receive
    more information than you currently need despite its benefit.

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.

    Comment

    • edgy

      #3
      Re: Language Switching

      On Wed, 01 Oct 2008 18:14:30 -0500, -Lost wrote:
      Response to edgy <hotmail@hotmai l.com>:
      >
      >I am a beginner with PHP, and I have made a language switcher on a site
      >that I am looking for your feedback on.
      >>
      >First of all, the page is http://www.mankar.ca My question regarding
      >usage is - well, does it work?
      >
      <snip>
      >
      This is where test-driven development comes in handy. It is also most
      beneficial to a beginner as you must completely understand the problem
      before deriving a solution.
      >
      SimpleTest [URL:http://simpletest.org/] is a simple unit testing
      framework.
      >
      This perhaps not so obviously removes the need to rely on other's
      opinions of the application immediately as I'm sure you'll receive more
      information than you currently need despite its benefit.
      thanks for the link - I will try to bend my mind around it and use this.

      But here's the strange thing - and I know that PHP always works exactly
      the same - depending on the browser it worked or not. Now I guess that
      means that my code isn't perfect, and I just got lucky that it worked on
      some, so hopefully this simpletest will help.

      Thanks.

      Comment

      • edgy

        #4
        Re: Language Switching

        On Wed, 01 Oct 2008 20:18:55 +0000, edgy wrote:
        Hello,
        >
        I am a beginner with PHP, and I have made a language switcher on a site
        that I am looking for your feedback on.
        >
        First of all, the page is http://www.mankar.ca My question regarding
        usage is - well, does it work? Can you continue browsing in your
        selected language? I am especially interested in people using an IE
        browser with a locale setting that is not english or french.
        >
        <snip>
        Well, I can see that a lot of people from this group visited the site,
        but it does leave me wondering why you wouldn't take a second to let me
        know if it works for you or not?

        Anyways, I would still appreciate it very much!

        Comment

        • -Lost

          #5
          Re: Language Switching

          Response to edgy <hotmail@hotmai l.com>:
          >I am a beginner with PHP, and I have made a language switcher
          >on a site that I am looking for your feedback on.
          >>
          >First of all, the page is http://www.mankar.ca My question
          >regarding usage is - well, does it work? Can you continue
          >browsing in your selected language? I am especially interested
          >in people using an IE browser with a locale setting that is not
          >english or french.
          >>
          <snip>
          Well, I can see that a lot of people from this group visited the
          site, but it does leave me wondering why you wouldn't take a
          second to let me know if it works for you or not?
          >
          Anyways, I would still appreciate it very much!
          I haven't visited it myself, but it may be a sign that it looks and
          behaves flawlessly -- therefore nothing for anyone to pick apart.

          Conversely it could be so horribly put together that no one finds
          it worth their time to comment (fix) everything for you.

          I'd set up those unit tests and let your users, clients, and peers
          be your beta review and critique.

          --
          -Lost
          Remove the extra words to reply by e-mail. Don't e-mail me. I am
          kidding. No I am not.

          Comment

          Working...