Bilingual websites made easy
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.
1) Function.php - file that will contain only our php functions
2) Index.php - the main website file that will contain the content and call our "magic" functions from function.php file.
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.
That would be it. Simple, and yet elegant way to switch between content in two languages.
Oh my man... I had the same problem like you. I've implemented a lot php scripts, but they failed to work or they were to complex. Until I found your snippet and it works like a charm. Thanks for sharing and saving me from another day of frustrations :)
Hi Marcin, I guess I was a bit too enthusiastic :( I'm facing a problem when I have pages two levels down from the root folder and it seems not to remember on which language the user was. He/she have to switch the language again on the two level down page itself, so that's working properly. Does this issue sound familiar to you or did you test this language switcher when pages are not in the root? Hope to hear from you soon... thanks!
Ralph, I've updated the script, please check if it works now. Thanks to sessions language variable will be accessible from whatever page visitor is on.
Great! Adding $_SESSION to the function did the trick. It works now with pages in a sub-directory. Good job, Marcin!
Hi, I am new to php and I will learn it now. What do I have to add? Must I translate the content in English and Portuguese or will the code translate automatic? Sorry if I´m asking to stupid! I want to have a site in Portug and Engli. for selling realestate in Brazil.
Oyvind: This code won't translate text for you. You have to provide translations. To better understand the code download the example from the link at the end of the article. As a side note, if you want your text to be automatically translated try using google service. It's not perfect but may get the job done. Either way good luck ;)
hi, the zip link is broken... thks
@Fabien: Fixed it, thanks.
Post a comment
All the fields in the form have to be filled in