Asif18

{ 0 Comments }

This article will teach about you the language translation in PHP, by saving the body texts into an array we can translate the words into any language. Simply we need to save the texts in .php file as an array and echo the array in the body page. Below is a simple example for language translation in php. Language translation in PHP is very simple, texts in an array and echo it in the files where do you want to display the texts in other languages.

First create a folder called lang and check the permissions it has 755

Create language files

Create language files and save it to seperate folder in their language code name. All the language files and folders that are all inside of the 'lang' folder must be in the name of thier language code.

First create we will create language folder and file for English,

Create a folder called 'en' inside of the 'lang' folder and create a php file inside 'en' folder called 'en.php'

Thus the 'en.php' containing directory will be like this :

lang/en/en.php

for more languages create the language files like above. In this example i have created files for 3 languages like,

lang/ar/ar.php
lang/en/en.php
lang/es/es.php

Put your texts in 'en.php' file in an array for English:

<?php
$lang = array(
"welcome"		=> "welcome",
"login"			=> "login",
"this page's language will be dynamically changed. open the library file and change the session value to see the language translation"	=> "this page's language will be dynamically changed. open the library file and change the session value to see the language translation"
);
?>

Put your texts in 'ar.php' file in an array for Arabic:

<?php
$lang = array(
"welcome"		=> "ترحيب",
"login"			=> "دخول",
"this page's language will be dynamically changed. open the library file and change the session value to see the language translation"	=> "سيتم تغيير حيوي لغة هذه الصفحة. فتح ملف المكتبة وتغيير قيمة الدورة لرؤية الترجمة"
);
?>

Put your texts in 'es.php' file in an array for Spanish:

<?php
$lang = array(
"welcome"		=> "bienvenida",
"login"			=> "iniciar la sesión",
"this page's language will be dynamically changed. open the library file and change the session value to see the language translation"	=> "el lenguaje de esta página se puede cambiar dinámicamente. abrir el archivo de la biblioteca y cambiar el valor de la sesión para ver la traducción de la lengua"
);
?>

Create a library file outside of 'lang' folder:

<?php
session_start();
$_SESSION["language"] = "en"; // set the default language of the website
if(isset($_GET["lang"]) && $_GET["lang"] != ""){ // check if get any language change parameter and change the session value
	$_SESSION["language"] = $_GET["lang"];
}
if(isset($_SESSION["language"]) && $_SESSION["language"] != ""){
	$language = $_SESSION["language"];
	include "lang/$language/$language.php"; // include the language file from the 'lang' folder
}
?>

Design the index file:

<?php
include 'library.php'; // include the library to get the session values
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $lang["login"]; // echo the array texts which you saved in language files ?></title>
</head>

<body>
<select onChange="window.location = '?lang='+this.value+''">
<option value="" selected="selected" disabled="disabled">Select language</option>
<option value="en">English</option>
<option value="ar">Arabic</option>
<option value="es">Spanish</option>
</select>
<h1><?php echo $lang["welcome"]; // echo the array texts which you saved in language files ?></h1>
<p><?php echo $lang["this page's language will be dynamically changed. open the library file and change the session value to see the language translation"]; // echo the array texts which you saved in language files ?></p>
</body>
</html>

Finally the directory design will be like this:

Your Directory/
              lang/
                  ar/
                     ar.php

                  en/
                     en.php

                  es/
                     es.php

              index.php
              library.php

NOTE : For single language jus remove the select box in index file and change the $_SESSION["language"] = "en"; value in library

Comments (0)