formatting.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ===================
  2. Format localization
  3. ===================
  4. Overview
  5. ========
  6. Django's formatting system is capable of displaying dates, times and numbers in
  7. templates using the format specified for the current
  8. :term:`locale <locale name>`. It also handles localized input in forms.
  9. When it's enabled, two users accessing the same content may see dates, times and
  10. numbers formatted in different ways, depending on the formats for their current
  11. locale.
  12. The formatting system is enabled by default. To disable it, it's
  13. necessary to set :setting:`USE_L10N = False <USE_L10N>` in your settings file.
  14. .. note::
  15. To enable number formatting with thousand separators, it is necessary to
  16. set :setting:`USE_THOUSAND_SEPARATOR = True <USE_THOUSAND_SEPARATOR>` in
  17. your settings file. Alternatively, you could use :tfilter:`intcomma` to
  18. format numbers in your template.
  19. .. note::
  20. There is a related :setting:`USE_I18N` setting that controls if Django
  21. should activate translation. See :doc:`/topics/i18n/translation` for more
  22. details.
  23. Locale aware input in forms
  24. ===========================
  25. When formatting is enabled, Django can use localized formats when parsing dates,
  26. times and numbers in forms. That means it tries different formats for different
  27. locales when guessing the format used by the user when inputting data on forms.
  28. .. note::
  29. Django uses different formats for displaying data to those it uses for
  30. parsing data. Most notably, the formats for parsing dates can't use the
  31. ``%a`` (abbreviated weekday name), ``%A`` (full weekday name),
  32. ``%b`` (abbreviated month name), ``%B`` (full month name),
  33. or ``%p`` (AM/PM).
  34. To enable a form field to localize input and output data use its ``localize``
  35. argument::
  36. class CashRegisterForm(forms.Form):
  37. product = forms.CharField()
  38. revenue = forms.DecimalField(max_digits=4, decimal_places=2, localize=True)
  39. .. _topic-l10n-templates:
  40. Controlling localization in templates
  41. =====================================
  42. When you have enabled formatting with :setting:`USE_L10N`, Django
  43. will try to use a locale specific format whenever it outputs a value
  44. in a template.
  45. However, it may not always be appropriate to use localized values --
  46. for example, if you're outputting JavaScript or XML that is designed
  47. to be machine-readable, you will always want unlocalized values. You
  48. may also want to use localization in selected templates, rather than
  49. using localization everywhere.
  50. To allow for fine control over the use of localization, Django
  51. provides the ``l10n`` template library that contains the following
  52. tags and filters.
  53. Template tags
  54. -------------
  55. .. templatetag:: localize
  56. ``localize``
  57. ~~~~~~~~~~~~
  58. Enables or disables localization of template variables in the
  59. contained block.
  60. This tag allows a more fine grained control of localization than
  61. :setting:`USE_L10N`.
  62. To activate or deactivate localization for a template block, use::
  63. {% load l10n %}
  64. {% localize on %}
  65. {{ value }}
  66. {% endlocalize %}
  67. {% localize off %}
  68. {{ value }}
  69. {% endlocalize %}
  70. .. note::
  71. The value of :setting:`USE_L10N` isn't respected inside of a
  72. ``{% localize %}`` block.
  73. See :tfilter:`localize` and :tfilter:`unlocalize` for template filters that will
  74. do the same job on a per-variable basis.
  75. Template filters
  76. ----------------
  77. .. templatefilter:: localize
  78. ``localize``
  79. ~~~~~~~~~~~~
  80. Forces localization of a single value.
  81. For example::
  82. {% load l10n %}
  83. {{ value|localize }}
  84. To disable localization on a single value, use :tfilter:`unlocalize`. To control
  85. localization over a large section of a template, use the :ttag:`localize` template
  86. tag.
  87. .. templatefilter:: unlocalize
  88. ``unlocalize``
  89. ~~~~~~~~~~~~~~
  90. Forces a single value to be printed without localization.
  91. For example::
  92. {% load l10n %}
  93. {{ value|unlocalize }}
  94. To force localization of a single value, use :tfilter:`localize`. To
  95. control localization over a large section of a template, use the
  96. :ttag:`localize` template tag.
  97. Returns a string representation for unlocalized numbers (``int``, ``float``,
  98. or ``Decimal``).
  99. .. _custom-format-files:
  100. Creating custom format files
  101. ============================
  102. Django provides format definitions for many locales, but sometimes you might
  103. want to create your own, because a format file doesn't exist for your locale,
  104. or because you want to overwrite some of the values.
  105. To use custom formats, specify the path where you'll place format files
  106. first. To do that, set your :setting:`FORMAT_MODULE_PATH` setting to the
  107. package where format files will exist, for instance::
  108. FORMAT_MODULE_PATH = [
  109. 'mysite.formats',
  110. 'some_app.formats',
  111. ]
  112. Files are not placed directly in this directory, but in a directory named as
  113. the locale, and must be named ``formats.py``. Be careful not to put sensitive
  114. information in these files as values inside can be exposed if you pass the
  115. string to ``django.utils.formats.get_format()`` (used by the :tfilter:`date`
  116. template filter).
  117. To customize the English formats, a structure like this would be needed::
  118. mysite/
  119. formats/
  120. __init__.py
  121. en/
  122. __init__.py
  123. formats.py
  124. where :file:`formats.py` contains custom format definitions. For example::
  125. THOUSAND_SEPARATOR = '\xa0'
  126. to use a non-breaking space (Unicode ``00A0``) as a thousand separator,
  127. instead of the default for English, a comma.
  128. Limitations of the provided locale formats
  129. ==========================================
  130. Some locales use context-sensitive formats for numbers, which Django's
  131. localization system cannot handle automatically.
  132. Switzerland (German)
  133. --------------------
  134. The Swiss number formatting depends on the type of number that is being
  135. formatted. For monetary values, a comma is used as the thousand separator and
  136. a decimal point for the decimal separator. For all other numbers, a comma is
  137. used as decimal separator and a space as thousand separator. The locale format
  138. provided by Django uses the generic separators, a comma for decimal and a space
  139. for thousand separators.