views:

42

answers:

1

Babel or pybabel is an interface to the CLDR (Common Locale Data Repository) in Python. As such, it has the same 'knowledge' as PHP's i18n functions and classes (if the appropriate locales are installed on the host), but without the hassle of using process-wide settings like setlocale().

Is there a similar library or toolkit for PHP? Especially to achieve:

  1. converting numbers to and fro language and region specific formats

  2. converting dates likewise

  3. accessing names, monetary and other information in a certain locale (like, e.g.

    >>> from babel import Locale
    >>> locale = Locale('en', 'US')
    >>> locale.territories['US']
    u'United States'
    >>> locale = Locale('es', 'MX')
    >>> locale.territories['US']
    u'Estados Unidos'
    
+2  A: 

PHP 5.3 comes with intl extension:

Internationalization extension (further is referred as Intl) is a wrapper for ICU library, enabling PHP programmers to perform UCA-conformant collation and date/time/number/currency formatting in their scripts.

  1. Converting numbers is possible with NumberFormatter class:

    $fmt = new NumberFormatter("de_DE", NumberFormatter::DECIMAL);
    echo $fmt->format(1234567.891234567890000);
    
  2. Converting dates is possible with IntlDateFormatter class:

    $fmt = new IntlDateFormatter("en_US", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/Los_Angeles', IntlDateFormatter::GREGORIAN);
    echo $fmt->format(0);
    
  3. Accessing names, monetary and other information in a certain locale is possible with Locale class:

    echo Locale::getRegion('de-CH-1901');
    

In addition, there are Collation and MessageFormatter classes.

Sagi