Help on language detection script
osviweb
Status: New User - Welcome
Joined: 16 Mar 2010
Posts: 2
Reply Quote
I'm trying to use the php language detection script published on this site, even if my php knowledge is not sufficient to do this.
I have a site www.bestholiday.fr which have 5 languages.
In the root I have the pages in english and the others are in folders named fr, it, de, cz .

I'd like to run the php language detection script on top of the head of main english index.php

If the language of the user is english or no other supported language, there is no need to redirect. The user stays on the same page.

If the user language is French, German, Italian or Czech the php should redirect the user to the appropriate folder. Ex. www.bestholiday.fr/fr

In the script provided on this site I can find the following code, where I guess I have to set this redirect.

:: Code ::
// this is just a sample, replace target language and file names with your own.
   elseif ( $feature == 'header' )
   {
      switch ( $user_languages[0][1] )// get default primary language, the first one in array that is
      {
         case 'en':
            $location = 'english.php';
            $found = true;
            break;
         case 'sp':
            $location = 'spanish.php';
            $found = true;
            break;
         default:
            break;
      }
      if ( $found )
      {
         header("Location: $location");
      }
      else// make sure you have a default page to send them to
      {
         header("Location: default.php");
      }
   }
}


Can someone please tell me how I have to modify the code above to get the redirection I need?
In particular I do not understand about what language should be the first and how to set a default language (english)
thanks so much
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
take the primary language abbreviation and add the ones you want to the switch list. Default is default, add the english there, and also have a en case for an explicit redirect.

Any time you deal with headers be prepared to discover that you don't know enough about setting headers, so do these tests on a test page before putting it on your main site.
Back to top
osviweb
Status: New User - Welcome
Joined: 16 Mar 2010
Posts: 2
Reply Quote
Thanks for your kind help.
I know it seems easy if you know it but it's difficoult if you don't know it and you have to guess.
I've tried to write it like you can see below. I've commented the latest defaut redirection because if language is english the user should just stay on current page (script will run on english index.php file).
From a SEO point of view I think this is better than a redirect in case of default language.

Does it seems right? It's quite difficoult to check because my operation system + browser is Italian and I don't know how to check others

:: Code ::

      switch ( $user_languages[0][1] )// get default primary language, the first one in array that is
      {
         case 'fr':
            $location = '/fr/index.php';
            $found = true;
            break;
         case 'it':
            $location = '/it/index.php';
            $found = true;
            break;
         case 'de':
            $location = '/de/index.php';
            $found = true;
            break;
         case 'cs':
            $location = '/cz/index.php';
            $found = true;
            break;   
         default:
            break;
      }
      if ( $found )
      {
         header("Location: $location");
      }
      else// make sure you have a default page to send them to
      {
         //header("Location: default.php");
      }
   }
}


Also can I use the syntax
:: Code ::

case 'fr' or 'fr-be':
$location = 'fr/index.php';


to use the a redirection to French for both users from France and Belgium

thanks so much
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
$user_languages[0][1] is the value of the primary language type.

fr-be is specific, fr is general. $user_languages[0][1] is general, so only one per primary type is required.

fr-be is a subset of fr, thus only fr is required, unless you want to specifically handle a subset.

if no redirection occurs, and you want en to be the default page, no further action is required for index.php.

There's no such thing as case .. or .... in php.

Rather than spend further time speculating, simply create a test page and have php echo out what the data is that you are trying to access, that's much more clear than me trying to explain it, and it takes less time to construct that test case, ie, replace the redirects // comment them out and instead use echos, but again, do not do this on the live site.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours