How to create a language changer with PHP

How to create a language changer with PHP

·

4 min read

Some sites cater for more then just the English speaking world in such sites there is a need for multilingual content, you would create different sites for each language you want to support but this can soon become unmanageable. 

A better way is to use language files have all the sites content stored in language files then use the relevant language upon request.

See the language changer in action Demo

This tutorial will show you how to do just that, very simply create language files and call a specific language on request.

First create a folder called 'lang' in this folder is there all the languages will be stored. Create a new file in the lang folder one for each language for example eng.php for english fre.php for french ger.php for german and so on.

For the language changer I've decided to use flags as there instantly recognizable, I've created a folder called flags and have a flag for language I will be using.

Download: github.com/daveismynamecom/language-changer..

In the page you wish to use the language changer (index.php in my case) place the following code above the doctype:

session_start();
if($_GET['la']){
    $_SESSION['la'] = $_GET['la'];
    header('Location:'.$_SERVER['PHP_SELF']);
    exit();
}

switch($_SESSION['la']){
     case "eng":
        require('lang/eng.php');
    break;
    case "fre":
        require('lang/fre.php');
    break;
    case "ger":
        require('lang/ger.php');
    break;
    default:
        require('lang/eng.php');
    }

This turns on sessions (session_start()) then an if statement is performed, if $_GET['la'] means if the get variable la has been requested then add it to the la session then reload the page.

Next a switch statement is performed which looks at the la session, if it is equal to one of the cases it will load in the requested language file if none of them match then a default language file will be loaded (require('lang/eng.php')). If the la session contained 'ger' then the German language will would be loaded.

These language files contain the content for the page once loaded the content will be available to the page, This works by referencing an array $lang then specifying which part is wanted.

For example for the page title in php we call the lang array then specify the index-title section is what we are after.

<title><?php echo $lang['index-title'];?></title>

The index-title refers to an array item inside the language file, here is the full contents of the English language file.

//english lang

//nav
$lang['lang-eng'] = 'English';
$lang['lang-fre'] = 'French';
$lang['lang-ger'] = 'German';

//index
$lang['index-title'] = 'Language Demo';
$lang['index-welcome'] = 'Welcome to the language demo';
$lang['index-text-1'] = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$lang['demo'] = 'Return to the tutorial';

As you can see $lang['index-title'] = 'Language Demo'; stores the page title in a French language file the title is the smae only its in French this way you can easily have multiple languages.

How you prefix your language parts of the array is not important as long as you don't use the same name twice in a single language file, I like to prefix the items with the page they are meant for so index then some name '$lang['index-welcome']' This makes it much easier to keep track of things.

So change language we need some way of easily been able in invoke a new language, As I said earlier I've chosen to use flags to represent the different languages. I've create a link for each language the link path links to index.php then I pass the requested language to the link with ?la=eng for French use ?la=fre inside the link is the flags for the alt and title tags are calls to get content from the loaded language so if images cannot be shown for what ever reason the user will still know which link represents which language. 

<div id="langSelect">
<a href="index.php?la=eng"><img src="flags/eng.png" alt="<?=$lang['lang-eng'];?>" title="<?=$lang['lang-eng'];?>" /></a>
<a href="index.php?la=fre"><img src="flags/fra.png" alt="<?=$lang['lang-fre'];?>" title="<?=$lang['lang-fre'];?>" /></a>
<a href="index.php?la=ger"><img src="flags/ger.png" alt="<?=$lang['lang-ger'];?>" title="<?=$lang['lang-ger'];?>" /></a>
</div>

Here's the index.php file:

<?php
session_start();
if($_GET['la']){
    $_SESSION['la'] = $_GET['la'];
    header('Location:'.$_SERVER['PHP_SELF']);
    exit();
}

switch($_SESSION['la']){
     case "eng":
        require('lang/eng.php');
    break;
    case "fre":
        require('lang/fre.php');
    break;
    case "ger":
        require('lang/ger.php');
    break;
    default:
        require('lang/eng.php');
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $lang['index-title'];?></title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
    <div id="langSelect">
        <a href="index.php?la=eng"><img src="flags/eng.png" alt="<?=$lang['lang-eng'];?>" title="<?=$lang['lang-eng'];?>" /></a>
        <a href="index.php?la=fre"><img src="flags/fra.png" alt="<?=$lang['lang-fre'];?>" title="<?=$lang['lang-fre'];?>" /></a>
        <a href="index.php?la=ger"><img src="flags/ger.png" alt="<?=$lang['lang-ger'];?>" title="<?=$lang['lang-ger'];?>" /></a>
    </div>
    <div id="cont">
        <p><?=$lang['index-welcome'];?></p>
        <p><?=$lang['index-text-1'];?></p>
    </div>
</div>
</body>
</html>

That's it hopefully you'll see how simple this method is.

Did you find this article valuable?

Support David Carr by becoming a sponsor. Any amount is appreciated!