[PHP] Make your site multilang!
7 posts
Page 1 of 1
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+

CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo![]()
good tutorial. I do believe this is the first Language tut I have seen. Awesome job. +rep to u!
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.

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![]()
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!
Well done!
:/ I don't know what host you're using, but sessions are stored indefinately on my server unless I delete them.

7 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023