Creating Multi-lingual Webpages using PHP (part 1)
For those who have ever tried to design a multilingual web page only to end up with 5 different index pages to represent your 5 different languages, this article may be of interest. I would like to point out that this solution is only one of many. While this solution is probably not best fit for large sites, I consider it a decent solution for smaller sites.
Here is the demo that we are going to create:
Multilingual Page in PHP Example
First create your main page: i.e. index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?php $language = $_REQUEST['lang']; if($language == 'en') { require 'languages/language.en.php'; } else if($language == 'jp') { require 'languages/language.jp.php'; } else if($language == 'ko') { require 'languages/language.ko.php'; } else if($language == 'zh') { require 'languages/language.zh.php'; } else { require 'languages/language.en.php'; } ?> <html> <head> <title><?php print $txt['txt_lang']; ?></title> </head> <meta http-equiv='Content-Type' content='text/html; charset=<?php print $txt['txt_charset']; ?>'> <style type='text/css'> body{ padding:0; background-color: #EEEEEE; } </style> <body style='color:gray'> <div class='nav0'> <a href='index.php?lang=en' title='English'><img src='http://www.Ken-Soft.com/code/php/langsupport/languages/flag_english.gif' alt='English'\></a> <a href='index.php?lang=jp' title='Japanese'><img src='http://www.Ken-Soft.com/code/php/langsupport/languages/flag_japan.gif' alt='Japanese'\></a></li> <a href='index.php?lang=ko' title='Korean'><img src='http://www.Ken-Soft.com/code/php/langsupport/languages/flag_korean.gif' alt='Korean'\></a> <a href='index.php?lang=zh' title='Chinese'><img src='http://www.Ken-Soft.com/code/php/langsupport/languages/flag_chinese.gif' alt='Chinese'\></a></li> </div> <?php print $txt['txt_first_name']; ?> <br /> <?php print $txt['txt_last_name']; ?> <br /> <?php print $txt['txt_phone_number']; ?><br /> </body> |
Notice the lines at the top.
1 | $language = $_REQUEST['lang']; |
If you have ever looked at the address bar when a php is loading you may have noticed something like www.something.com/index.php?lang=en
The $_REQUEST['lang']; call would return ‘en’. This can be used for use inside your PHP script.
In the case of our index.php file, the ‘lang’ variable is used to determine what language file to include, and in the case that ‘lang’ is not defined, the English language file is included by default.
Next, notice how when the pages html is ‘echoed’ to the page every instance where text is displayed, it refers to the hash array
1 | $txt |
for example:
1 | <?php print $txt['txt_first_name']; ?> |
This looks up the hash called ‘txt_first_name’ and writes the value. Note: As well as using a hash array using Defines also works well.
Here are the sample language files used in the example.
language.en.php
1 2 3 4 5 6 7 8 9 | <?php $txt = array ( 'txt_charset' => 'iso-8859-1', 'txt_lang' => 'Language', 'txt_first_name' => 'First Name', 'txt_last_name' => 'Last Name', 'txt_phone_number' => 'Phone Number' ); ?> |
language.jp.php
1 2 3 4 5 6 7 8 9 | <?php $txt = array ( 'txt_charset' => 'utf-8', 'txt_lang' => '言語', 'txt_first_name' => '名', 'txt_last_name' => '姓', 'txt_phone_number' => '電話番号' ); ?> |
language.zh.php
1 2 3 4 5 6 7 8 9 | <?php $txt = array ( 'txt_charset' => 'utf-8', 'txt_lang' => '语言', 'txt_first_name' => '名字', 'txt_last_name' => '姓', 'txt_phone_number' => '电话号码' ); ?> |
language.ko.php
1 2 3 4 5 6 7 8 9 | <?php $txt = array ( 'txt_charset' => 'utf-8', 'txt_lang' => '언어 ', 'txt_first_name' => '이름', 'txt_last_name' => '성', 'txt_phone_number' => '전화 번호' ); ?> |









Thanks a lot! This is the best explanation of this feature I’ve found on the Net. I had to include a PHP script that I wanted to have localised in a WordPress installation that uses qtranslate and this works perfectly fine.
[...] Shared Ken-Soft » Creating Multi-lingual Webpages using PHP (part 1). [...]
Glad it’s useful! Btw, I cleaned it up a bit today. so please check it for any changes
[...] ニューラルネット(神経回路網・誤差逆伝播法)PDF [...]