Globalize your Web Applications: PHP's Locale Formatting Classes | WebReference

Globalize your Web Applications: PHP's Locale Formatting Classes

By Rob Gravelle


In programming, a locale is a set of parameters that define attributes for a user's specific geographic location, including the user's language, country and any special variant preferences that the user wants to see in his/her user interface. In Part 1 of the Globalize your Web Applications series, we examined what the locale entails, how they are supported by different operating systems, and we looked at some specific implementations in PHP. The Part 2 article continued with the PHP language as we explored how to make your dates locale aware, using both the native datetime functions as well as the I18N_DateTime class. Today, we'll be covering PHP's two remaining I18N Libraries, the I18N_Number and I18N_Currency classes, both of which are subclasses of the I18N_DateTime function.

Formatting numbers

Just as dates and times can vary greatly depending on the country, numbers are also written differently in different languages. For instance, the number 123456.789 would be written as 123 456,789 in German. Here are some LCIDs and their associated number formats for various languages and countries:

Float format:

  • Spanish - Spain (es_ES): 123.456,000
  • Dutch - Netherlands (nl_NL): 123 456,000
  • German - Germany (de_DE): 123.456,000
  • French - France (fr_FR): 123 456,000
  • Italian - Italy (it_IT): 123.456,000
  • English - United States (en_US): 123,456.000

Integer format:

  • Spanish - Spain (es_ES): 123.456
  • Dutch - Netherlands (nl_NL): 123 456
  • German - Germany (de_DE): 123.456
  • French - France (fr_FR): 123 456
  • Italian - Italy (it_IT): 123.456
  • English - United States (en_US): 123,456

The I18N_Number Class

The purpose of the I18N_Number class is to format a number to either a default or pre-defined format. It's main function is:

  • string format( mixed $number, [mixed $format = null]): Formats a given number depending on the locale.

The $number parameter is required and is the number to be formatted. The optional $format argument accepts a specified format to use. It can be one of the values: I18N_NUMBER_FLOAT, I18N_NUMBER_INTEGER or a custom format created using the setFormat() method described next. The default value is I18N_NUMBER_FLOAT if the $format argument is missing. The format() function returns the formatted date as a string.

The class inherits two functions from its I18N_Format parent:

  • int setFormat(string $format): Defines a custom format according to the $format argument. Returns the $format ID, which can be used to call format( $number , $format-id ) to tell the method you want to use the format with that ID.
  • void getFormat(): Returns a format identifier that can be passed to the format() method for specifying the number's format type.

Here's an example that demonstrates the usage of the above functions:

Currency Formatting for Different Countries

As anyone who has ever traveled abroad can tell you, adapting to another country's currency can be a challenge. Similarly, programming for different locales requires careful attention to regional differences in both currency used and the formatting. Some of the main factors that one needs to take into consideration when formatting currency include the following:

  • Currency symbol: This can be a pre-defined symbol like the European Euro '€' or a combination of letters like the use of 'GBP' for British Pound.
  • Currency symbol placement: It can be be placed either before or after the digits.
  • Negative-amount display: The minus symbol can be placed either before or after the digits or parentheses may be used instead.

The following chart shows various currency formats for positive and negative numbers:

Country Positive Number Formatting Negative Number Formatting
UK £123.45 -£123.45
Denmark kr 123,45 kr-123,45
France € 123,45 € 123,45
US $123.45 ($123.45)


The I18N_Currency Class

Luckily, you don't have to spend a lot of time dwelling on formatting details. PHP's I18N_Currency class handles the specifics for the locale string that you pass to the class constructor.

The I18N_Currency class also implements its I18N_Number parent class's format() function:

  • string format( mixed $amount, [mixed $format = I18N_CURRENCY_LOCAL]): Returns the $amount formatted according to the specified format, which can be either I18N_CURRENCY_LOCAL (a value of 1), or I18N_CURRENCY_INTERNATIONAL (a value of 2), or can take a custom format created using the setFormat() function described earlier. If the $format argument is missing, the default value is I18N_CURRENCY_LOCAL.

Here's an example that displays information about several currencies: