When creating a website, It is occasionally asked of you to use more than one language - for example English and French.
Unfortunately this presents some logistical problems, as the French for "homepage" is not "homepage" (it is actually Accueil or directly, Foyer biper).
So how do we handle this? Well we need to set up a dictionary / library and get the information from there. First we need to select a language/format for the library. A text based library works just as well, without the bother of editing a database.
There are 3 realistic choices here:
XML
Benefits:
- semi-native & very easy read format for PHP
- easily understood to a coder
Drawbacks:
- not easily understood to A N Other
- takes a lot of effort to edit (compared to next two options)
YAML
Benefits:
- ultra readable for people
- very flexible language, allowing nested definitions
Drawbacks:
- requires a library to read which most PHP installations wont have
- restricts the use of TAB characters, which is a frustrating error to make when writing it
INI
Benefits:
- ultra readable for people
- native read format for PHP
- allows almost any characters
Drawbacks:
- doesn't allow nesting easily although it is possible at the expense of other features.
conclusion on file type[/]
Whilst it is no doubt clear that I am deliberately writing to the conclusion that INI files are the way to go, I hope i have justified to a reasonable extent. INI files are very easy to read for both humans and PHP.
so how does will my files look?
Ok, firstly for reasons of organization it is recommended that you keep your dictionary files in their own Directory, probably "dictionary" or "languages".
Each distinct language will be housed in its own file, for example "english.ini" and "french.ini".
Each phrase will have a "dictionary name" which must be conthe system consistent across all INI files.
So lets set up the two files, with the home page translations in each one:
English.ini
; English dictionary - its good to talk... [menu items] homelinktext = Home aboutlinktext = About
French.ini
;Dictionnaire Francais - c'est bon sur parler [menu items] homelinktext = Siège aboutlinktext = Propos
So you can see here that homelinktext has a value in each file.
I have separated the sections with a usual section separator ( [sectionname] ). This is not entirely necessary here, you could just separate it with commented lines ( ; at the front of it ).
Ok so thats section one, take a breather, get a fresh drink, and scroll onwards for more!
[break]
So how will we access this text, i hear you ask?
well heres a quick code example:
<li> <a href='index.php?page=home'> <?=LIB_Homelinktext;?></a> </li>
Those of you who have even a passing knowledge of PHP will notice that i have used a CONSTANT here. For more information see this page: http://uk3.php.net/define
This code so far obviously does nothing. Lets change that yes? Heres the sequence of events as the page is processed:
* figure out which language to use (either a default or a user-selected setting)
* load the related INI file
* define all necessary constants
That really is it... 3 steps.
figure out which language to use
As i mentioned, it will either be the default setting or a user-selected setting. First we set up the default:
$lib_path = "./library/"; $lib_default_language = "english";
Simple yes? There is no need for a fancy pants bit of coding when a variable will do...
But what if the user has previously selected "french"? Well presumably we would store that choice in a COOKIE. So lets open up the cookie jar.
$lib_selected_language = $_COOKIE['lib_selected_language'];
yes, i know.. long variable names - but it helps for clarity.
So lets make our code do some real work for once:
if( strlen( $lib_selected_language ) > 0 AND file_exists( $lib_path.$lib_selected_language.".ini" ) ) {
$lib_used_language = $lib_selected_language;
} else {
$lib_used_language= $lib_default_language;
}
$library = parse_ini_file( $lib_path.$lib_used_language.".ini" );now hopefully this means that:
* if a cookie exists with a known library, that library will be used
* if a cookie exists without a known library, the default will be used
* if a cookie DOESN'T exist, the default is used
So now we have our library selected, and set out in $library, as a single-dim array.
So final step now, to define all constants
a simple foreach will do here - there really is no reason not to:
foreach( $library as $lib_name => $lib_text ) {
define( "LIB_".$lib_name, $lib_text, true );
}ta daaa....
Final step now... the properly final step.. not just one i put there for S&G's
So we have a library set up, and if the user were to select french then things would display in french. But the can't actually select french. We need to set a cookie, at the appropriate moment.
First, lets see what a link might look like: "index.php?lang=french"
nice and simple, nothing too complex...
so in the code view, right at the top before anything else in the script:
if ( isset($_GET['lang']) ) {
setcookie( "lib_selected_language", $_GET['lang'], time()+3600*24*365 );
$lib_selected_language = $_GET['lang'];
}nice and easy... that cookie lasts for a year by the way.
[b]so here is the final PHP code:
<?php
if( isset($_GET['lang']) ) {
setcookie( "lib_selected_language", $_GET['lang'], time()+3600*24*365 );
$lib_selected_language = $_GET['lang'];
}
$lib_path = "./library/";
$lib_default_language = "english";
if( strlen( $lib_selected_language ) > 0 AND file_exists( $lib_path.$lib_selected_language.".ini" ) ) {
$lib_used_language = $lib_selected_language;
} else {
$lib_used_language= $lib_default_language;
}
$library = parse_ini_file( $lib_path.$lib_used_language.".ini" );
foreach( $library as $lib_name => $lib_text ) {
define( "LIB_".$lib_name, $lib_text, true );
}
//rest of code below hereand to recap, heres how to use a bit of library text
<?=LIB_textname;?>
and a link to change the language:
<a href='?lang=french'>Francais</a>
Hope this helps someone eventually
Help



















