Page 1 of 1

[PHP] Make your site multilang!

Posted: Fri Sep 14, 2012 9:50 pm
by 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

Re: [PHP] Make your site multilang!

Posted: Fri Sep 14, 2012 11:01 pm
by Scottie1972
good tutorial. I do believe this is the first Language tut I have seen. Awesome job. +rep to u!

Re: [PHP] Make your site multilang!

Posted: Sun Sep 16, 2012 6:34 am
by Shim
wow nice tutorial well explained :D +rep

Re: [PHP] Make your site multilang!

Posted: Sat Dec 01, 2012 11:10 pm
by rocky4126
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.

Re: [PHP] Make your site multilang!

Posted: Sun Dec 02, 2012 8:16 am
by 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.

Re: [PHP] Make your site multilang!

Posted: Sun Dec 02, 2012 2:05 pm
by 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!

Re: [PHP] Make your site multilang!

Posted: Fri Feb 08, 2013 7:52 am
by rocky4126
:/ I don't know what host you're using, but sessions are stored indefinately on my server unless I delete them.