Internationalization in PHP: PmWiki’s approach Dr. Patrick R. Michaud September 13, 2005
This presentation demonstrates internationalization (i18n) in PmWiki Introduction to PmWiki Implementing I18n in PmWiki Internationalization issues Unicode UTF-8 ISO CJKM
PmWiki is a wiki-based system for collaborative maintenance of websites A wiki allows editing via a web browser Uses simple markup (similar to ) Easy to create pages and links between pages
PmWiki is designed for needs of web and content administrators Easy to install and upgrade Simple configuration Skin capabilities Markup customization
PmWiki has an active user base mailing list subscribers Hundreds of sites globally 200+ “add-on” recipes 14 languages
Internationalization is often abbreviated “i18n” Internationalization 18 characters
We want to provide language-specific programmatic prompts
In PmWiki, all prompts are passed through a special formatting function $[Edit] - $[History] - $[Print] - $[Recent Changes] - $[Search] $[Page last modified on $LastModified] The $[phrase] sequence is a special marker used by PmWiki to indicate a translatable phrase
Translations for phrases are held in a global lookup table $XL = array( ‘Edit’ => ‘Editer’, ‘View’ => ‘Vue’, ‘Save’ => ‘Enregistrer’, …);
A special XL() function returns a phrase’s translation, or the phrase function XL($key) { global $XL; if (isset($XL[$key])) return $XL [$key]; return $key; } Phrases without a translation in the table are thus left in English.
Strings can be easily converted using a preg_replace() function XL($key) { global $XL; if (isset($XL[$key])) return $XL [$key]; return $key; } $str = preg_replace( '/ \\$\\[ ([^\\]]+) \\] /ex', "XL('$1'))", $str);
PmWiki allows the translation table to be loaded from wiki pages
Proper internationalization requires selecting an appropriate character set In HTML, specified in the or in tag Common choices: utf-8 (Unicode) iso (Latin-1) Best is to standardize on utf-8 if possible PHP can have difficulties with Unicode
Locales can be used for language- specific dates and times. $x = strftime(‘%c’, time); “Tue Sep 13 18:21: ” setlocale(‘fr_FR’); $x = strftime(‘%c’, time); “mar 13 sep :22:23”
Summary of i18n in PmWiki Simple translation table for phrase => translation Phrase keys as printable values Select proper encoding type and locale