Author: Jason Eyermann
For a recent site that I created I wanted to add a language selector. I had look around the internet to try and find a suitable solution, but i couldn't find any, so I developed my own. My finished solution seems to work perfectly for the site. if you want to add such a feature you can view my solution below.
This feature was created using php.
Here is the code for the buttons:-h
-form tag here
-an image input tag with a value of Italian linked to an Italian flag and a name tag of lang
-an image input tag with a value of German linked to an Italian flag and a tag name of lang
-an image input tag with a value of England linked to an England flag and a tag name of lang
-input type="hidden" name="submitted" value="true"
So I used flags for each of the buttons to represent the different countries. You will also need to add a value as i have done with the name of the country's language.
Next you will need to add the following code the top of each of your pages:-
if (isset($_POST['submitted'])) {
setcookie('language', $_POST['lang'], time()+604800);
header("Location: index.php");
}
if (isset($_COOKIE['language'])) {
$cookieText = $_COOKIE['language'];
} else {
$cookieText = "English";
}
include("text/" . $cookieText . ".php");
If this is the first time on the site then this code adds a cookie to the user's computer. It defaults the language to english. If one of the buttons is clicked then $_POST['submitted'] becomes true and then the users cookie gets updated with new value from the button. The language files come from a folder on the system called text. Inside here are several different php files. one for each language and each one is picked out depending on what the variable $cookieText is changed to.
In the 'text' folder pages, the following code is applied - As a web des……. There are many…….'); etc.......
define('HOME_PAGE_MAIN', '
define('FREE_FRENCH_SITES_TITLE', 'Learn french the free way');
define('FREE_FRENCH_SITES', '
define('ADD_SITE_TITLE', 'Submit a suggestion');
And then you place the echo HOME_PAGE_MAIN . " where you want to add the text.
"
You can see this action on my Connect French site. Even though as the time of writing i've only added two languages it's now very easy to add more.