Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 1st, 2008, 09:25 PM
edgy
Guest
 
Posts: n/a
Default 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_GET_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_LANGUAGE"])) {//check second to see
if they've been nice and set the language
$langs=explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);//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_GET_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_LANGUAGE"])) {//check second to see
if they've been nice and set the language
$langs=explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);//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;
?>
  #2  
Old October 2nd, 2008, 12:15 AM
-Lost
Guest
 
Posts: n/a
Default Re: Language Switching

Response to edgy <hotmail@hotmail.com>:
Quote:
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.
  #3  
Old October 2nd, 2008, 08:35 PM
edgy
Guest
 
Posts: n/a
Default Re: Language Switching

On Wed, 01 Oct 2008 18:14:30 -0500, -Lost wrote:
Quote:
Response to edgy <hotmail@hotmail.com>:
>
Quote:
>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.
  #4  
Old October 6th, 2008, 02:35 AM
edgy
Guest
 
Posts: n/a
Default Re: Language Switching

On Wed, 01 Oct 2008 20:18:55 +0000, edgy wrote:
Quote:
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!
  #5  
Old October 9th, 2008, 11:25 PM
-Lost
Guest
 
Posts: n/a
Default Re: Language Switching

Response to edgy <hotmail@hotmail.com>:
Quote:
Quote:
>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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles