Wednesday, 29 November 2017

Multi-language Support to Website using PHP

Creating a Multi-language (multilingual) web page is easy to implement. Multi-language websites are used to increase the volume of users supported for a website. Multi-language support can be done in various ways.
  1. We can have duplicate pages with the same content in required languages.
  2. We can add language configuration (localization) to change the text in a different language.
I prefer to go with the second option described. Instead of duplicating pages, let’s have a common page structure and populate the content based on the language selected by pulling language specific content from lang files and database.

In this tutorial, we are going to create a multilingual page which shows both static and dynamic data. The static data are changed in language basis by using respective language configuration file. The dynamic data are managed by the database columns prefixed with the language code. I will provide multilingual support with two languages English and German. So the database column_name will be en_column_name and de_column_name.

Multi Language HTML Page

This HTML page contains a header showing title and the links ‘English‘, ‘Deutsche‘ for changing the language between English and German, respectively. Also, this page is showing dynamic data from the database. The whole content of this page including static labels and the dynamic database results will be changed based on the language selected.
<div class="language-header">
 <div class="demo-page-title"><?php echo $language["LIST_TITLE"]; ?></div>
 <div class="language-link">
  <a class="language-link-item" href="index.php?lang=en" <?php if($lang == 'en'){?> style="color:#ff9900;" <?php } ?>>English</a> | 
  <a class="language-link-item" href="index.php?lang=de"  <?php if($lang == 'de'){?> style="color:#ff9900;" <?php } ?>>Deutsche</a>
 </div>
</div>
<?php 
 if(!empty($result)){ 
  foreach($result as $k=>$v){
?>
 <div class="demo-row-data">
 <div><strong><?php echo $result[$k][$lang.'_title']; ?></strong></div> 
 <p class="demo-row-description"><?php echo $result[$k][$lang.'_description']; ?>
 </p>
 </div>
<?php  
  } 
 } else {
?>
 <div class="no-result"><?php echo $language["NOTIFY_NO_RESULT"]; ?></div>
<?php 
 } 
?>

Changing the Page content using PHP, Based on Language

In this example, it has two types of data. One is the static text in HTML like the heading or the no-result-notification message. The other is the dynamic data read from the database. In this section, we are going to see the code for changing these two types of data based on the selected language.
On clicking language links, I pass the selected language code in the Querystring. I have created language configuration (localization) files for English and German (lang.en.php, lang.de.php), containing the array of text that used in HTML. Based on the selected language the appropriate configuration file will be included get the text in selected language. The code is,
<?php
 // include language configuration file based on selected language
 $lang = "en";
 if(isset($_GET['lang'])){ 
  $lang = $_GET['lang']; 
 } 
 require_once("lang.".$lang.".php");
?>
...
<div class="demo-page-title"><?php echo $language["LIST_TITLE"]; ?></div>
...
<div class="no-result"><?php echo $language["NOTIFY_NO_RESULT"]; ?></div>
In the PHP code, we connect database and fetch the results. The database contains two type of information title and description. I have separate columns for storing information in multiple languages. The table structure is,



This part of the example is to help you understand, how to do localization using database.
The column name is prefixed by the language code en_ and de_ for storing information in English and German. In the PHP code, we use the selected language code to prepend it with title and description to show the content in the appropriate language. The code is,
<?php 
 if(!empty($result)){ 
  foreach($result as $k=>$v){
?>
 <div class="demo-row-data">
 <div><strong><?php echo $result[$k][$lang.'_title']; ?></strong></div> 
 <p class="demo-row-description"><?php echo $result[$k][$lang.'_description']; ?>
 </p>
 </div>
<?php  
  } 
 }
?>
 Download

0 comments:

Post a Comment