Welcome Guest ( Log In | Register )

2 Pages V   1 2 >  
Reply to this topicStart new topic
> Creating A Simple Multi Language System, PHP - Beginner - Intermediate
Phryxus
post Oct 25 2004, 10:33 AM
Post #1


Uptight Dot
Group Icon
Group: Moderator
Posts: 1,168
Joined: 20-October 04
Member No.: 916





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 step

Almost 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 step
Now 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";
?>


IPB
Gelderblom Webdesign {SITE24 - BLA Rotterdam}

Open Source alternatives:
 
+Quote Post  Go to the top of the page
avertut
post Jan 22 2007, 05:18 AM
Post #2


New Member
Group Icon
Group: Member
Posts: 3
Joined: 22-January 07
From: Brasil
Member No.: 14,586





HI Phryxus!
Sorry, about my english, it's very poor!
This code is very important to me, I looked therefore the much time.
I'm sorry I can't understand very well, I'm not a programmer but I try to learn.
How can the user change the language?
I've try to work but something is worng, like:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\apache\htdocs\sessoes\4\includes\get_language.php on line 15
Have you got a same example?

Thank's
 
+Quote Post  Go to the top of the page
avertut
post Jan 22 2007, 01:56 PM
Post #3


New Member
Group Icon
Group: Member
Posts: 3
Joined: 22-January 07
From: Brasil
Member No.: 14,586





HI Phryxus!
I Obtained!!!!!!!!!!!!!


But when I close the browser and open again the folow code

if (empty($_SESSION['idiomas'])) {
$_SESSION['idiomas']['id'] = 1;
}

Not work!

Warning: main(./languages//main.lang.php) [function.main]: failed to open stream: No such file or directory in c:\apache\htdocs\sessoes\4\default.php on line 10
 
+Quote Post  Go to the top of the page
avertut
post Jan 22 2007, 02:31 PM
Post #4


New Member
Group Icon
Group: Member
Posts: 3
Joined: 22-January 07
From: Brasil
Member No.: 14,586





Hey!
I win!

Where ->
if (empty($_SESSION['language'])) {
$_SESSION['language']['lid'] = "1";
}

Now is ->
if (empty($_SESSION['language']['lid'])) {
 $_SESSION['language']['lid'] = "1";
}

and works very well!!!!!!!!!!!!!!!!!!!!!!
 
+Quote Post  Go to the top of the page
Phryxus
post Mar 6 2007, 04:15 AM
Post #5


Uptight Dot
Group Icon
Group: Moderator
Posts: 1,168
Joined: 20-October 04
Member No.: 916





Ahh, I should have seen this post earlier. bigwink.gif Going to fix it in the tutorial, thanks!


IPB
Gelderblom Webdesign {SITE24 - BLA Rotterdam}

Open Source alternatives:
 
+Quote Post  Go to the top of the page
SkyRanger
post Apr 10 2007, 05:41 PM
Post #6


New Member
Group Icon
Group: Member
Posts: 1
Joined: 9-April 07
Member No.: 15,773





Awsome script, thanks for the post. I am currently trying to use this script with some good luck, I can actually get it to read the default language set both by the $_SESSION['language']['lid'] = "1"; and also by $_SESSION['language']['lid'] = "$defaultlang"; which is the default set by the admin.

Now because I am a real noob, does anybody have a demo of this script. I can't figure out how to get the visitor to change the language to the one they want.
 
+Quote Post  Go to the top of the page
Webgirl
post Apr 30 2007, 12:09 PM
Post #7


New Member
Group Icon
Group: Member
Posts: 12
Joined: 19-April 07
Member No.: 15,919





Thanks for sharing might use it in future as I use different languages.
 
+Quote Post  Go to the top of the page
rszeus
post Jun 19 2007, 09:34 AM
Post #8


New Member
Group Icon
Group: Member
Posts: 2
Joined: 19-June 07
Member No.: 16,576





Could you manage the necessary scripts ?

Just the basic ones ?
The code to the link (when you press another language)
And the get_language.php
The database, I am not geting the point, could you explain it again ?

Thank you
 
+Quote Post  Go to the top of the page
rooftopnu
post Jun 19 2007, 05:40 PM
Post #9


New Member
Group Icon
Group: Member
Posts: 1
Joined: 17-June 07
Member No.: 16,541





Hi, I'm new to mysql and I don't really know how to create a database and with all the information that you gave, and I've been trying to make a new database and I receive many errors.. So, please, if there's anyone who can help me with mysql with this tutorial, or if there's any other way to make this possible without using google translators.. Pleasee! I need it! (: My msn irina@rooftop.nu
 
+Quote Post  Go to the top of the page
rszeus
post Aug 27 2007, 06:07 PM
Post #10


New Member
Group Icon
Group: Member
Posts: 2
Joined: 19-June 07
Member No.: 16,576





QUOTE(rszeus @ Jun 19 2007, 06:34 PM) *
Could you manage the necessary scripts ?

Just the basic ones ?
The code to the link (when you press another language)
And the get_language.php
The database, I am not geting the point, could you explain it again ?

Thank you


Would like that too.
Looks very simple to work with the languages that way, but I am not understanding very well how to do it.

It would be nice if you provide the sample code.
Thanks
 
+Quote Post  Go to the top of the page

2 Pages V   1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 2nd December 2008 - 01:10 PM
Advertising | Xecuter 3 Mod Chip | Mortgage Calculator | Truckers Forum | Loans