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
stringid:
pageid:
en:
arb:
fr: - Here stringid is the code for getting content eg: welcomemessage
pageid is for saving in which page the content belongs to. eg: homepage, 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(languageid, pageid) 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 stringid = ‘welcomemessage’
- For some string fragment like aboutus, contactus 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 pageid as array, and use this array all through the page. Thus you can avoid mysql query multiple times.
eg: function getall($pageid) returns array, say $Translate of all strings replacement. $Translate[‘loginmessage’], $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