Zend Framework comes with an initial set of helper classes related to Internationalization: e.g., formatting a date, formatting currency, or displaying translated content. You can use helper, or plugin, classes to perform these behaviors for you.
See the section on view helpers for more information.
The CurrencyFormat view helper can be used to simplify rendering of localized currency values. It acts as a wrapper for the NumberFormatter class within the Internationalization extension (Intl).
Basic Usage of CurrencyFormat
1 2 3 4 5 6 7 | // Within your view
echo $this->currencyFormat(1234.56, "USD", "en_US");
// This returns: "$1,234.56"
echo $this->currencyFormat(1234.56, "EUR", "de_DE");
// This returns: "1.234,56 €"
|
currencyFormat(float $number , string $currencyCode [, string $locale ])
CurrencyFormat Setters
The $currencyCode and $locale options can be set prior to formatting and will be applied each time the helper is used:
1 2 3 4 5 | // Within your view
$this->plugin("currencyformat")->setCurrencyCode("USD")->setLocale("en_US");
echo $this->currencyFormat(1234.56); // "$1,234.56"
echo $this->currencyFormat(5678.90); // "$5,678.90"
|
The DateFormat view helper can be used to simplify rendering of localized date/time values. It acts as a wrapper for the IntlDateFormatter class within the Internationalization extension (Intl).
Basic Usage of DateFormat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | // Within your view
// Date and Time
echo $this->dateFormat(
new DateTime(),
IntlDateFormatter::MEDIUM, // date
IntlDateFormatter::MEDIUM, // time
"en_US"
);
// This returns: "Jul 2, 2012 6:44:03 PM"
// Date Only
echo $this->dateFormat(
new DateTime(),
IntlDateFormatter::LONG, // date
IntlDateFormatter::NONE, // time
"en_US"
);
// This returns: "July 2, 2012"
// Time Only
echo $this->dateFormat(
new DateTime(),
IntlDateFormatter::NONE, // date
IntlDateFormatter::SHORT, // time
"en_US"
);
// This returns: "6:44 PM"
|
dateFormat(mixed $date [, int $dateType [, int $timeType [, string $locale ]]])
DateFormat Setters
The $locale option can be set prior to formatting with the setLocale() method and will be applied each time the helper is used.
By default, the system’s default timezone will be used when formatting. This overrides any timezone that may be set inside a DateTime object. To change the timezone when formatting, use the setTimezone method.
1 2 3 4 5 | // Within your view
$this->plugin("dateFormat")->setTimezone("America/New_York")->setLocale("en_US");
echo $this->dateFormat(new DateTime(), IntlDateFormatter::MEDIUM); // "Jul 2, 2012"
echo $this->dateFormat(new DateTime(), IntlDateFormatter::SHORT); // "7/2/12"
|
The NumberFormat view helper can be used to simplify rendering of locale-specific number and percentage strings. It acts as a wrapper for the NumberFormatter class within the Internationalization extension (Intl).
Basic Usage of NumberFormat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | // Within your view
// Example of Decimal formatting:
echo $this->numberFormat(
1234567.891234567890000,
NumberFormatter::DECIMAL,
NumberFormatter::TYPE_DEFAULT,
"de_DE"
);
// This returns: "1.234.567,891"
// Example of Percent formatting:
echo $this->numberFormat(
0.80,
NumberFormatter::PERCENT,
NumberFormatter::TYPE_DEFAULT,
"en_US"
);
// This returns: "80%"
// Example of Scientific notation formatting:
echo $this->numberFormat(
0.00123456789,
NumberFormatter::SCIENTIFIC,
NumberFormatter::TYPE_DEFAULT,
"fr_FR"
);
// This returns: "1,23456789E-3"
|
numberFormat(number $number [, int $formatStyle [, int $formatType [, string $locale ]]])
NumberFormat Setters
The $formatStyle, $formatType, and $locale options can be set prior to formatting and will be applied each time the helper is used.
1 2 3 4 5 6 7 8 | // Within your view
$this->plugin("numberformat")
->setFormatStyle(NumberFormatter::PERCENT)
->setFormatType(NumberFormatter::TYPE_DOUBLE)
->setLocale("en_US");
echo $this->numberFormat(0.56); // "56%"
echo $this->numberFormat(0.90); // "90%"
|
The Translate view helper can be used to translate content. It acts as a wrapper for the Zend\I18n\Translator\Translator class.
Translate Setup
Before using the Translate view helper, you must have first created a Translator object and have attached it to the view helper. If you use the Zend\I18n\Translator\TranslatorServiceFactory to create your Translator object, this will be done automatically for you.
If you are not using the TranslatorServiceFactory, then you will need to manually attach your Translator object, such as:
1 2 | // Somewhere early in the process...
$serviceLocator->get('ViewHelperManager')->get('translate')->setTranslator($translator);
|
Basic Usage of Translate
1 2 3 4 5 6 7 8 9 | // Within your view
echo $this->translate("Some translated text.");
echo $this->translate("Translated text from a custom text domain.", "customDomain");
echo sprintf($this->translate("The current time is %s."), $currentTime);
echo $this->translate("Translate in a specific locale", "default", "de_DE");
|
translate(string $message [, string $textDomain [, string $locale ]])
The TranslatePlural view helper can be used to translate words which take into account numeric meanings. English, for example, has a singular definition of “car”, for one car. And has the plural definition, “cars”, meaning zero “cars” or more than one car. Other languages like Russian or Polish have more plurals with different rules.
The viewhelper acts as a wrapper for the Zend\I18n\Translator\Translator class.
TranslatePlural Setup
Before using the TranslatePlural view helper, you must have first created a Translator object and have attached it to the view helper. If you use the Zend\I18n\Translator\TranslatorServiceFactory to create your Translator object, this will be done automatically for you.
If you are not using the TranslatorServiceFactory, then you will need to manually attach your Translator object, such as:
1 2 | // Somewhere early in the process...
$serviceLocator->get('ViewHelperManager')->get('translateplural')->setTranslator($translator);
|
Basic Usage of TranslatePlural
1 2 3 4 5 6 7 8 | // Within your view
echo $this->translatePlural("car", "cars", $num);
// Use a custom domain
echo $this->translatePlural("monitor", "monitors", $num, "customDomain");
// Change locale
echo $this->translate("locale", "locales", $num, "default", "de_DE");
|
translatePlural(string $singular, string $plural, int $number [, string $textDomain [, string $locale ]])
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.