Syed Abdul Baqi

Multilingual website using PHP

Posted on: July 2, 2009

Hello Everybody,

Today I will show you a simple approach to achieve Multilingual functionality in the website powered by PHP. This approach is so simple that I believe we can use it in small and medium scale projects without any hesitations :).

Before going into more details, if you want to check the code you can download it from here (rename extension “doc” to “zip”).

My approach towards this requirement is simple. First create different Language Files (example: english.lng, dutch.lng, etc) which will hold language vocabulary defined in a constants. After this we will have to replace the text (in our PHP files) with constant defined in file, where ever neccessary. Now depending upon the language selected load the particular language file. Best way to this approach is by passing language name using query string like,

http://localhost/multilingual/index.php?language=en

So based upon the query string parameter language file will be loaded and hence the text in the website will change as per the language file. To make things more better we can also make use of Apache Mod Rewrite to make URL more user friendly.

.htaccess

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteRule    ^(en|du|EN|DU)/(.*) $2?language=$1 [NC,L,QSA]
 RewriteRule    ^(en|du|EN|DU)$ http://localhost/multilingual/$1/index.php [R=301,L]
</IfModule>

This code will convert the URL

http://localhost/multilingual/index.php?language=en

to

http://localhost/multilingual/en/index.php

You can download the complete code from here (rename extension “doc” to “zip”). Let me know how I can make this code more better and robust.

Comments are welcomed….

Advertisements

14 Responses to "Multilingual website using PHP"

Abdul you have done good job!

hello abdul,

i can not download the code from the link u have given..can u give me alternative way to get the code.

thanks,
vimal

Hello,

I have updated the download link.

Thanks.

What about dynamic text i mean some text comes from database and how we can implement language for images.

Excellent Job

The download link for the code isn’t working 😦

@tomxlim
download link is working fine. you just need to rename the file extension from “doc” to “zip” after downloading.

how to use it for urdu and what about the contents on the web page coming from database

@LalShad
For Urdu language look into unicode encoding.
For dynamic websites instead of getting data from language definition file, you need to pull data from database based upon language selection.

Hi, first I wanted to congratulate you on the material and the tutorial:
One question, as I do I detect the browser language automatically …?

@Tucuta Gonzalez
Thanks. 🙂
About your question, I don’t believe browser language has to do anything with this approach.

Hi, thanks for the prompt reply.

Sorry for my bad English.

My question is how do I detect the browser language and redirect to corresponding.

example

if ($ USER_LANGUAGE == ‘on’) {
header (‘Location: http://www.ejemplo.con/en‘);
}

I think this should do the trick……..

if(($_REQUEST[‘language’] != ‘en’) && preg_match(‘/en-US/’, $_SERVER[‘HTTP_USER_AGENT’]))
{
header (‘Location: http://www.ejemplo.con/en‘);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: