Internationalisation with PHP and Intl

11,522 views
11,238 views

Published on

Internationalisation with PHP and the Intl extension. Featuring source code and three real world case studies.

Published in: Technology
7 Comments
6 Likes
Statistics
Notes
  • Just to let you all know that this slideshow is quite old now so please check http://www.php.net/manual/en/book.intl.php for the current state of play with PHP's Intl. (It looks like it's a bit more complete now than when it was at the time of making the slideshow).
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • @ShellyWarren1 Glad you like it! I actually learned this stuff - the hard way - not long before writing the slides so it was all 'fresh'. Annoyingly I've never had the chance to use this in production PHP - so you could say the slides are unpolluted by real world concerns! http://www.php.net/manual/en/book.intl.php and http://site.icu-project.org are great practical and theoretical resources (respectively).
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • @kehalim Great question! I've had a look (http://www.php.net/manual/en/class.locale.php) and I don't see and way to list supported locales. I think the idea is it's *supposed* to support EVERY language and locale. But as you can see from the slides some bits of some locales are missing. Perhaps whole locales are missing? If so, there is then the issue of is it missing in ICU itself or just in PHP's Intl library? (Rambling answer sorry!)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Thank You!! The best and quickest explanation, I have spent 2 days reading various posts to teach myself this stuff and boy do I wish I found this sooner!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Nice slide. any way to retrieve all supported regions/languages with php intl?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
11,522
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
40
Comments
7
Likes
6
Embeds 0
No embeds

No notes for slide

