internationalisation with php and intl

Download Internationalisation with PHP and Intl

If you can't read please download the document

Upload: danielrhodes

Post on 24-May-2015

10.214 views

Category:

Technology


5 download

DESCRIPTION

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

TRANSCRIPT

  • 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. What is the Intl extension?

  • A wrapper for ICU International Components for Unicode (http://site.icu-project.org)

3. Let's us...

    • Compare and sort strings
  • 4. Format numbers, times and dates

5. Format text

  • ... as appropriate for a given locale!

6. See PHP docs athttp://www.php.net/manual/en/book.intl.php 7. Example locale differences

  • Here are some example locale differences that we might like to tackle:
  • Japan'sofficialcalendar counts years by using emperor's era name followed by year number of that emperor's reign. 2011 is Heisei 23 for example.

8. Korean language system of counting in tens of thousands. 10,000 is one man for example. 9. German alphabet with its umlauted vowels (, and ) and eszett () 10. How to get the extension

  • A wrapper for ICU so you'll need that first!

11. Bundled extension as of PHP 5.3.0 (compile freaks will want --enable-intl) 12. PHP 5.2.0 onwards can get the extension from PECL 13. ( http://www.php.net/manual/en/install.pecl.php ) 14. Check extension presence

  • phpinfo() will look something like:

15. First play and test 16. Functionality overview

  • Sort and compare strings (Collator class)

17. Format currency amounts and other numbers (NumberFormatter class) 18. Locale getting, setting and information (Locale class) 19. Format natural language sentences (MessageFormatter class) 20. Format dates (IntlDateFormatter class) 21. And much more!(Transliterator class not yet implemented) 22. Case study sorting German

  • More than one native way to sort!

23. Dictionary ,phonebookandAustriansort orders 24. Seehttp://de.wikipedia.org/wiki/Alphabetische_Sortierung 25. Basically English A to Z order but with...

  • Where do the umlauted vowels go?

26. Where doeseszett go? ...to answer 27. Case study sorting German

  • Let's try plain vanilla PHP first:

28. Case study sorting German

  • With sort() we seem to coincidentally (?!) be getting the Austrian phonebook sort order

29. What about the other two sort orders? 30. Well, sort() supports system locales... 31. ...so let's try that! 32. Case study sorting German 33. Case study sorting German

  • System locale of de_DE.utf8,if present! , gives us dictionary sort order

34. Setlocale() [and fgetcsv(), nl_langinfo(), localeconv() etc] needs system locale installed on OS 35. But system locale can be spelled differently,or be absent! , on different OS 36. And we still haven't got phonebook sort order! 37. A job for Intl... 38. Case study sorting German 39. Case study sorting German

  • ICU locales and system locales are different and independent:
  • ICU locales don't need to be installed on the OS

40. ICU locales do not specify encoding (as ICU is for Unicode) 41. ICU locales specify variant locales differently 42. ICU locales support UCA keywords (eg. numbers=roman) 43. Challenge for you!

  • 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:
  • The collation string passed to Collator::__construct()

44. Collator::setAttribute() 45. Case study Japanese era

  • Gory details athttp://en.wikipedia.org/wiki/Japanese_era

46. Basically the current emperor's reign is named Heisei and 2011 is Heisei 23 as it's the 23 rdyear of the current emperor's reign 47. Month and day same as Gregorian 48. Used for more official paperwork and formal situations (Gregorian year used day-to-day) 49. Let's see how Intl can present this format for a Japanese locale... 50. Case study Japanese era 51. Case study Japanese era

  • Works out of the box!

52. Because calendar=japanese supported by UCA 53. Give IntlDateFormatter::__construct()[email_address] =uca_calendar_typeandIntlDateFormatter::TRADITIONALto get non-Gregorian calendars 54. Challenge for you!

  • Ignoring the fact that they refer to months of the Chinese lunar calendar, can you get IntlDateFormatter to output dates for ja_JP using thetraditionalmonth names (see http://en.wikipedia.org/wiki/Japanese_calendar)? Try experimenting with:
  • IntlDateFormatter::getPattern()

55. IntlDateFormatter::setPattern() 56. Case study Korean numbers

  • Let's try to spell out numbers in Korean

57. Korean equivalent of the newspaper style four million, two hundred thousand and forty-seven 58. So need Korean words / characters for numbers 59. BUT Korean counts in ten thousands 60. One, ten, hundred, thousand, man (ten thousand), eok (100 million ie. Ten thousand man), jo (one trillion ie. Ten thousand eok) 61. See http://en.wikipedia.org/wiki/Korean_numerals 62. Case study Korean numbers 63. Case study Korean numbers

  • Korean locale is supported but with no number spellout rules

64. We have to create, and then set, our own spellout rules 65. Luckily we can base our rules on the already present Japanese spellout rules! 66. Japanese also counts in ten thousands and uses non-Arabic numerals 67. Case study Korean numbers

  • Our rules:

68. Case study Korean numbers 69. Challenge for you!

  • Can you extend the Korean spellout rules to support the following?:
  • Hangul as used in the North

70. Hanja 71. Financial / anti-forgery Hanja 72. (See http://en.wikipedia.org/wiki/Korean_numerals) 73. Summary

  • Not every bit of every locale works out of the box

74. But we can usually set our own rules 75. Don't need to install locales on our OS 76. More flexible and fine-grained thansystemlocales (ie. setlocale() and money_format() etc) 77. NumberFormatter class also supports durations 78. Resource bundles for locale-specific messages, patterns etc supported with ResourceBundle class 79. Links

  • Should be able to get a PHP source code pack from wherever you got this presentation

80. http://site.icu-project.org 81. http://www.php.net/manual/en/intro.intl.php 82. http://de.wikipedia.org/wiki/Alphabetische_Sortierung 83. http://en.wikipedia.org/wiki/Japanese_era 84. http://en.wikipedia.org/wiki/Korean_numerals