Sunday 6th June 2010
by Sajith M.R
If you aim to create a multi-language website using PHP + Mysql , remember the following tips.
- You can use mysql database for this purpose rather than using separate language files as usual content management system does.
- You can create a table with following structure for this purpose.Table: muli-lang
string_id:
page_id:
en:
arb:
fr: - Here string_id is the code for getting content eg: welcome_message
page_id is for saving in which page the content belongs to. eg: home_page, login_page etc
In field en, you have to enter the message in english,eg: Welcome to my website
In arb field you can enter the corresponding translation in arab. eg: مرحبا بكم في موقعي - If you need to add more languages, it is just adding one more column to this table and put all translated values.
- Write a class or functions in PHP to retrieve all the informations.
eg: function get(language_id, page_id) returns corresponding message - Use session to save lang value. For example if you switch to french, set a variable lang= fr and use this variable to decide which column is to access from multi-lang table.eg: the sql will be , SELECT $lang FROM multi-lang WHERE string_id = ‘welcome_message’
- For some string fragment like about_us, contact_us etc, it has global scope; then leave page_id as blank
- To avoid multiple calling of functions to get translation, write a common function to get all translation for a particular page_id as array, and use this array all through the page. Thus you can avoid mysql query multiple times.
eg: function get_all($page_id) returns array, say $Translate of all strings replacement. $Translate['login_message'], $Translate['login_error'] - Additional things to remember:
- Use this meta tab in html header
<meta http-equiv=”Content-Type” content=”text /html; charset=UTF-8” />
to show all languages - Create my_sql table in UTF-8 format
- Execute the following query before executing any retrieval queries. Otherwise you may get ????? for language like Arabic, Hindi, Chinese etc.
mysql_query(“SET CHARACTER SET ‘utf8′”, $link);
I hope these information may help you when you develop a multi-langauge support website
Thank you
Sajith
Tags: arab, engilsh, featured, french, hindi, multi, multi-language, multi-langugage support, php, support
I am learning the PHP right now,and it is useful to me,thank you very much.