[How-To]Multi-language Support to a PHP Website

02/09/2013 08:53 TraxWall#1
Are you interested in having a multilingual website? This is a tutorial that shows you how you can do that in PHP.
[Only registered and activated users can see links. Click Here To Register...]
The first thing we need to do is to create a couple of files that will contain the text for each of the languages that will be supported by the website. For demo purpose I have chosen English, Spanish and German. Make a directory named “directory”. In this directory create 3 files: lang.de.php, lang.en.php, and lang.es.php. In our main file (index.php) we will include a file (common.php) containing a piece of code that gets the requested language. Here’s the content of those 3 language files:

lang.en.php

Code:
<?php
/*
------------------
Language: English
------------------
*/
 
$lang = array();
 
$lang['PAGE_TITLE'] = 'My website page title';
$lang['HEADER_TITLE'] = 'My website header title';
$lang['SITE_NAME'] = 'My Website';
$lang['SLOGAN'] = 'My slogan here';
$lang['HEADING'] = 'Heading';

// Menu

$lang['MENU_HOME'] = 'Home';
$lang['MENU_ABOUT_US'] = 'About Us';
$lang['MENU_OUR_PRODUCTS'] = 'Our products';
$lang['MENU_CONTACT_US'] = 'Contact Us';
$lang['MENU_ADVERTISE'] = 'Advertise';
$lang['MENU_SITE_MAP'] = 'Site Map';
?>

As you can notice, some constants are created using the define() function. In every file the defined constants have the same name, bu the values is different. We will output the values of the constants inside the index.php file. Therefore we will see different text every time we will call other language file.
Determine the right language

Let’s analyze the common.php file:

Code:
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_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 'de':
  $lang_file = 'lang.de.php';
  break;

  case 'es':
  $lang_file = 'lang.es.php';
  break;

  default:
  $lang_file = 'lang.en.php';

}

include_once 'languages/'.$lang_file;
?>
After we determine the value of $lang, we use switch() to compare its value with some different values, and execute a different piece of code depending on which value it equals to. After the value of the $lang_file is determined, the script will include the necessary language file. As you can see I have used sessions to register the value of $lang. This way users can navigate through the whole site and see the content in the chosen language (lang=[language here] does not need to be passed in every URL). Additionally, I have used cookies to store the selected language in users computer for 30 days. When the visitor will come back he will see the site in the language that he previously selected.
How if the website’s language requested?

In this demo I have chosen to use some image flags, each image having a link to index.php?lang=[LANG HERE]. So, to see the site in german we will use the German image flag which links to index.php?lang=de.

Lastly, the constants values should be outputted in the page. Examples:


for document title

Code:
<title><?php echo $lang['PAGE_TITLE']; ?></title>
for header menu
Code:
<ul>
     <li><a href="/"><?php echo $lang['MENU_HOME']; ?></a></li>
     <li><a href="about_us"><?php echo $lang['MENU_ABOUT_US']; ?></a></li>
  <li><a href="our_products"><?php echo $lang['MENU_OUR_PRODUCTS']; ?></a></li>
     <li><a href="contact_us"><?php echo $lang['MENU_CONTACT_US']; ?></a></li>
     <li><a href="advertise"><?php echo $lang['MENU_ADVERTISE']; ?></a></li>	
     <li><a href="sitemap"><?php echo $lang['MENU_SITE_MAP']; ?></a></li>
   </ul>

Thank you for checking on my topic.
If you liked hit a thank-

:handsdown::handsdown::mofo:
02/09/2013 09:19 #SoNiice#2
And why u post it at this section?
02/09/2013 10:06 TraxWall#3
Metin2 sites and so perfect.
Also if you do not belong here ... you can then move to a moderator
02/09/2013 11:47 -TÜRK-#4
Good Tutorial. Thank You. I can use on MT2GS CMS.
02/09/2013 12:21 .Alessa#5
Please give the source of this tutorial. I don't think you wrote it yourself.
02/09/2013 12:33 TraxWall#6
Yes, I did not write but do not have the above side epvp :)
The page where you can find it was no longer there because 2 years Description ....

Only this is not what I wrote but was made public.
Only what I have written, and the system you call your own.
Just what I met many pages there are no more language options, and so I published the source code
02/09/2013 13:12 .Alessa#7
Then write down that you didn't made the tutorial.

Quote:
Thank you for checking on my topic.
This is clearly misleading. You can get "Thanks" (for whatever you need them) for publishing this tutorial here but make CLEAR that you didn't wrote it.
02/09/2013 13:12 ©NewProject#8
O nice danke
02/09/2013 13:48 Interception#9
Quote:
Originally Posted by .Alessa View Post
Then write down that you didn't made the tutorial.


This is clearly misleading. You can get "Thanks" (for whatever you need them) for publishing this tutorial here but make CLEAR that you didn't wrote it.
Alessa, what is your problem? I didn't knew this language thingy, and i'm sure more do. This is helpfull for people which are using an cms or some sort of site. As for you and the other flamers, haters or w.e. Just ignore and shutup?


Greetings
02/09/2013 14:02 TraxWall#10
I'm sorry for my mother because it was trying to help people so ....
I'm sorry for swearing but I hate that kind of thing that, how to connect it to another ...

sorry for the bad translation text.
02/09/2013 14:26 .Alpha.#11
Just use [Only registered and activated users can see links. Click Here To Register...] :o
02/09/2013 14:26 Red Firestar#12
Good Tutorial Thanks *-*
02/09/2013 14:51 Interception#13
btw, for the people which are not familiar with the include option:

Code:
<?php include("common.php"); ?>
on top of your php file :)
12/28/2013 03:44 V3X0RE#14
Please give credits >_<
Here's the source [Only registered and activated users can see links. Click Here To Register...]