formatting.txt 6.0 KB

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