Internationalisation with PHP and Intl

  1. 1. Internationalisation with PHP This presentation is about the “Intl” extension for PHP. By Daniel Rhodes of Warp Asylum http://www.warpasylum.co.uk
  2. 2. What is the “Intl” extension? <ul><li>A wrapper for ICU – International Components for Unicode (http://site.icu-project.org)
  3. 3. Let's us... </li></ul><ul><ul><li>Compare and sort strings
  4. 4. Format numbers, times and dates
  5. 5. Format text </li></ul></ul><ul><li>... as appropriate for a given locale!
  6. 6. See PHP docs at http://www.php.net/manual/en/book.intl.php </li></ul>
  7. 7. Example locale differences <ul>Here are some example locale differences that we might like to tackle: <li>Japan's official calendar counts years by using emperor's era name followed by year number of that emperor's reign. 2011 is “Heisei 23” for example.
  8. 8. Korean language system of counting in tens of thousands. 10,000 is “one man” for example.
  9. 9. German alphabet with its umlauted vowels (ä, ö and ü) and eszett (ß) </li></ul>
  10. 10. How to get the extension <ul><li>A wrapper for ICU so you'll need that first!
  11. 11. Bundled extension as of PHP 5.3.0 (compile freaks will want --enable-intl)
  12. 12. PHP 5.2.0 onwards can get the extension from PECL
  13. 13. ( http://www.php.net/manual/en/install.pecl.php ) </li></ul>
  14. 14. Check extension presence <ul><li>phpinfo() will look something like: </li></ul>
  15. 15. First play and test
  16. 16. Functionality overview <ul><li>Sort and compare strings (Collator class)
  17. 17. Format currency amounts and other numbers (NumberFormatter class)
  18. 18. Locale getting, setting and information (Locale class)
  19. 19. Format natural language sentences (MessageFormatter class)
  20. 20. Format dates (IntlDateFormatter class)
  21. 21. And much more! (Transliterator class not yet implemented) </li></ul>
  22. 22. Case study – sorting German <ul><li>More than one native way to sort!
  23. 23. Dictionary , phonebook and Austrian sort orders
  24. 24. See http://de.wikipedia.org/wiki/Alphabetische_Sortierung
  25. 25. Basically English A to Z order but with... </li><ul><li>Where do the umlauted vowels go?
  26. 26. Where does eszett go? </li></ul><li>...to answer </li></ul>
  27. 27. Case study – sorting German <ul>Let's try plain vanilla PHP first: </ul>
  28. 28. Case study – sorting German <ul><li>With sort() we seem to coincidentally (?!) be getting the Austrian phonebook sort order
  29. 29. What about the other two sort orders?
  30. 30. Well, sort() supports system locales...
  31. 31. ...so let's try that! </li></ul>
  32. 32. Case study – sorting German
  33. 33. Case study – sorting German <ul><li>System locale of de_DE.utf8, if present! , gives us dictionary sort order
  34. 34. Setlocale() [and fgetcsv(), nl_langinfo(), localeconv() etc] needs system locale installed on OS
  35. 35. But system locale can be spelled differently, or be absent! , on different OS
  36. 36. And we still haven't got phonebook sort order!
  37. 37. A job for Intl... </li></ul>
  38. 38. Case study – sorting German
  39. 39. Case study – sorting German <ul>ICU locales and system locales are different and independent: <li>ICU locales don't need to be installed on the OS
  40. 40. ICU locales do not specify encoding (as ICU is for Unicode)
  41. 41. ICU locales specify variant locales differently
  42. 42. ICU locales support UCA keywords (eg. numbers=roman) </li></ul>
  43. 43. Challenge for you! <ul>We've seen that Collator can quite easily give us the phonebook and dictionary sort orders for German. But can you use Collator to get the Austrian phonebook sort order? Try experimenting with: <li>The collation string passed to Collator::__construct()
  44. 44. Collator::setAttribute() </li></ul>
  45. 45. Case study – Japanese era <ul><li>Gory details at http://en.wikipedia.org/wiki/Japanese_era
  46. 46. Basically the current emperor's reign is named “Heisei” and 2011 is Heisei 23 as it's the 23 rd year of the current emperor's reign
  47. 47. Month and day same as Gregorian
  48. 48. Used for more official paperwork and formal situations (Gregorian year used day-to-day)
  49. 49. Let's see how Intl can present this format for a Japanese locale... </li></ul>
  50. 50. Case study – Japanese era
  51. 51. Case study – Japanese era <ul><li>Works out of the box!
  52. 52. Because calendar=japanese supported by UCA
  53. 53. Give IntlDateFormatter::__construct() [email_address] =uca_calendar_type and IntlDateFormatter::TRADITIONAL to get non-Gregorian calendars </li></ul>
  54. 54. Challenge for you! <ul>Ignoring the fact that they refer to months of the Chinese lunar calendar, can you get IntlDateFormatter to output dates for ja_JP using the traditional month names (see http://en.wikipedia.org/wiki/Japanese_calendar)? Try experimenting with: <li>IntlDateFormatter::getPattern()
  55. 55. IntlDateFormatter::setPattern() </li></ul>
  56. 56. Case study – Korean numbers <ul><li>Let's try to spell out numbers in Korean
  57. 57. Korean equivalent of the newspaper style “four million, two hundred thousand and forty-seven”
  58. 58. So need Korean words / characters for numbers
  59. 59. BUT Korean counts in ten thousands
  60. 60. One, ten, hundred, thousand, man (ten thousand), eok (100 million ie. Ten thousand man), jo (one trillion ie. Ten thousand eok)
  61. 61. See http://en.wikipedia.org/wiki/Korean_numerals </li></ul>
  62. 62. Case study – Korean numbers
  63. 63. Case study – Korean numbers <ul><li>Korean locale is supported but with no number spellout rules
  64. 64. We have to create, and then set, our own spellout rules
  65. 65. Luckily we can base our rules on the already present Japanese spellout rules!
  66. 66. Japanese also counts in ten thousands and uses non-Arabic numerals </li></ul>
  67. 67. Case study – Korean numbers <ul><li>Our rules: </li></ul>
  68. 68. Case study – Korean numbers
  69. 69. Challenge for you! <ul>Can you extend the Korean spellout rules to support the following?: <li>Hangul as used in the North
  70. 70. Hanja
  71. 71. Financial / anti-forgery Hanja
  72. 72. (See http://en.wikipedia.org/wiki/Korean_numerals) </li></ul>
  73. 73. Summary <ul><li>Not every bit of every locale works out of the box
  74. 74. But we can usually set our own rules
  75. 75. Don't need to install locales on our OS
  76. 76. More flexible and fine-grained than system locales (ie. setlocale() and money_format() etc)
  77. 77. NumberFormatter class also supports durations
  78. 78. Resource bundles for locale-specific messages, patterns etc supported with ResourceBundle class </li></ul>
  79. 79. Links <ul><li>Should be able to get a PHP source code pack from wherever you got this presentation
  80. 80. http://site.icu-project.org
  81. 81. http://www.php.net/manual/en/intro.intl.php
  82. 82. http://de.wikipedia.org/wiki/Alphabetische_Sortierung
  83. 83. http://en.wikipedia.org/wiki/Japanese_era
  84. 84. http://en.wikipedia.org/wiki/Korean_numerals </li></ul>

×