formatting.txt 6.1 KB

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