m ulti - language for php with g ettext binh quan 2014-05-20
TRANSCRIPT
![Page 1: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/1.jpg)
MULTI-LANGUAGE FOR PHP WITH GETTEXT
Binh Quan2014-05-20
![Page 2: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/2.jpg)
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: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/3.jpg)
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: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/4.jpg)
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: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/5.jpg)
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: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/6.jpg)
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: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/7.jpg)
Page 7
5. Multi-language for Javascript files
(Pending)
![Page 8: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/8.jpg)
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: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/9.jpg)
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: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/10.jpg)
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: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/11.jpg)
Page 11
9. Using POEdit
• Preparing your .po file• Setting source paths & keywords• Extracting messages• Perform translation• Update changes
![Page 12: M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20](https://reader036.vdocuments.net/reader036/viewer/2022081519/56649e245503460f94b1231a/html5/thumbnails/12.jpg)
12
QUESTION?