Download presentation
Presentation is loading. Please wait.
Published byEmily Weaver Modified over 9 years ago
1
M ULTI - LANGUAGE FOR PHP WITH G ETTEXT Binh Quan 2014-05-20
2
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
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 3
4
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
5
3. Converting text Text in PHP scripts <?php $msg = _t(“This is a message for translation”); echo $msg; Text in HTML files 5
6
4. Multi-language for images & static pages With images ”/> With static pages 6
7
5. Multi-language for Javascript files (Pending) 7
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 8
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 9
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 <?php require_once("locale/bootstrap.php"); // some code for getting user’s language code putenv("LANGUAGE={$userLangCode}"); 10
11
9. Using POEdit Preparing your.po file Setting source paths & keywords Extracting messages Perform translation Update changes 11
12
Q UESTION ? 12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.