Globalize your Web Applications: PHP's Locale Package / Page 2 | WebReference

Globalize your Web Applications: PHP's Locale Package / Page 2


[prev]

Globalize your Web Applications: PHP's Locale Package [con't]

The PHP I18N Libraries

PHP comes with a set of PEARs that support internationalization from several different angles and levels of complexity. PEAR stands for PHP Extension and Application Repository, which is a framework and distribution system for reusable PHP components. The package is named using the abbreviation for Internationalization that we saw above. The I18N library comes with the following class trees:

Root class I18N_Common
    I18N_Common
    I18N_Country
        I18N_Language
Root class I18N_Format
    I18N_Format
    I18N_DateTime
        I18N_Number
            I18N_Currency


The following section covers each class in more detail.

I18N_Country

The I18N_Country class has a couple of functions to help determine the country name from a country code:

  • void getName([: $code = '']): Returns the name of the country using the country code passed by the string argument $code.
  • void isValidCode(mixed $code): Check whether the country code passed by the $code argument is valid.

Here's a simple example that shows how to use the above methods. The following code accepts a two letter country code from the user and prints the country name for a valid code:



I18N_Language

The I18N_Language class also implements the getName() method:

  • void getName([: $code = ''], : 1)

It returns the name of the language using the language code passed by the string argument $code. The 1 parameter represents the string language code.

Here's a simple script that uses the I18N_Language class to display an HTML-formatted list of several languages:

The above code produces the following in a browser:

Language codes

  • en: English
  • fr: French
  • es: Spanish
  • af: Afrikaans
  • da: Danish
  • de: German

That brings us to the end of part 1. In the next installment, we'll conclude our look at PHP I18N package and move on to our next language.


Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. In 2007, Rob recorded the KNIGHTFALL CD in collaboration with the former Ivory Knight vocalist and legendary guitarist/producer, Jeff Waters of Annihilator fame. A completely FREE high quality MP3 download of his "Ultraviolence" intrumental, is availalable from his website, www.robgravelle.com. Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: December 7, 2009


[prev]