How to Add Multi-language Support to a PHP Website
Posted on July 23, 2009, 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 ------------------ */ $lang = array(); $lang['PAGE_TITLE'] = 'My website page title'; $lang['HEADER_TITLE'] = 'My website header title'; $lang['SITE_NAME'] = 'My Website'; $lang['SLOGAN'] = 'My slogan here'; $lang['HEADING'] = 'Heading'; // Menu $lang['MENU_HOME'] = 'Home'; $lang['MENU_ABOUT_US'] = 'About Us'; $lang['MENU_OUR_PRODUCTS'] = 'Our products'; $lang['MENU_CONTACT_US'] = 'Contact Us'; $lang['MENU_ADVERTISE'] = 'Advertise'; $lang['MENU_SITE_MAP'] = 'Site Map'; ?>
lang.es.php
<?php /* ----------------- Language: Spanish ----------------- */ $lang = array(); $lang['PAGE_TITLE'] = 'Título de la página de mi sitio web'; $lang['HEADER_TITLE'] = 'Mi sitio web de la cabecera título'; $lang['SITE_NAME'] = 'Mi Sitio Web'; $lang['SLOGAN'] = 'Mi lema aquí'; $lang['HEADING'] = 'Título'; // Menu $lang['MENU_HOME'] = 'Inicio'; $lang['MENU_ABOUT_US'] = 'Sobre Nosotros'; $lang['MENU_OUR_PRODUCTS'] = 'Nuestros productos'; $lang['MENU_CONTACT_US'] = 'Contáctenos'; $lang['MENU_ADVERTISE'] = 'Publicidad'; $lang['MENU_SITE_MAP'] = 'Mapa del Sitio'; ?>
lang.de.php
<?php /* ----------------- Language: German ----------------- */ $lang = array(); $lang['PAGE_TITLE'] = 'Meine Webseite Titel'; $lang['HEADER_TITLE'] = 'Meine Website-Header Titel'; $lang['SITE_NAME'] = 'Meine Website'; $lang['SLOGAN'] = 'Mein Slogan hier'; $lang['HEADING'] = 'Position'; // Menu $lang['MENU_HOME'] = 'Heim'; $lang['MENU_ABOUT_US'] = 'Über uns'; $lang['MENU_OUR_PRODUCTS'] = 'Unsere Produkte'; $lang['MENU_CONTACT_US'] = 'Kontaktieren Sie uns'; $lang['MENU_ADVERTISE'] = 'Werben'; $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.
- 83 comments
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 11 comments
-
Submit your Website to the Most Popular CSS galleries: MeeCSSat July 26, 2009
-
Introduction to HTML5: The New Hypertext Markup Language Formatat September 22, 2009
-
Discover How to Make a Website: 50 Mistakes You Should Avoidat July 13, 2010 with 10 comments

83 Replies to "How to Add Multi-language Support to a PHP Website"
April 10, 2011 at 6:02 PM
Thanks for the great tutorials although its very basic. But good for beginners.
April 27, 2011 at 7:18 PM
nice Tuts keep up going…
May 11, 2011 at 4:05 AM
I have used this code with 4 lang website, and having problem:
You are on different from default ENG Language page and want to go to another page DE for example,
going on DE page resets this page back to default ENG language and to see it again on DE or any other lang u have to press again.
ANYONE KNOWS THE SOLUTION HELP!
August 11, 2011 at 11:57 PM
I’m having the same issue as Makcum. Anyone find a solution?
August 16, 2011 at 9:41 PM
Hello, I’m having this problem also.
A have a small site with pages like index.php, contact.php, product.php. In each page a include menu.php and in menu.php a include common.php.
I think is a problem with session_start() but i dont know how to fix it.
August 17, 2011 at 9:06 PM
I figured it out.The cookie couldn’t be written because if was set after html tags.
November 21, 2011 at 1:33 AM
I try to add to, for example, lang.de.php something like this:
$lang_url = '?lang=de';
The same I use in other languages and than for links in nav I use this:
href="ponuka.php<?php echo $lang_url; ?>"
I know that isn’t professional solution but it works for me :)
June 5, 2011 at 7:49 PM
If i use this script, will google spider get all the other languages content ? Thanks
June 7, 2011 at 11:13 AM
Thank you for this tutorial / article.
I am learning PHP and this gave me a mighty help to what I do.
September 14, 2011 at 6:30 AM
perfect, perfect. thank you very much. you have been very helpfull for me. very good tutorial.
thanks
regards
September 26, 2011 at 8:10 AM
This tutorial was great, but sort of skipped over the entire issue of encoding + how to integrate languages such as Russian / Chinese….
October 5, 2011 at 2:02 AM
great job!!! Just What I was looking for!!! Amazing
December 3, 2011 at 3:30 AM
Thanks a lot for this great tutorial!!
2 little questions:
- is there a way to change the code in a way that when changing the language, the user stays on the same page instead of jumping to the index page?
- how can special caracters be displayed they look kind of funny (even in the example)
thanks a bunch
December 11, 2011 at 2:03 AM
i found a way:
1) just delete the index.php from the links
2) the language files have to be saved as utf-8 coded
December 8, 2011 at 11:05 PM
That
include_once 'languages/'.$lang_file;
always generates a break line on top of HTML. How can i prevent it?