Upload
binh-quan
View
952
Download
3
Tags:
Embed Size (px)
DESCRIPTION
A short presentation on how to integrate small library for supporting multi-language in a PHP project
Citation preview
MULTI-LANGUAGE FOR PHP WITH GETTEXT
Binh Quan2014-05-20
Page 2
Table of content1. Why Gettext? 2. Applying to a PHP project3. Converting text4. Multi-language for images & a static page5. Multi-language for Javascript files6. Translating with parameters7. Group translations by domain8. Multi-language for Guess & Logged Users9. Using POEdit
Page 3
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
Page 4
2. Applying to a PHP project
• Put the code below into entry PHP scriptrequire_once("locale/bootstrap.php");
• Edit config.php to match your need
• Translated message files are laid at:locale/{langcode}/LC_MESSAGES/{domain}.po
Page 5
3. Converting text
• Text in PHP scripts<?php
$msg = _t(“This is a message for translation”);
echo $msg;
• Text in HTML files<p>
<span><?php echo _t(“This is a span text”);?></span></p>
Page 6
4. Multi-language for images & static pages
• With images<img src=“/path/to/image/<?php echo _t(“banner.png”);?>”/>
• With static pages<h2><?php echo _t(“My CMS Page”);?></h2>
<?php include(_t(“cms_page.phtml”));?>
Page 7
5. Multi-language for Javascript files
(Pending)
Page 8
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
Page 9
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
Page 10
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
<?phprequire_once("locale/bootstrap.php");
// some code for getting user’s language code
putenv("LANGUAGE={$userLangCode}");
Page 11
9. Using POEdit
• Preparing your .po file• Setting source paths & keywords• Extracting messages• Perform translation• Update changes
12
QUESTION?