Articles

The month of PHP functions : localization

  • Ecrit par Rodolphe Eveilleau
Image pour le titre du contenu

Ce document est aussi disponible en français fr 


Easy localization with setlocale()

There are times  in a Web application where you need to localize some information, such as dates, financial sums and others data.

As for dates, the often solution is to rely on PHP arrays, to gather all names for days and months.

As for financial numbers, there is number_format() or the ugly hack of replacing point by commas with str_replace() .
Forget all about them : the setlocale() function is here to save the day. This function will automatically adapt your application to local customs.
16600 16745 16764 16766 Example

In this example, you'll find the application is being configured in French. There are also several international codes to learn, as it depends on the plat-form :  Microsoft Windows will request fra or france and for GNU Linux, it will be fr or fr_FR.

<?php
    /**
     * Localize all application for France
     */
    $locale = setlocale(LC_ALL, 'fr_FR@euro', 'fr_FR', 'fra');
   
   
    /**
     * Localize only the dates
     */
    $locale = setlocale(LC_TIME, 'fr_FR', 'fra'); // French
    print strftime('%B', mktime(0,0,0,1,1,2007));
    $locale = setlocale(LC_TIME, 'de_DE');  // German
    print strftime('%B', mktime(0,0,0,1,1,2007));
    $locale = setlocale(LC_TIME, 'it_IT');  // Italien
    print strftime('%B', mktime(0,0,0,1,1,2007));
    $locale = setlocale(LC_TIME, 'zh_TW'); // Chinese
    print strftime('%B', mktime(0,0,0,1,1,2007));
   
    /**
     * Localize the whole application and fix encoding
     */
    $locale = setlocale(LC_ALL, 'fr_FR.utf-8', 'fr_FR.iso-8859-15');
?>

This script will display 

janvier
Januar
Gennaio
1&#26376;

Example: localize the application depending on the browser specification

In the following example, the whole application is localized depending on the configuration of the browser. In the $locale variable, we'll have the locale value that will be finally used by the server
<?php
    /**
     * Default accepted langues
     */
    $myLanguages = array('fr_FR', 'en_US');
    /**
     * Reading the expected languages from the browser
     */
    $acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
   
    /**
     * Analyzing the query and finding the appropriate language
     */
    if (preg_match_all('`[a-z]{2}-[a-z]{2}`', $acceptLanguage, $languages)) {
        $myLanguages = <a href="http://www.php.net/array">array()</a>;
        foreach ($languages[0] as $language) {
            list($prefix, $sufix) = explode('-', $language);
            $myLanguages[] = $prefix . '_' . strtoupper($sufix);
        }
    }
    /**
     * Configuring the application
     */
    $locale = setlocale(LC_ALL, $myLanguages);
?>
 setlocale() impacted functions Remember
  • setlocale() is available since PHP 4.
  • setlocale() return FALSE when locale is not available
  • Locale codes depends on the system
  • setlocale() is also used for comparing strings : this is useful for French accent, Spanish tilde, German Umlaut, etc.
  • setlocale() uses environnement default values
  • Unix command : locale -a display the list of all available languages

Commentaires

Vous pouvez ajouter votre commentaire!


Vous devez vous connecter pour commenter