Implement Multi-language support to Website with PHP

Setting up multi-language support is a good way to attract the new users to your website but there is no direct way to implement it to the website.

You need to write extra code and update existing hardcoded text content to enable multiple languages.

For maintaining this you can either create separate files or use MySQL table.

In this tutorial, I will create simple examples to show multi-language support on the website.

Implement Multi-language support to Website with PHP


Contents

  1. Define constant
  2. MySQL
  3. Conclusion

1. Define constant

Create a separate PHP file for languages e.g. lang_en.php, lang_pl.php, etc. and define constants variable in it.

Include the file according to language selection and use the constant variables in the place of string text on the web page.

Example

Create two lang_en.php and lang_pl.php files. The lang_en.php to store english value and lang_pl.php for Polish.

lang_en.php

<?php

define("_REGISTER", "Register");
define("_NAME", "Name");
define("_USERNAME", "Username");
define("_EMAIL", "Email");
define("_ADDRESS", "Address");
define("_SUBMIT", "Submit");

lang_pl.php

<?php

define("_REGISTER", "Zarejestrować");
define("_NAME", "Nazwa");
define("_USERNAME", "Nazwa Użytkownika");
define("_EMAIL", "E-mail");
define("_ADDRESS", "Adres");
define("_SUBMIT", "Zatwierdź");

Add <meta > tag to set charset to utf8.

Create a <select > element with language options and create a <form> where use constant variables. Submit the language form when the option is selected from the <select> element.

Initialize $_SESSION['lang'] with submitted language value and include a file.

index.php

<?php
session_start();

// Set Language variable
if(isset($_GET['lang']) && !empty($_GET['lang'])){
 $_SESSION['lang'] = $_GET['lang'];

 if(isset($_SESSION['lang']) && $_SESSION['lang'] != $_GET['lang']){
  echo "<script type='text/javascript'> location.reload(); </script>";
 }
}

// Include Language file
if(isset($_SESSION['lang'])){
 include "lang_".$_SESSION['lang'].".php";
}else{
 include "lang_en.php";
}
?>

<!doctype html>
<html>
 <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
 <body >
 <script>
 function changeLang(){
  document.getElementById('form_lang').submit();
 }
 </script>

 <!-- Language -->
 <form method='get' action='' id='form_lang' >
   Select Language : <select name='lang' onchange='changeLang();' >
   <option value='en' <?php if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'en'){ echo "selected"; } ?> >English</option>
   <option value='pl' <?php if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'pl'){ echo "selected"; } ?> >Polish</option>
  </select>
 </form>

<!-- Form -->
 <h1><?= _REGISTER ?></h1>
 <form method='post' action=''>
  <?= _NAME ?> : <input type='text' name='name' /><br>
  <?= _USERNAME ?> : <input type='text' name='username' /><br>
  <?= _EMAIL ?> : <input type='text' name='email' /><br>
  <?= _ADDRESS ?> : <input type='text' name='address' /><br>
  <input type='submit' value='<?= _SUBMIT ?>'>
 </form>
 </body>
</html>

2. MySQL

Create separate columns or MySQL table to handle multi-language.

In the example, I am creating a column to store English and Polish content.

posts Table

Set the table Collation to utf8_general_ci and also for the field which will store other language content e.g. title_pl, content_pl.

CREATE TABLE IF NOT EXISTS `posts` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(100) CHARACTER SET latin1 NOT NULL,
 `title_pl` varchar(100) NOT NULL,
 `content` text CHARACTER SET latin1 NOT NULL,
 `content_pl` text NOT NULL,
 `link` varchar(255) CHARACTER SET latin1 NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

config.php

<?php
session_start();

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}
mysqli_set_charset( $con, 'utf8');

NOTE – Make sure to add mysqli_set_charset( $connection-variable, 'utf8').

lang_en.php

<?php

define("_POSTS", "POSTS");
define("_MORE", "More");

lang_pl.php

<?php

define("_POSTS", "POSTS");
define("_MORE", "Jeszcze");

index.php

<?php
include "config.php";

// Set Language variable
if(isset($_GET['lang']) && !empty($_GET['lang'])){
 $_SESSION['lang'] = $_GET['lang'];

 if(isset($_SESSION['lang']) && $_SESSION['lang'] != $_GET['lang']){
  echo "<script type='text/javascript'> location.reload(); </script>";
 }
}

// Include Language file
if(isset($_SESSION['lang'])){
 include "lang_".$_SESSION['lang'].".php";
}else{
 include "lang_en.php";
}
?>

<!doctype html>
<html>
 <head>
  <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
  <link href="style.css" type="text/css" rel="stylesheet">
 </head>
 <body >
 <script>
 function changeLang(){
  document.getElementById('form_lang').submit();
 }
 </script>

 <!-- Language -->
 <form method='get' action='' id='form_lang' >
  Select Language : <select name='lang' onchange='changeLang();' >
   <option value='en' <?php if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'en'){ echo "selected"; } ?> >English</option>
   <option value='pl' <?php if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'pl'){ echo "selected"; } ?> >Polish</option>
  </select>
 </form>

 <!-- POST List -->
 <h1><?= _POSTS ?></h1>
 <div class="container">

 <?php
 $query = "select * from posts";
 $result = mysqli_query($con,$query);

 while($row = mysqli_fetch_array($result)){

  // Selection field
  $title_field = "title";
  $content_field = "content";
  if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'pl'){
   $title_field = "title_pl";
   $content_field = "content_pl";
  }
  $id = $row['id'];
  $title = $row[$title_field];
  $content = $row[$content_field];
  $shortcontent = substr($content, 0, 160)."...";
  $link = $row['link'];
?>
 <!-- Post -->
 <div class="post" id="post_<?php echo $id; ?>">
 <h1><?php echo $title; ?></h1>
 <p>
 <?php echo $shortcontent; ?>
 </p>
 <a href="<?php echo $link; ?>" class="more" target="_blank"><?= _MORE ?></a>
 </div>

 <?php
 }
 ?>
 </div>
 </body>
</html>

3. Conclusion

Set your web page charset to utf8 using <meta > tag and while storing multi-language to the MySQL database table set its Collation to utf8_general_ci.

If you found this tutorial helpful then don't forget to share.