Easy Language Selection

Categories: code snippet
Comments: No Comments
Published on: December 20, 2011

You want your visitors to be able to switch to a second language and remember the selection when the visitor returns. Three straightforward steps will give you this functionality using a cookie.

Step 1 On your site, create a top level directory called language.

In this directory you will need to create a file with a php extension for each language you want to use. Name the files using ISO 639-1 two-letter country codes. For example en.php for English, de.php for German, fr.php for French, and so on.

In these files put the replacement variables you need. Obviously the variable names must be the same in each language file. I use a name that reflects where I use it, for example $contact for the contact page variables. Of course, you can use whatever you like, as long as there are no duplicates. The following examples show two files, the first for the English language, the second the German equivalent.

en.php

<?php
//Contact variables
$contact['name']='Your Name';
$contact['address']='Your eMail Address';
$contact['subject']='Subject';
$contact['verify'] ='Verify';
$contact['message']='Message';
//Page variables
$page['webdev']='Web Development';
$page['tools']='Tools';
?>

de.php

<?php
//Contact variables
$contact['name']='Ihr Name';
$contact['address']='Ihre E-Mail';
$contact['subject']='Betreff';
$contact['verify']='Spamschutz';
$contact['message']='Ihre Nachricht';
//Page variables
$page['webdev']='Web Entwicklung';
$page['tools']='Werkzeuge';
?>

Step 2 Create a file called langfile.php in the home directory. This file contains the code to save a cookie that remembers the language the visitor selected.

langfile.php

<?php
setcookie('lang', $_GET['lang'], time()+31536000, "/");
header("Location:".$_SERVER['HTTP_REFERER']);
?>

Step 3 This is all about the main page, where you want the language change to take place. There are three things you need to do here. Firstly, put in the code which calls the cookie file, then sets and includes the selected language file. Secondly put in the code to allow the visitor to select the language. Thirdly, put in the replacement variables themselves.

First of all put this at the very beginning of your Index page.

index.php

<?php
$lang = "en";
if ( isset($_COOKIE['lang']) ) $lang = $_COOKIE['lang'];
$php_self = $_SERVER['PHP_SELF'];
$dir = 'language';
include_once $dir.'/'.$lang.'.php';
?>

Next, add this code where you want to place the language selection for the visitor. You could use a flag image instead of the text used here.

index.php

<?php
if ( $lang == 'en' ) echo '<a href="langfile.php?lang=de" title="Deutsch">DE</a>';
else echo '<a href="langfile.php?lang=en" title="English">EN</a>'; ?>

Finally, add the variables in place of the text you want changed.

index.php

<?php echo $contact['name']; ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Welcome , today is Wednesday, February 22, 2012