[PHP] Make your site multilang!

7 posts Page 1 of 1
Contributors
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

[PHP] Make your site multilang!
Filip
Hello everyone,

It's been a while since I posted last tut so there you go :D

What will this tutorial do?
It will make your site support multiple languages

So let's begin:

First of all you want to make a php script which will save cookie on user's machine which will tell the script in which language page will be displayed.

Make a php file and name it how you want (in my case lang.php)

First of all we will open php tag and start a session:
Code: Select all
<?php session_start();
Then we will open an if loop and check if languge was set/reset via $_GET method
Code: Select all
if(isset($_GET['lang']))
{
This is basicly geting parameters from URL bar:

/index.php?lang=en

And if parameter is set we'll set session and make cookie
Code: Select all
$lang = $_GET['lang'];
		$_SESSION['lang'] = $lang;
		setcookie('lang', $lang, time() + (3600 * 24 * 30));
As you may see cookie will last for 30 days (Since cookie interval is set in seconds)

Next, we will check if cookie or session is already set:
Code: Select all
else if(isset($_SESSION['lang']))
{
	$lang = $_SESSION['lang'];
}
else if(isset($_COOKIE['lang']))
{
	$lang = $_COOKIE['lang'];
}
So if it is already set, script will change language to one defined in cookie/session

Then we will pass on to language switching part:

this is done by following code:
Code: Select all
switch ($lang) {
	case 'en':
		$lang_file = 'lang_en.php';
		break;
	case 'es':
		$lang_file = 'lang_es.php';
		break;
	default:
		$lang_file = 'lang_en.php';
}
And then finaly include dictionary file:
Code: Select all
include_once "$lang_file";
On the end it should look like:
Code: Select all
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
	$lang = $_GET['lang'];
		$_SESSION['lang'] = $lang;
		setcookie('lang', $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
	$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
	$lang = $_COOKIE['lang'];
}
else
{
	$lang = 'en';
}
switch ($lang) {
	case 'en':
		$lang_file = 'lang_en.php';
		break;
	case 'es':
		$lang_file = 'lang_es.php';
		break;
	default:
		$lang_file = 'lang_en.php';
}

include_once 'include/dictionary/'.$lang_file;
?>
*************************

Next step is dictionary file,
again we will be using php arrays to define texts

So for example

lang_en.php file which will contain english defininitions (Or other lang file defined above)
Code: Select all
$dictionary['welcomemsg'] = 'Welcome to Code\'n\'Stuff!';
And then spanish:
Code: Select all
$dictionary['welcomemsg'] = 'Bienvenido a Code\'n\'Stuff';
(yes it is google translator :D)

and so on

***************************

Main php page

First of all, you should include lang.php file (Including is done above html tag in <?php ?>)
Code: Select all
include_once 'language.php';
And then where you want to print definition out of dictionary:
Code: Select all
<?php echo $dictionary['welcomemsg']; ?>
***************************

Switching language:

It is simple as
Code: Select all
<a href="index.php?lang=en">Switch to english</a> 
***************************

That's pretty much it :D

Hope you like it, if I helped you please rep+ :D Thanks
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

good tutorial. I do believe this is the first Language tut I have seen. Awesome job. +rep to u!
Image
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: [PHP] Make your site multilang!
Shim
wow nice tutorial well explained :D +rep
Find my programs on Softpedia
User avatar
rocky4126
VIP - Donator
VIP - Donator
Posts: 258
Joined: Mon Nov 16, 2009 7:39 pm

one question, why use sessions and cookies at the same time? They both serve the same purpose, just one is clientside and the other is serverside.
Image
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: [PHP] Make your site multilang!
Filip
Sessions and cookies do not serve same purpose. Session works like a token allowing access and passing information while the user has their browser open. Cookies on the other hand can be stored until deleted or expired (in this code 30 days). Also, user can disable cookies or delete them.
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
Danny
VIP - Donator
VIP - Donator
Posts: 621
Joined: Sat Oct 30, 2010 8:21 pm

Re: [PHP] Make your site multilang!
Danny
This is one option indeed, but realize if you have a very big website it takes a lot of time to translate all the webpages.
Well done!
User avatar
rocky4126
VIP - Donator
VIP - Donator
Posts: 258
Joined: Mon Nov 16, 2009 7:39 pm

:/ I don't know what host you're using, but sessions are stored indefinately on my server unless I delete them.
Image
7 posts Page 1 of 1
Return to “Tutorials”