M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan
Table of content 1.Why Gettext? 2.Applying to a PHP project 3.Converting text 4.Multi-language for images & a static page 5.Multi-language for Javascript files 6.Translating with parameters 7.Group translations by domain 8.Multi-language for Guess & Logged Users 9.Using POEdit 2
1. Why Gettext Encode and compress messages in binary format Support by almost popular programming languages (C, C++, Java, Python, Objective C, C#, etc) Support translating text in Singular/Plural form Powerful tool: POEdit 3
2. Applying to a PHP project Put the code below into entry PHP script require_once("locale/bootstrap.php"); Edit config.php to match your need Translated message files are laid at: locale/{langcode}/LC_MESSAGES/{domain}.po 4
3. Converting text Text in PHP scripts <?php $msg = _t(“This is a message for translation”); echo $msg; Text in HTML files 5
4. Multi-language for images & static pages With images ”/> With static pages 6
5. Multi-language for Javascript files (Pending) 7
6. Translating with parameters <?php $msg = _t(“This photo has {comments} {commentText}”, array( “{comments}”=> $totalComments, “{commentText} => _p(“comment", “comments", $totalComments) )); echo $msg; If $totalComments = 5, this will output : This photo has 5 comments 8
7. Group translations by domain To enable domain translation, do following steps: 1.Define domain translation function in function.php 2.Create translation file and enable function name as keyword for extracting text 3.Use POEdit for extracting text 9
8. Multi-language for Guess & Logged Users This library using cookie for keeping user’s preferred language Already enabled for Guess Enabling multi-language for logged users by adding following script after bootstrap.php is loaded <?php require_once("locale/bootstrap.php"); // some code for getting user’s language code putenv("LANGUAGE={$userLangCode}"); 10
9. Using POEdit Preparing your.po file Setting source paths & keywords Extracting messages Perform translation Update changes 11
Q UESTION ? 12