Syed Abdul Baqi

Archive for the ‘apache’ Category

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….