Multi-language support with PHP

multi-language-interface

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

8 Comments , , , , , , , , ,

8 Responses to “Multi-language support with PHP”

  1. christian louboutin June 7, 2010 at 6:58 am #

    I am learning the PHP right now,and it is useful to me,thank you very much.

  2. martin jacob September 22, 2010 at 2:51 pm #

    is there any pluggins in fire fox for language conversion

  3. Sriram October 5, 2010 at 12:28 pm #

    Actually this is not multi language support in PHP. Because it is depends on browsers and mysql. This will mislead people who searching for PHP will support multi language or not? Actually core PHP won’t. You can’t do string manipulations over other languages easily. PHP 6 is going to support it.

  4. Vanu December 3, 2010 at 8:18 pm #

    ΑΝΑΣΤΑΣΙΟΣ type of output comes, Do u ve ne suggestion fr this?

  5. Zack Lee December 6, 2011 at 2:26 pm #

    Hi Sajith..I am looking for a programmer to do some customization work on a Groupon Clone script, if you interested, please email me at the above address. Thanks!

  6. aneeq April 5, 2012 at 12:47 pm #

    The following link has an excellent explanation of how to enable Arabic, Urdu and other foreign language support in PHP/MySQL.

    addr.pk/a44d

    OR

    phphelp.co/2012/03/30/how-to-make-php-and-mysql-support-arabic-urdu-and-charactersets-or-other-foreign-languages/

  7. Suneel April 6, 2012 at 6:51 pm #

    Sajith, Thanks for such a detailed explanation. I was just wondering if you can provide an example in a zipped form to play with the code. I am not so good in PHP and I find this a little difficult to put into a real life application.

    Thanks for giving directions.

Leave a Reply

More in general, php (26 of 134 articles)


A very interesting presentation I got from  one of my friend last week. I found somebody uploaded that in slideshare. ...