Hello all!,Nowadays everyone wants that their website visited by every users in every countries and this can be possible with make your website multilingual, so in this blog I am going to describe you how to make your website multilingual.
First you need to create couple of file that will keep language code, In this blog I have chosen English,German,Spanish,Hindi,French.
Make a directory "languages" and in this directory keep all file lang.de.php, lang.en.php, lang.es.php, lang.fr.php, lang.hi.php.
In our main index.php file we will include language.php file which containing a some code that gets the requested language.
Here's I am using 3 file you can download full code below
lang.de.php
<?php
/*
-----------------
Language: German
-----------------
*/
$lang = array();
$lang['PAGE_TITLE'] = 'Meine Webseite Titel';
$lang['HEADER_TITLE'] = 'Meine Website-Header Titel';
$lang['SITE_NAME'] = 'Meine Website';
$lang['SLOGAN'] = 'Mein Slogan hier';
$lang['HEADING'] = 'Position';
// Menu
$lang['MENU_HOME'] = 'Heim';
$lang['MENU_ABOUT_US'] = 'ber uns';
$lang['MENU_OUR_PRODUCTS'] = 'Unsere Produkte';
$lang['MENU_CONTACT_US'] = 'Kontaktieren Sie uns';
$lang['MENU_ADVERTISE'] = 'Werben';
$lang['MENU_SITE_MAP'] = 'Site Karte';
$lang['TEXT1'] = 'Willkommen bei mehrsprachig Blog wird es Ihnen helfen zu verstehen';
$lang['TEXT2'] = 'Es ist ein zweites Textfeld';
?>
lang.en.php
<?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';
$lang['TEXT1'] = 'Welcome to Multi language Blog it will help you to understand';
$lang['TEXT2'] = ' It is a second text box';
?>
lang.hi.php
<?php
/*
------------------
Language: Hindi
------------------
*/
$lang = array();
$lang['PAGE_TITLE'] = ' ';
$lang['HEADER_TITLE'] = ' ';
$lang['SITE_NAME'] = ' ';
$lang['SLOGAN'] = ' ';
$lang['HEADING'] = '';
// Menu
$lang['MENU_HOME'] = '';
$lang['MENU_ABOUT_US'] = ' ';
$lang['MENU_CONTACT_US'] = ' ';
$lang['MENU_ADVERTISE'] = ' ';
$lang['MENU_SITE_MAP'] = ' ';
$lang['TEXT1'] = ' , ';
$lang['TEXT2'] = ' ';
?>
Let’s analyze the language.php file:
<?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':
//English
$lang_file = 'lang.en.php';
break;
case 'de':
//German
$lang_file = 'lang.de.php';
break;
case 'es':
//Spanish
$lang_file = 'lang.es.php';
break;
case 'hi':
//Hindi
$lang_file = 'lang.hi.php';
break;
case 'fr':
//Franch
$lang_file = 'lang.fr.php';
break;
// Default English
default:
$lang_file = 'lang.en.php';
}
include_once 'languages/'.$lang_file;
?>
After we determine the value of $lang,we can compare its value with some different values,and execute a different code depend on which value it equals to.after this script will include necessary language file.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.
When we click the flag each flag having a link to see the site in diffrent language.
now your Index.php file look like this
<?php
include_once 'language.php';
?>
<head>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-GB" xml:lang="en-GB">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en-GB" />
<title><?php echo $lang['PAGE_TITLE']; ?></title>
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Hindi">
<link href="css/style.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div id="OuterFrmDl">
<div id="HeadSectionDl">
<div id="languages">
<a href="index.php?lang=en"><img src="images/en.png" title="English"/></a>
<a href="index.php?lang=de"><img src="images/de.png" title="German"/></a>
<a href="index.php?lang=es"><img src="images/es.png" title="Spanish"/></a>
<a href="index.php?lang=hi"><img src="images/hi.png" title="Hindi"/></a>
<a href="index.php?lang=fr"><img src="images/fr.png" title="French"/></a>
</div>
<div id="WSTitleDL"><?php echo $lang['HEADER_TITLE']; ?></div>
<div id="WSubTitleDL"><?php echo $lang['SLOGAN']; ?></div>
</div>
<div id="TopHzNavSectionDl">
<div class="HzNavCntDL">
<ul>
<li><a href="#"><?php echo $lang['MENU_HOME']; ?></a></li>
<li><a href="#"><?php echo $lang['MENU_ABOUT_US']; ?></a></li>
<li><a href="#"><?php echo $lang['MENU_CONTACT_US']; ?></a></li>
<li><a href="#"><?php echo $lang['MENU_ADVERTISE']; ?></a></li>
<li><a href="#"><?php echo $lang['MENU_SITE_MAP']; ?></a></li>
</ul>
</div>
</div>
<div id="CntSectionDlDiv">
<table id="CntSectionDlTbl" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="ContentsArea" valign="top">
<div class="ContentBoxDl">
<h1><?php echo $lang['HEADING']; ?> 1 </h1>
<p>
<?php echo $lang['TEXT1']; ?>
</p>
<p> </p>
<h2><?php echo $lang['HEADING']; ?> 2 </h2>
<p>
<?php echo $lang['TEXT2']; ?>
</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="FooterBxDL">
<p>Copyright Blog</p>
</div>
</body>
</html>
You can download complete code from attached .zip file below
1 Comment(s)