PHP Beginners: How to Add Multi-language Support to a Website
Posted on July 23, 2009, Filled under PHP,
Bookmark it
Are you interested in having a multilingual website? This is a tutorial that shows you how you can do that in PHP.

The first thing we need to do is to create a couple of files that will contain the text for each of the languages that will be supported by the website. For demo purpose I have chosen English, Spanish and German. Make a directory named “directory”. In this directory create 3 files: lang.de.php, lang.en.php, and lang.es.php. In our main file (index.php) we will include a file (common.php) containing a piece of code that gets the requested language. Here’s the content of those 3 language files:
lang.en.php
<?php
/*
------------------
Language: English
------------------
*/
define('LANG_PAGE_TITLE', 'My website page title');
define('LANG_HEADER_TITLE', 'My website header title');
define('LANG_SITE_NAME', 'My Website');
define('LANG_SLOGAN', 'My slogan here');
define('LANG_HEADING', 'Heading');
// Menu
define('LANG_MENU_HOME', 'Home');
define('LANG_MENU_ABOUT_US', 'About Us');
define('LANG_MENU_OUR_PRODUCTS', 'Our products');
define('LANG_MENU_CONTACT_US', 'Contact Us');
define('LANG_MENU_ADVERTISE', 'Advertise');
define('LANG_MENU_SITE_MAP', 'Site Map');
?>
lang.es.php
<?php
/*
------------------
Language: Spanish
------------------
*/
define('LANG_PAGE_TITLE', 'TÃtulo de la página de mi sitio web');
define('LANG_HEADER_TITLE', 'Mi sitio web de la cabecera tÃtulo');
define('LANG_SITENAME', 'Mi Sitio Web');
define('LANG_SLOGAN', ' Mi lema aquÃ');
define('LANG_HEADING', 'TÃtulo');
// Menu
define('LANG_MENU_HOME', 'Inicio');
define('LANG_MENU_ABOUT_US', 'Sobre Nosotros');
define('LANG_MENU_OUR_PRODUCTS', 'Nuestros productos');
define('LANG_MENU_CONTACT_US', 'Contáctenos');
define('LANG_MENU_ADVERTISE', 'Publicidad');
define('LANG_MENU_SITE_MAP', 'Mapa del Sitio');
?>
lang.de.php
<?php
/*
-----------------
Language: German
-----------------
*/
define('LANG_PAGE_TITLE', 'Meine Webseite Titel');
define('LANG_HEADER_TITLE', 'Meine Website-Header Titel');
define('LANG_SITE_NAME', 'Meine Website');
define('LANG_SLOGAN', 'Mein Slogan hier');
define('LANG_HEADING', 'Position');
// Menu
define('LANG_MENU_HOME', 'Heim');
define('LANG_MENU_ABOUT_US', 'Über uns');
define('LANG_MENU_OUR_PRODUCTS', 'Unsere Produkte');
define('LANG_MENU_CONTACT_US', 'Kontaktieren Sie uns');
define('LANG_MENU_ADVERTISE', 'Werben');
define('LANG_MENU_SITE_MAP', 'Site Karte');
?>
As you can notice, some constants are created using the define() function. In every file the defined constants have the same name, bu the values is different. We will output the values of the constants inside the index.php file. Therefore we will see different text every time we will call other language file.
Determine the right language
Let’s analyze the common.php file:
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'de':
$lang_file = 'lang.de.php';
break;
case 'es':
$lang_file = 'lang.es.php';
break;
default:
$lang_file = 'lang.en.php';
}
include_once 'languages/'.$lang_file;
?>
After we determine the value of $lang, we use switch() to compare its value with some different values, and execute a different piece of code depending on which value it equals to. After the value of the $lang_file is determined, the script will include the necessary language file. As you can see I have used sessions to register the value of $lang. This way users can navigate through the whole site and see the content in the chosen language (lang=[language here] does not need to be passed in every URL). Additionally, I have used cookies to store the selected language in users computer for 30 days. When the visitor will come back he will see the site in the language that he previously selected.
How if the website’s language requested?
In this demo I have chosen to use some image flags, each image having a link to index.php?lang=[LANG HERE]. So, to see the site in german we will use the German image flag which links to index.php?lang=de.
Lastly, the constants values should be outputted in the page. Examples:
for document title
<title><?php echo LANG_PAGE_TITLE; ?></title>
for header menu
<ul>
<li><a href="/"><?php echo LANG_MENU_HOME; ?></a></li>
<li><a href="about_us"><?php echo LANG_MENU_ABOUT_US; ?></a></li>
<li><a href="our_products"><?php echo LANG_MENU_OUR_PRODUCTS; ?></a></li>
<li><a href="contact_us"><?php echo LANG_MENU_CONTACT_US; ?></a></li>
<li><a href="advertise"><?php echo LANG_MENU_ADVERTISE; ?></a></li>
<li><a href="sitemap"><?php echo LANG_MENU_SITE_MAP; ?></a></li>
</ul>


Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- July 23, 2009
- article by Gabriel C.
- 14 comments
Sponsors
Related Posts
-
Enhance Website Navigation Menus: jQuery Multi Level Popup Menuat December 30, 2009
-
How to Choose a Color Palette When Designing a Brand New Websiteat April 29, 2009 with 3 comments
-
Submit your Website to the Most Popular CSS galleries: MeeCSSat July 26, 2009
-
Bumpbox – An Awesome Lightbox Clone with Support for PDF Filesat July 25, 2009
-
A PHP Contact Form with JavaScript Real Time Validationat April 30, 2009 with 17 comments

