RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Base Concepts of Internationalization in PHP

Discover how to use internationalized numbers, currencies and dates/times with the I18N PEAR package.

f you develop Web applications that have an international target audience, then you have to take internationalization into account—a process that includes avoiding date/time or currency confusions and delivering all text pertinent to the user interface in the user's preferred language. Applications that can grow international traffic and improve revenue must respect their clients' needs.

PHP comes with a set of PEARs that support internationalization from several different angles and levels of complexity. The I18N PEAR presented in this article offers the core internationalization support for your PHP applications. You will see how to:

  • Download and install the I18N PEAR
  • Internationalize numbers
  • Internationalize currencies
  • Internationalize dates and times
A Brief Introduction to Internationalization for Web Applications
Internationalization is a programming technique that you use to adapt an application for use in an international context. In an internationalized application, important information, such as currency values, numbers, times and dates, specific symbols, native characters, directions, and so on, get rendered in the format and language common to each user's expectations. Internationalized applications try to detect the user's locale (the user's country and preferred language) by querying the user's browser, which passes a combined "language and country code" that uses ISO-standardized values to specify the "default" country and language. For example, people in the United States who prefer English typically have their browsers set to pass an en_US (English, United States) code, while French-speaking people in France typically have their browser set to pass the code fr_FR.

Browsers pass the two values with each request as a single string concatenated with an underscore, as shown in the previous examples. Applications are responsible for splitting the string into the separate language and country codes. After doing that, the application can use the codes to apply a format corresponding to that request's language and country codes.

Alternatively, internationalized applications offer users the ability to choose their locale—often by placing a set of flags somewhere on the page that reflect the countries for which specifically formatted or translated information is available, or providing a list of countries and languages from which users can select a preference.

One way to achieve internationalization is to store locale-dependent resources in external files that contain the pre-formatted or translated resources—one for each supported locale—and then have the application load resources from the appropriate file based on either the user's browser locale settings, or the user's locale selection. However, when language is not a problem, and the application simply needs to show currency values, numbers, dates, and times in a locale-specific way, developers can use a simpler internationalization technique, storing a single "standard" value, and passing that value, along with the locale, to a library that returns a version formatted appropriately for the specified locale.

This article explains the latter approach. In PHP, you can use the I18N PEAR library to format locale-dependent values.

Download and Install the I18N PEAR Package
You can download and install the I18N PEAR as follows (note that the latest beta release of this package is 0.8.6):

   >> pear install I18N-0.8.6
Class Trees for Package I18N
This PEAR 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 rest of this article explains how to use these classes.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date