You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

intl.rst 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. The Intl Extension
  2. ==================
  3. The *Intl* extensions provides the ``localizeddate``, ``localizednumber`` and ``localizedcurrency`` filters.
  4. Installation
  5. ------------
  6. First, :ref:`install the Extensions library<extensions-install>`. Next, add
  7. the extension to Twig::
  8. $twig->addExtension(new Twig_Extensions_Extension_Intl());
  9. ``localizeddate``
  10. -----------------
  11. Use the ``localizeddate`` filter to format dates into a localized string
  12. representating the date.
  13. .. code-block:: jinja
  14. {{ post.published_at|localizeddate('medium', 'none', locale) }}
  15. The ``localizeddate`` filter accepts strings (it must be in a format supported
  16. by the `strtotime`_ function), `DateTime`_ instances, or `Unix timestamps`_.
  17. .. note::
  18. Internally, Twig uses the PHP `IntlDateFormatter::create()`_ function for
  19. the date.
  20. Arguments
  21. ~~~~~~~~~
  22. * ``date_format``: The date format. Choose one of these formats:
  23. * 'none': `IntlDateFormatter::NONE`_
  24. * 'short': `IntlDateFormatter::SHORT`_
  25. * 'medium': `IntlDateFormatter::MEDIUM`_
  26. * 'long': `IntlDateFormatter::LONG`_
  27. * 'full': `IntlDateFormatter::FULL`_
  28. * ``time_format``: The time format. Same formats possible as above.
  29. * ``locale``: The locale used for the format. If ``NULL`` is given, Twig will
  30. use ``Locale::getDefault()``
  31. * ``timezone``: The date timezone
  32. * ``format``: Optional pattern to use when formatting or parsing. Possible
  33. patterns are documented in the `ICU user guide`_.
  34. ``localizednumber``
  35. -------------------
  36. Use the ``localizednumber`` filter to format numbers into a localized string
  37. representating the number.
  38. .. code-block:: jinja
  39. {{ product.quantity|localizednumber }}
  40. .. note::
  41. Internally, Twig uses the PHP `NumberFormatter::create()`_ function for
  42. the number.
  43. Arguments
  44. ~~~~~~~~~
  45. * ``style``: Optional date format (default: 'decimal'). Choose one of these formats:
  46. * 'decimal': `NumberFormatter::DECIMAL`_
  47. * 'currency': `NumberFormatter::CURRENCY`_
  48. * 'percent': `NumberFormatter::PERCENT`_
  49. * 'scientific': `NumberFormatter::SCIENTIFIC`_
  50. * 'spellout': `NumberFormatter::SPELLOUT`_
  51. * 'ordinal': `NumberFormatter::ORDINAL`_
  52. * 'duration': `NumberFormatter::DURATION`_
  53. * ``type``: Optional formatting type to use (default: 'default'). Choose one of these types:
  54. * 'default': `NumberFormatter::TYPE_DEFAULT`_
  55. * 'int32': `NumberFormatter::TYPE_INT32`_
  56. * 'int64': `NumberFormatter::TYPE_INT64`_
  57. * 'double': `NumberFormatter::TYPE_DOUBLE`_
  58. * 'currency': `NumberFormatter::TYPE_CURRENCY`_
  59. * ``locale``: The locale used for the format. If ``NULL`` is given, Twig will
  60. use ``Locale::getDefault()``
  61. ``localizedcurrency``
  62. ---------------------
  63. Use the ``localizedcurrency`` filter to format a currency value into a localized string.
  64. .. code-block:: jinja
  65. {{ product.price|localizedcurrency('EUR') }}
  66. .. note::
  67. Internally, Twig uses the PHP `NumberFormatter::create()`_ function for
  68. the number.
  69. Arguments
  70. ~~~~~~~~~
  71. * ``currency``: The 3-letter ISO 4217 currency code indicating the currency to use.
  72. * ``locale``: The locale used for the format. If ``NULL`` is given, Twig will
  73. use ``Locale::getDefault()``
  74. .. _`strtotime`: http://php.net/strtotime
  75. .. _`DateTime`: http://php.net/DateTime
  76. .. _`Unix timestamps`: http://en.wikipedia.org/wiki/Unix_time
  77. .. _`IntlDateFormatter::create()`: http://php.net/manual/en/intldateformatter.create.php
  78. .. _`IntlDateFormatter::NONE`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.none
  79. .. _`IntlDateFormatter::SHORT`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.short
  80. .. _`IntlDateFormatter::MEDIUM`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.medium
  81. .. _`IntlDateFormatter::LONG`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.long
  82. .. _`IntlDateFormatter::FULL`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.full
  83. .. _`ICU user guide`: http://userguide.icu-project.org/formatparse/datetime
  84. .. _`NumberFormatter::create()`: http://php.net/manual/en/numberformatter.create.php
  85. .. _`NumberFormatter::DECIMAL`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.decimal
  86. .. _`NumberFormatter::CURRENCY`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.currency
  87. .. _`NumberFormatter::PERCENT`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.percent
  88. .. _`NumberFormatter::SCIENTIFIC`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.scientific
  89. .. _`NumberFormatter::SPELLOUT`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.spellout
  90. .. _`NumberFormatter::ORDINAL`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.ordinal
  91. .. _`NumberFormatter::DURATION`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.duration
  92. .. _`NumberFormatter::TYPE_DEFAULT`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-default
  93. .. _`NumberFormatter::TYPE_INT32`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-int32
  94. .. _`NumberFormatter::TYPE_INT64`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-int64
  95. .. _`NumberFormatter::TYPE_DOUBLE`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-double
  96. .. _`NumberFormatter::TYPE_CURRENCY`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-currency