View Single Post
  #12 (permalink)  
Old 08-06-2007, 03:08 AM
rrrajesh84in rrrajesh84in is offline
D-Web Master
 
Join Date: Mar 2007
Posts: 399
rrrajesh84in is on a distinguished road
Default Re: Multi Language Website

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 DB_DataObject section.

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

1. 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.
2. 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)

1. 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.
__________________________________________________ ________________
/in en.php file
define( 'TEXT_LOGIN', 'Login' );
define( 'TEXT_CLICK_HERE', 'Click here' );

//in fr.php file
define( 'TEXT_LOGIN', 'Ouverture' );
define( 'TEXT_CLICK_HERE', 'Clic ici' );

//in es.php file
define( 'TEXT_LOGIN', 'Conexion' );
define( 'TEXT_CLICK_HERE', 'chasque aqui' );
__________________________________________________ ________________

2. Retrieve languages list

Often, languages are listed in the header of your website.We are going to get all the languages from their table.

__________________________________________________ ________________
//create language object and get all languages
$languages = DB_DataObject::factory( 'languages' );
$languages->orderBy( 'default_language DESC, language ASC');
$languages->find();

//create a_languages array, which you will use to create language links.
$a_languages = array();
while( $languages->fetch() )
{
$a_languages[] = array( 'lid'=>$languages->languages_id, 'language_short'=>$languages->language_short );
}
__________________________________________________ ________________


3. Set current language

__________________________________________________ ________________
//Language requested, set current language
if( isset( $_GET['lid'] ) && !empty( $_GET['lid'] ) )
{
$current_language = DB_DataObject::factory( 'languages' );
$current_language->get( (int)$_GET['lid'] );
}
//If no language is requested, load default language
else
{
$current_language = DB_DataObject::factory( 'languages' );
$current_language->get( 'default_language', '1' );
}
__________________________________________________ ________________

4. Load language file

And now that use have the language set, we must load the right language file:

__________________________________________________ ________________
//Load language file
include_once( $current_language->language_short . '.php' );
//If languages_id = 1 then constants defined in en.php will be loaded
__________________________________________________ ________________

5. Load menu in current language

And finally menu name must be loaded according with the current language.

__________________________________________________ ________________
//create menu object, menu_description object
$menu_object = DB_DataObject::factory( 'menu' );
$menu_object_description = DB_DataObject::factory( 'menu_description' );

//join them
$menu_object->joinAdd( $menu_object_description );
$menu_object->orderBy( 'sort_order ASC' );
$menu_object->whereAdd( 'languages_id=' . $current_language->languages_id );
$menu_object->find();
__________________________________________________ ________________
__________________
.....................................
''''''
Rajesh''''''
Ants. . . . . . Like me
Reply With Quote