Notice: Use of undefined constant HTTP_SERVER - assumed 'HTTP_SERVER' in /srv/www/askbee.net/public_html/includes/functions.php on line 3
AskBee.NET | php | MultiLanguage Website | multilanguage dynamic website
AskBee.NET
Google
AskBee - Articles, Tutorials.
   Categories    Resources
   Photo Gallery    Contact

Categories / php / MultiLanguage Website

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

So let's get it started. I will create a table, in our database, called 'languages' with the following structure:

Languages Structure

And I will have the following records:

Languages 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:

Database Relationships

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:

Menu List Records

Menu Description Records

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' );
  1. 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 );
}

  1. 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' );
}
  1. 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
  1. 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();
Advertising
Are you paying more than $170 per month on your credit cards?
Let us help you. Free Debt Relief !
Host your website now
Unlimited Hosting
Cheap Hosting
Bee recommends:
SEO Articles
AskBee Hosting Plans
Affordable hosting
AskBEE Directory
www.codeworkshq.com
www.averagedesign.com
www.4invent.com
Catering
Anunturi online
revelion 2012
anvelope iarna si all season
Cumpara Bitcoin Romania
Taxi Heathrow to London
Privacy Policy | Top Searches | Cheap web hosting | Ringtomes | Shared Hosting Valid HTML 4.01 Transitional Valid CSS!