[PHP] Make your site multilang!
Posted: Fri Sep 14, 2012 9:50 pm
Hello everyone,
It's been a while since I posted last tut so there you go
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:
/index.php?lang=en
And if parameter is set we'll set session and make cookie
Next, we will check if cookie or session is already set:
Then we will pass on to language switching part:
this is done by following code:
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)
and so on
***************************
Main php page
First of all, you should include lang.php file (Including is done above html tag in <?php ?>)
Switching language:
It is simple as
That's pretty much it
Hope you like it, if I helped you please rep+ Thanks
It's been a while since I posted last tut so there you go
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
Then we will open an if loop and check if languge was set/reset via $_GET method<?php session_start();
Code: Select all
This is basicly geting parameters from URL bar:if(isset($_GET['lang']))
{
/index.php?lang=en
And if parameter is set we'll set session and make cookie
Code: Select all
As you may see cookie will last for 30 days (Since cookie interval is set in seconds)$lang = $_GET['lang'];
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + (3600 * 24 * 30));
Next, we will check if cookie or session is already set:
Code: Select all
So if it is already set, script will change language to one defined in cookie/sessionelse if(isset($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isset($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
Then we will pass on to language switching part:
this is done by following code:
Code: Select all
And then finaly include dictionary file:switch ($lang) {
case 'en':
$lang_file = 'lang_en.php';
break;
case 'es':
$lang_file = 'lang_es.php';
break;
default:
$lang_file = 'lang_en.php';
}
Code: Select all
On the end it should look like:include_once "$lang_file";
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
And then spanish:$dictionary['welcomemsg'] = 'Welcome to Code\'n\'Stuff!';
Code: Select all
(yes it is google translator )$dictionary['welcomemsg'] = 'Bienvenido a Code\'n\'Stuff';
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
And then where you want to print definition out of dictionary:include_once 'language.php';
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
Hope you like it, if I helped you please rep+ Thanks