473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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;
?>
Oct 1 '08 #1
4 2297
Response to edgy <ho*****@hotmail.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.
Oct 1 '08 #2
On Wed, 01 Oct 2008 18:14:30 -0500, -Lost wrote:
Response to edgy <ho*****@hotmail.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.
Oct 2 '08 #3
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!
Oct 6 '08 #4
Response to edgy <ho*****@hotmail.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.
Oct 9 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
5
by: Gustaf Liljegren | last post by:
In IE (at least from version 5), you can change your prefered language, so that for example the Windows Update page appears in another language than the system default. Here's the process: Tools...
70
by: KingIshu | last post by:
Hi All, I am developing an object oriented OS code-named "ikbocs". 1) What are the pros and cons of the following languages interms of Effectiveness on System Programming, Object Orientedness etc...
6
by: Garmt de Vries | last post by:
On some of my English language webpages, I have images of Dutch books. In the alt text for this image, I give the title of the book, for example: <img src="maan.jpg" alt="Cover of De reis naar...
3
by: - HAL9000 | last post by:
Hello, Background: I am familiar with VB.NET, vanilla C++, and Pascal but not Csharp. Please confirm/comment: If I want to get a windows application running quickly (and minimize total...
12
by: koorb | last post by:
Is there any reason why Microsoft is keeping the languages separate or will they eventual replace all the languages with one, just to simplify? And why do people keep talking about switching to...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
19
by: Bryan | last post by:
Hi, I work for a company that is heading towards an FDA approved development process. We have always used C++ in a windows environment, and we have more than 6 years of code, applications and...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.