The Intl extensions provides the localizeddate
, localizednumber
and localizedcurrency
filters.
First, :ref:`install the Extensions library<extensions-install>`. Next, add the extension to Twig:
use Twig\Extensions\IntlExtension; $twig->addExtension(new IntlExtension());
Use the localizeddate
filter to format dates into a localized string
representating the date.
{{ post.published_at|localizeddate('medium', 'none', locale) }}
The localizeddate
filter accepts strings (it must be in a format supported
by the strtotime function), DateTime instances, or Unix timestamps.
Note
Internally, Twig uses the PHP IntlDateFormatter::create() function for the date.
date_format
: The date format. Choose one of these formats:- 'none': IntlDateFormatter::NONE
- 'short': IntlDateFormatter::SHORT
- 'medium': IntlDateFormatter::MEDIUM
- 'long': IntlDateFormatter::LONG
- 'full': IntlDateFormatter::FULL
time_format
: The time format. Same formats possible as above.locale
: The locale used for the format. IfNULL
is given, Twig will useLocale::getDefault()
timezone
: The date timezoneformat
: Optional pattern to use when formatting or parsing. Possible patterns are documented in the ICU user guide.calendar
: Calendar to use for formatting. The default value is 'gregorian', which corresponds to IntlDateFormatter::GREGORIAN. Choose one of these formats:- 'gregorian': IntlDateFormatter::GREGORIAN
- 'traditional': IntlDateFormatter::TRADITIONAL
- For the following calendars should use 'traditional':
- Japanese
- Buddhist
- Chinese
- Persian
- Indian
- Islamic
- Hebrew
- Coptic
- Ethiopic
Also for non-Gregorian calendars need to be specified in locale. Examples might include locale="fa_IR@calendar=PERSIAN".
Use the localizednumber
filter to format numbers into a localized string
representating the number.
{{ product.quantity|localizednumber }}
Note
Internally, Twig uses the PHP NumberFormatter::create() function for the number.
style
: Optional number format (default: 'decimal'). Choose one of these formats:- 'decimal': NumberFormatter::DECIMAL
- 'currency': NumberFormatter::CURRENCY
- 'percent': NumberFormatter::PERCENT
- 'scientific': NumberFormatter::SCIENTIFIC
- 'spellout': NumberFormatter::SPELLOUT
- 'ordinal': NumberFormatter::ORDINAL
- 'duration': NumberFormatter::DURATION
type
: Optional formatting type to use (default: 'default'). Choose one of these types:- 'default': NumberFormatter::TYPE_DEFAULT
- 'int32': NumberFormatter::TYPE_INT32
- 'int64': NumberFormatter::TYPE_INT64
- 'double': NumberFormatter::TYPE_DOUBLE
- 'currency': NumberFormatter::TYPE_CURRENCY
locale
: The locale used for the format. IfNULL
is given, Twig will useLocale::getDefault()
Use the localizedcurrency
filter to format a currency value into a localized string.
{{ product.price|localizedcurrency('EUR') }}
Note
Internally, Twig uses the PHP NumberFormatter::create() function for the number.
currency
: The 3-letter ISO 4217 currency code indicating the currency to use.locale
: The locale used for the format. IfNULL
is given, Twig will useLocale::getDefault()