How to develop multi lingual websites in PHP – PHP



HI all,
I want to develp a multi lingual website in PHP.(english and french)
I am a novice developer in this aspect.
Pls suggest us what are the methods used in general.
Do we need any extra s/w.


basicly you do it like a monolingual website, where you can decide, whether to put the english or french content in (e.g. by using templates)



This can be as simple as:Define an array of values in different languages, preferably each language having it’s own array defined in a separate file. Create a script that figures out which language to use (by, for example, reading session, cookie or get/post values) and includes the proper file. Include said script into your pages, using the array defined in the language files to echo the text in those pages.
Consider, if you had these files:


You could define each of the "locale" files as such:

* File: locale/english.php
$locale = array(
'title' => 'Title in English',
'h1' => 'The following in in English:',
'p1' => 'This is a sample text, in English'

* File: locale/french.php
$locale = array(
'title' => 'Titre en français',
'h1' => 'Le texte suivant en en français:',
'p1' => 'Il s\'agit d\'un échantillon de texte, en français.'

Then, in your "set_locale.php" file, you could select a language based on some input variable, like say; a variable named "lang" from the GET array, and include one of those files based on that value:

* File: set_locale.php
// Get the language from the query string, or set a default.
($language = @$_GET['lang']) or $language = 'english';
// Set up a list of possible values, and make sure the
// selected language is valid.
$allowed_locales = array('english', 'french');
if(!in_array($language, $allowed_locales)) {
$language = 'english'; // Set default if it is invalid.
// Inlclude the selected language
include "locale/{$language}.php";
// Make it global, so it is accessible everywhere in the code.
$GLOBALS['L'] = $locale;

Then, in your pages (the index.php page, in this example), include the "set_locale.php" script and use the $GLOBALS[‘L’] array to set language specific phrases in the HTML:

* File: index.php
// Include the "set_locale" script to fetch the locale
// that should be used.
include_once "set_locale.php"
// Print the HTML, using the selected locale.
<title><?php echo $GLOBALS['L']['title']; ?></title>
<h1><?php echo $GLOBALS['L']['h1']; ?></h1>
<p><?php echo $GLOBALS['L']['p1']; ?></p>

See what I mean?


Or you could just tell your users to learn how to use Google Translate. 😉


thanks for your reply


thanks for your valuable code


Given the code, how would you go about coding an <a></a> tag with alternating classes?

For example when the home page is selected, the home link looks:
<a href="index.php?lang=english" class="selected">Home</a>

but if I’m on the contact us page for example, I want the home link to be (without the class="selected"):
<a href="index.php?lang=english">Home</a>