How to use the internationalization (intl) extension in PHP

This article describes how to use the internationalization (intl) extension in PHP to process and display locale-specific information.

Using the internationalization extension

A2 Hosting's shared servers include support for PHP's internationalization extension, which is named intl. This extension enables you to specify a locale for your PHP applications. A locale is simply is a collection of the following region-specific settings:

  • The alphabetical order used to sort text strings.
  • Character classification and conversion, such as between uppercase and lowercase letters.
  • Currency formatting.
  • Decimal separator.
  • Date and time formatting.

To view a list of locales available on your server, log in to your A2 Hosting account using SSH, and then type the following command:

locale -a

Most locales are in the format xx_YY, where xx is a two-letter designation for the language, and YY is a two-letter designation for a country or region. Some locales also have an additional identifier that indicates the character set. For example, en_US represents English in the United States, while zh_TW.big5 represents Chinese in Taiwan using the Big5 character set.

The following code example demonstrates how to set locales using the setlocale() function. It uses an HTML table to display the current locale setting, as well as the date, time, and a number represented in the current locale:

<html>
<head><title>PHP intl example</title></head>
<body>
<table>
<tr><th>Encoding</th><th>Date/time</th><th>Number</th></tr>

<?php

    // Set the locale to French in France:
    setlocale( LC_ALL, 'fr_FR' );
    printRow();

    // Set the locale to English in the United States:
    setlocale( LC_ALL, 'en_US' );
    printRow();

    // This function generates a table row that displays various text using the current locale setting:
    function printRow()
    {
        $number = 133.52;

        print "<tr>";
        print "<td>" . setlocale( LC_ALL, 0 ) . "</td>";    // Calling setlocale() with locale '0' returns the current locale
        print "<td>" . strftime( '%c' ) . "</td>";
        print "<td>" . $number . "</td>";
        print "</tr>";
    }
?>

</table>
</body>
</html>

If you run this example, you see the date and time format displayed differently for each locale. Similarly, the $number variable is displayed differently: in French and Russian, the decimal separator is a comma (133,52), whereas in English it is a period (133.52).

Depending on the locale, you may also have to change the page's character encoding set to ensure that text displays correctly. In the example above, the Cyrillic characters may not display correctly when the locale is set to Russian. One way to fix this is to specify the character set directly in the HTTP header as follows:

<?php
    header('Content-Type: text/html;charset=iso-8859-5');
?>

More Information

For more information about the setlocale() function, please visit http://php.net/manual/en/function.setlocale.php.

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.