How to develop multi lingual websites in PHP – PHP

  php

Q(Question):

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.

A(Answer):

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)

A(Answer):

Hey.

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:

locale/
english.php
french.php
set_locale.php
index.php

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

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

<?php
/*
* 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:

<?php
/*
* 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:

<?php
/*
* 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.
?>
<html>
<head>
<title><?php echo $GLOBALS['L']['title']; ?></title>
</head>
<body>
<h1><?php echo $GLOBALS['L']['h1']; ?></h1>
<p><?php echo $GLOBALS['L']['p1']; ?></p>
</body>
</html>

See what I mean?

A(Answer):

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

A(Answer):

thanks for your reply

A(Answer):

thanks for your valuable code

A(Answer):

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>

LEAVE A COMMENT