Friday, December 21, 2012

Zend framework 2 Locale / i18n

In Application/config/module.config.php:

return array(
//...,
'translator' => array(
        'locale' => 'en_GB',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
//...


In Application/Module.php bootstrap:

//set your time zone here

date_default_timezone_set("Asia/Kuala_Lumpur");
$oViewHelperManager = $e->getApplication()->getServiceManager()->get("ViewHelperManager");
$oViewHelperManager->get("dateFormat")>setLocale($sLocale)->setTimezone(date_default_timezone_get());
$oViewHelperManager->get("numberFormat")->setLocale($sLocale);
$oViewHelperManager->get("currencyFormat")->setLocale($sLocale)->setCurrencyCode("MYR");

To output in view template:
echo $this->translate("My text here");
echo $this->dateFormat(new DateTime(), IntlDateFormatter::SHORT);
echo $this->numberFormat(12345.0123, NumberFormatter::DECIMAL);
//default currency
echo $this->numberFormat(12345.5226, NumberFormatter::CURRENCY);

//custom currency output:
echo $this->currencyFormat(1234.5226, "MYR");


Requires:
  • php5-intl

No comments: