intl.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * ``calendar``: Calendar to use for formatting. The default value is 'gregorian',
  35. which corresponds to IntlDateFormatter::GREGORIAN. Choose one of these formats:
  36. * 'gregorian': `IntlDateFormatter::GREGORIAN`_
  37. * 'traditional': `IntlDateFormatter::TRADITIONAL`_
  38. For the following calendars should use 'traditional':
  39. * Japanese
  40. * Buddhist
  41. * Chinese
  42. * Persian
  43. * Indian
  44. * Islamic
  45. * Hebrew
  46. * Coptic
  47. * Ethiopic
  48. Also for non-Gregorian calendars need to be specified in locale.
  49. Examples might include locale="fa_IR@calendar=PERSIAN".
  50. ``localizednumber``
  51. -------------------
  52. Use the ``localizednumber`` filter to format numbers into a localized string
  53. representating the number.
  54. .. code-block:: jinja
  55. {{ product.quantity|localizednumber }}
  56. .. note::
  57. Internally, Twig uses the PHP `NumberFormatter::create()`_ function for
  58. the number.
  59. Arguments
  60. ~~~~~~~~~
  61. * ``style``: Optional number format (default: 'decimal'). Choose one of these formats:
  62. * 'decimal': `NumberFormatter::DECIMAL`_
  63. * 'currency': `NumberFormatter::CURRENCY`_
  64. * 'percent': `NumberFormatter::PERCENT`_
  65. * 'scientific': `NumberFormatter::SCIENTIFIC`_
  66. * 'spellout': `NumberFormatter::SPELLOUT`_
  67. * 'ordinal': `NumberFormatter::ORDINAL`_
  68. * 'duration': `NumberFormatter::DURATION`_
  69. * ``type``: Optional formatting type to use (default: 'default'). Choose one of these types:
  70. * 'default': `NumberFormatter::TYPE_DEFAULT`_
  71. * 'int32': `NumberFormatter::TYPE_INT32`_
  72. * 'int64': `NumberFormatter::TYPE_INT64`_
  73. * 'double': `NumberFormatter::TYPE_DOUBLE`_
  74. * 'currency': `NumberFormatter::TYPE_CURRENCY`_
  75. * ``locale``: The locale used for the format. If ``NULL`` is given, Twig will
  76. use ``Locale::getDefault()``
  77. ``localizedcurrency``
  78. ---------------------
  79. Use the ``localizedcurrency`` filter to format a currency value into a localized string.
  80. .. code-block:: jinja
  81. {{ product.price|localizedcurrency('EUR') }}
  82. .. note::
  83. Internally, Twig uses the PHP `NumberFormatter::create()`_ function for
  84. the number.
  85. Arguments
  86. ~~~~~~~~~
  87. * ``currency``: The 3-letter ISO 4217 currency code indicating the currency to use.
  88. * ``locale``: The locale used for the format. If ``NULL`` is given, Twig will
  89. use ``Locale::getDefault()``
  90. .. _`strtotime`: http://php.net/strtotime
  91. .. _`DateTime`: http://php.net/DateTime
  92. .. _`Unix timestamps`: http://en.wikipedia.org/wiki/Unix_time
  93. .. _`IntlDateFormatter::create()`: http://php.net/manual/en/intldateformatter.create.php
  94. .. _`IntlDateFormatter::NONE`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.none
  95. .. _`IntlDateFormatter::SHORT`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.short
  96. .. _`IntlDateFormatter::MEDIUM`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.medium
  97. .. _`IntlDateFormatter::LONG`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.long
  98. .. _`IntlDateFormatter::FULL`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.full
  99. .. _`IntlDateFormatter::GREGORIAN`: http://php.net/IntlDateFormatter#intldateformatter.constants.gregorian
  100. .. _`IntlDateFormatter::TRADITIONAL`: http://php.net/IntlDateFormatter#intldateformatter.constants.traditional
  101. .. _`ICU user guide`: http://userguide.icu-project.org/formatparse/datetime
  102. .. _`NumberFormatter::create()`: http://php.net/manual/en/numberformatter.create.php
  103. .. _`NumberFormatter::DECIMAL`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.decimal
  104. .. _`NumberFormatter::CURRENCY`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.currency
  105. .. _`NumberFormatter::PERCENT`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.percent
  106. .. _`NumberFormatter::SCIENTIFIC`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.scientific
  107. .. _`NumberFormatter::SPELLOUT`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.spellout
  108. .. _`NumberFormatter::ORDINAL`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.ordinal
  109. .. _`NumberFormatter::DURATION`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.duration
  110. .. _`NumberFormatter::TYPE_DEFAULT`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-default
  111. .. _`NumberFormatter::TYPE_INT32`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-int32
  112. .. _`NumberFormatter::TYPE_INT64`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-int64
  113. .. _`NumberFormatter::TYPE_DOUBLE`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-double
  114. .. _`NumberFormatter::TYPE_CURRENCY`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-currency