Tech and stuff
Most of us would probably agree that if you wanted to create a single language website and wanted it to be accessible to the broadest audience, you should write the content in the English language. Although the English language has become international, there are still many countries where it is not spoken. What do you do if you want your website to be easily accessible for viewers from specific nationalities? You simply include multilingual content on your website.
One of my clients asked me once to build him a web site that needed to be available in two languages. Because I hadn’t had any experience at that time with multilingual websites, I started digging into internet to look for solutions. I have come across several methods that where either too complex for my project or just, in my opinion, not efficient enough. I finally finished my project dealing with this problem the way I thought was very simple and yet flexible enough to use in variety of other websites I had pleasure to work later on.
The solution introduced in this article is well suited for a variety of websites but works best with 2 languages. To begin, simple functions need to be created to switch between contents stored in PHP variables. This example will be utilizing browser cookies, also called tracking cookies. “Why will we use cookies”? you may ask. Let me explain why it is useful to use them in our scenario.
A cookie is a small file, saved to your computer that contain some website related data. They are usually used to store small values, like user id or the number of visits to the website. In our example cookie will be used to store language variable. It will be stored in a form of short string like “en” or “pl”. When visitor returns to the website, information about preferred language will be loaded from the cookie and automatically set website language. Hey, that sounds good, doesn’t it?
In this example you will have to create two files.
function set_lang() {
session_start();
$default = 'en';
if(!isset($_SESSION['lang_ses'])) {
if(isset($_COOKIE['lang'])) {
$_SESSION['lang_ses'] = $_COOKIE['lang'];
} else {
$_SESSION['lang_ses'] = $default;
}
}
if(isset($_GET['lang'])) {
$_SESSION['lang_ses']=$_GET['lang'];
setcookie('lang',$_GET['lang'],time()+24*3600);
}
}
Let’s take a look at our function.php file. There are two functions that will do all the work. The first one is called set_lang(). The purpose of this function is to do a quick check every time the page is loaded to see if visitor requested to change the language preference. It starts off with checking if the cookie with language variable exists. If it doesn’t, it has to be created and its default value would be set to the one declared in variable $default. In our case variable $default is set to “en” which will correspond to English language. When visitor clicks on a link to change the language, function set_lang() will put new value to the cookie.
function get_lang($lang_pl, $lang_en) {
if($_SESSION['lang_ses']=='pl') {
return $lang_pl;
} elseif ($_SESSION['lang_ses'] ='en') {
return $lang_en;
}
}
Look closely at the string that defines our cookie. First parameter is the name of the cookie, second is the information it will hold and third parameter refers to time in seconds by when the cookie will expire. If cookie expiry time is not set, the cookie will expire right after the browser is closed. As you can see, in our example the cookie is set to expire after 30 days.
Now, here is the example content of the index.php file.
<?php
// Include the function file
include("function.php");
// Call set_lang() function
set_lang();
?>
<html>
<head>
<title>Multilingual website</title>
</head>
<body>
<p>
<!-- Menu that lets you switch languages -->
<a href="index.php?lang=en">English</a> | <a href="index.php?lang=pl">Polish</a>
</p>
<p>
<a href="index.php">Index</a><br/>
<a href="page1.php">Page 1</a>
</p>
<p>
<!-- Call get_lang() function and set-->
<? echo get_lang('Some content in Polish language','Some content in English language'); ?>
</p>
</body>
</html>
That would be it. Simple, and yet elegant way to switch between content in two languages. Download example files: 1_bilingual-websites.zip