14 Replies to "PHP Beginners: How to Add Multi-language Support to a Website"
July 25, 2009 at 5:44 PM
This may works out for a couple of pages, but not for much, ( except the menu and static stuff ) as the page title is vital for SEO, and every page having the same title will really be a problem.
Furthermore you’d need to write all your content in 3 languages and associate that with the pages that should display that content later on. Better is to have a nice – maybe written by your own – CMS that really does nothing else than “spit” out requested content in the requested language on the appropriate page.
We use a small CMS for our news section, which supports 3 languages. Using such a thing together with your approach then makes sense. It displays contents on our different language websites accordingly.
August 4, 2009 at 1:30 AM
I wonder could that be changed so when changing the language, the hrefs of the header change aswell as the title of the link? that way i could simply just add
“href=”index.php?page=etusivu&lang=th”
and with include, bring in different texts to the body area, according to the language:
include ($lang.”/etusivu.html”);
That way, I dont need to define different words for different languages, since all words will be in separate .php or .html file.
August 4, 2009 at 3:55 AM
It’s good to have a certain order of the files and keep each language words in separate files. I have seen many professional applications built this way and I really recommend this practice. I don’t like the idea of changing the links inside the page. I believe it’s better to use specific links or (flag) images to set the desired language.
August 4, 2009 at 9:19 PM
@Gabriel,
I aggree on the point: of using specific links or imgs to set the language, but the href that i want to change is the menu’s links “home, links, contact, etc”
could that be possible with your script? Im a newbie sorta with php.
Thanks
August 11, 2009 at 10:55 AM
[...] This post was Twitted by planetphp [...]
August 11, 2009 at 10:57 AM
Hi,
Using constants is just the simplest way of localizing your content. Personally I got realy fond of the way Wordpress has incorporated localisation. Somewhat simplified but everywhere you want to use a localized string you just wrap it in __() or _e():
to echo a localized string. The function _e($str) knows if a translated version of the string is present in your translation set. If so the translation is outputted, otherwise the unaltered text. This makes sure you never get ugly constant warnings in your interface.
Using any of the dozens of ways you can easily hook your __() and _e() implementation to your prefered dictionairy method of choice. Be it ini files, or xml files, or when you are up to it going with gettext (.po/.me and .pot files, with POEdit).
This post is okee for just a simple site, but when you get into larger sites, you need to get a more sound and less error prone approach.
My 2cts
Arnoud
September 17, 2009 at 11:27 AM
Thanks
man.
.-= simi.ro’s last blog ..U Tv =-.
November 23, 2009 at 8:48 PM
Hi buddy this is very simple way to convert language……..
December 9, 2009 at 4:07 AM
Great tutorial. It’s what the feature I am looking for for my dating website.
Can you tell us a bit further how do replace the flag icon with the dropdown box option.
December 23, 2009 at 12:16 AM
Hi
Is it actually possible instead of redirecting each language link to the index
Instead of <a href="?lang=en" rel="nofollow">
use
lt;a href="<?php $_SERVER['PHP_SELF']?>" rel="nofollow">?lang=nl">
December 26, 2009 at 10:37 AM
Hi Peter i think a better explaination would be better coz the second link is sensless
February 1, 2010 at 1:22 PM
How to modify for use with mysql database?
February 2, 2010 at 12:31 PM
I do like this but I prefer to do this a different way and I think it keeps files more organized but I’m not sure how to do it. Perhaps someone can tell me.
I want to have each language files in their own folder (en/ and fr/) for english and french for example. I want to be able to click on the image/text and have the page I’m currently on change to the same page but in a different language. So essentially, we are just changing the folder name from en to fr.
Can anyone please tell me how to do this? A sample site here but it’s in ASP code (http://www.contacservices.com/)
Thank you in advance.
February 7, 2010 at 9:19 PM
Very usefull tutorial!
I have a doubt. How can I add to this code a php function to identify the default browser language?