Before you start:Things to know before you start:
- You need to know the basic functions of php
- You need to know how sessions work
- You need to know how to use MySQL databases
I hope you'll like this tutorial! It's about how to make a Multi Language System. You may think this is very hard to do, but when you know the tricks everything is much simpler then you thought. The only thing you will need to do is practice a lot.
Now, the first thing to do is to consider what we want to reach. Or what we want to do with it. For this tutorial I will going to use it for a basic website. I guess that almost all of you guys will have that. CMS Systems and forums aren't much different, but take a lot of time.
First step (creating a database):In this first step we are going to make a MySQL database. It will be one table. I always make it in a excel document, to make sure that I doesn't have to start all over again when I take a break.

The "language id" must be unique and descending. For the "language path" you can use a TEXT or VARCHAR. Remember that you use two slashes. You may want to use only the name, like "english". You can even use the id as language path. But I don't think that will be functional if you have a database with more then 5 languages.
Second step (define function):In this system we are going to use a very easy function of php. It's the define() function of php.
Its very easy to explain. First an example:
CODE
<?php
define(NAME, 'text goes here');
?>
This is it. On the left side you'll have the name of the text (notice that this is head case only!) and on the right side you have the full text that you place in that name. Just two single quotes to make it W3Schools standard.
Just try this one yourself. You can simply echo this text like this:
CODE
<?php
echo NAME;
?>
This will be enough to use this function. You don't need to use a $ like in vars.
Third step (including the languages):At this moment I will try to explain how to create the file construction, how you will going to include them into your website and how you can make a session that will save the user language.
With the define function (step 2) we are going to create the language files. It may be difficult, but still it's fun to try.
We are going to make the English language as the standard language. So what we do is making the following map structure:

The main file is going to be in
http://www.my-domain.com/includes/languages and the sub files in
http://www.my-domain.com/includes/languages/sub/.
We are going to create a php file named "main.lang.php" and we put this into the "Main File". In this document we are creating all the main things that will be needed in every webpage you are going to create. Here is a example:
CODE
<?php
// title of my website
define(TITLE, 'Phryxus Webdesign');
// address of my website
define(COPYRIGHT, '© Copyright 2004 - Phryxus Webdesign');
?>
When you are ready with this you can include this page in you website, just like below:
CODE
<?php
include "./includes/languages/english/main.lang.php";
?>
You may now see what the reason of the database is. But first we are going to move on with the tutorial.
Now the sub names. In the sub files you will place all the texts that are in every webpage different. Just make sure that you give them a name that is easy to use. You can include this one easy under the main file. This is the example:
CODE
<?php
include "./includes/languages/english/main.lang.php";
include "./includes/languages/english/sub/name.lang.php";
?>
In this tutorial you can simply rename the name.lang.php file every time you start a new website. So the contact page would look like this:
CODE
<?php
include "./includes/languages/english/main.lang.php";
include "./includes/languages/english/sub/contact.lang.php";
?>
<HTML>
<HEAD>
<TITLE><?php echo TITLE; ?></TITLE>
Etc.
And the Home page would look like this:
CODE
<?php
include "./includes/languages/english/main.lang.php";
include "./includes/languages/english/sub/home.lang.php";
?>
<HTML>
<HEAD>
<TITLE><?php echo TITLE; ?></TITLE>
Etc.
In this simple version of a language system this will work properly, but when you create a cms system you may need to make an other structure.
When you want to create a new language, you just copy all the English files and place them in the map Dutch (or something else) and translate does files. After you have translated them you can insert a new field in the database and you are ready.
Fourth stepAlmost ready now. The only thing we need to do is to make the other languages work. At this moment everything will use the map /english/.
Every time the user changes from language you just insert the "language id" in a session. So if the user selects English save the number "1" in the session, like below:
CODE
<?php
$_SESSION['language']['lid'] = "1";
?>
Off course you need to get that "lid" from the database, but I think that won't be a difficult thing to do.
When you have done this you need to make a script that chooses the right language and that will create the session when the user haven't entered the website before. See the last step.
Last stepNow you have done that you can use this session to get the information out of the database. Just by making a new include file. See the example below:
CODE
<?php
include "./includes/get_language.php";
include "./includes/languages/english/main.lang.php";
include "./includes/languages/english/sub/name.lang.php";
?>
In this file you can simply use a mysql script to get the information out of the database. You will have something like below:
CODE
<?php
// if the user haven't selected a language before!!
if (empty($_SESSION['language']['lid'])) {
 $_SESSION['language']['lid'] = "1";
}
// mysql
 "SELECT * FROM 'languages' WHERE lid = '" . $_SESSION['language'] ['lid'] . "'";
?>
And as includes:
CODE
<?php
include "./includes/get_language.php";
include "./includes/languages" . $object->lpath . "main.lang.php";
include "./includes/languages" . $object->lpath . "sub/name.lang.php";
?>