 |
Categories / php / MultiLanguage Website |
| By |
| Last updated: November 27, 2005 |
| |
How to Build a Multi-Language Dynamic Website - The Concept |
| |
I am going to present you a nice and easy way to build a multi language dynamic website.
Let's say that your website requires three languages: english, french and espanol.
Please note that this method will work no matter of the languages number you use.
I will use many graphics in this tutorial, so you can understand better the relationships between tables and how this method really works.
Note: DB_DataObject layer will be used to make sql queries. If you are not familiar with it, you should first read
So let's get it started.
I will create a table, in our database, called 'languages' with the following structure:

And I will have the following records:

As you can see in the above picture, I have three records in our database.
The 'defaut_language' field shows what is the default language.
Being a dynamic site, there will be an admin area too.
Admin Area
- Manage languages
In this section, you should be able to insert, edit, delete language, or change the default language.
I am not going to write any code for this section, since it's a simple four fields table, which I'm sure you can handle it.
- Manage content
Your website most likely will have a menu, and I will make this menu multi-language.
The database schema is shown in the next picture:

menu_id and languages_id are foreign keys in the menu_description table.
A new menu must be inserted in all three languages (in this case).
Suppose I have two menus: first page, contact us.
All these menus must be entered in three languages so I'll have:


Now the management of the menu and menu_description tables it's easy to develop having
the necessary tables and relationships between them.
Client Area (Presentation)
- Create language files
There are three languages in our case, so we must create three files, where we will define words or phrases in our languages:
- en.php
- fr.php
- es.php
In each of this file, we need to define constants with words or phrases we will use.
|
- define( 'TEXT_LOGIN', 'Login' );
- define( 'TEXT_CLICK_HERE', 'Click here' );
- define( 'TEXT_LOGIN', 'Ouverture' );
- define( 'TEXT_CLICK_HERE', 'Clic ici' );
- define( 'TEXT_LOGIN', 'Conexion' );
- define( 'TEXT_CLICK_HERE', 'chasque aqui' );
|
|
- Retrieve languages list
Often, languages are listed in the header of your website.We are going to get all the languages from their table.
|
- $languages = DB_DataObject::factory( 'languages' );
- $languages->orderBy( 'default_language DESC, language ASC');
- $languages->find();
- $a_languages = array();
- while( $languages->fetch() )
- {
- $a_languages[] = array( 'lid'=>$languages->languages_id, 'language_short'=>$languages->language_short );
- }
|
|
- Set current language
|
- if( isset( $_GET['lid'] ) && !empty( $_GET['lid'] ) )
- {
- $current_language = DB_DataObject::factory( 'languages' );
- $current_language->get( (int)$_GET['lid'] );
- }
- else
- {
- $current_language = DB_DataObject::factory( 'languages' );
- $current_language->get( 'default_language', '1' );
- }
|
|
- Load language file
And now that use have the language set, we must load the right language file:
|
- include_once( $current_language->language_short . '.php' );
|
|
- Load menu in current language
And finally menu name must be loaded according with the current language.
|
- $menu_object = DB_DataObject::factory( 'menu' );
- $menu_object_description = DB_DataObject::factory( 'menu_description' );
- $menu_object->joinAdd( $menu_object_description );
- $menu_object->orderBy( 'sort_order ASC' );
- $menu_object->whereAdd( 'languages_id=' . $current_language->languages_id );
- $menu_object->find();
|
|
|
|
|
|
 |
|