formatting.txt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. .. _format-localization:
  2. ===================
  3. Format localization
  4. ===================
  5. .. versionadded:: 1.2
  6. Overview
  7. ========
  8. Django's formatting system is capable to display dates, times and numbers in templates using the format specified for the current :term:`locale <locale
  9. 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.py
  17. startproject <startproject>` includes :setting:`USE_L10N = True <USE_L10N>`
  18. for convenience.
  19. .. note::
  20. There is also an independent but related :setting:`USE_I18N` setting that
  21. controls if Django should activate translation. See
  22. :doc:`/topics/i18n/translation` for more 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 simply use its
  35. ``localize`` 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. .. versionadded:: 1.3
  59. Enables or disables localization of template variables in the
  60. contained block.
  61. This tag allows a more fine grained control of localization than
  62. :setting:`USE_L10N`.
  63. To activate or deactivate localization for a template block, use::
  64. {% load l10n %}
  65. {% localize on %}
  66. {{ value }}
  67. {% endlocalize %}
  68. {% localize off %}
  69. {{ value }}
  70. {% endlocalize %}
  71. .. note::
  72. The value of :setting:`USE_L10N` isn't respected inside of a
  73. ``{% localize %}`` block.
  74. See :tfilter:`localize` and :tfilter:`unlocalize` for template filters that will
  75. do the same job on a per-variable basis.
  76. Template filters
  77. ----------------
  78. .. templatefilter:: localize
  79. localize
  80. ~~~~~~~~
  81. .. versionadded:: 1.3
  82. Forces localization of a single value.
  83. For example::
  84. {% load l10n %}
  85. {{ value|localize }}
  86. To disable localization on a single value, use :tfilter:`unlocalize`. To control
  87. localization over a large section of a template, use the :ttag:`localize` template
  88. tag.
  89. .. templatefilter:: unlocalize
  90. unlocalize
  91. ~~~~~~~~~~
  92. .. versionadded:: 1.3
  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. 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 files 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 first.
  106. To do that, just set your :setting:`FORMAT_MODULE_PATH` setting to the package
  107. where format files will exist, for instance::
  108. FORMAT_MODULE_PATH = 'mysite.formats'
  109. Files are not placed directly in this directory, but in a directory named as
  110. the locale, and must be named ``formats.py``.
  111. To customize the English formats, a structure like this would be needed::
  112. mysite/
  113. formats/
  114. __init__.py
  115. en/
  116. __init__.py
  117. formats.py
  118. where :file:`formats.py` contains custom format definitions. For example::
  119. THOUSAND_SEPARATOR = ' '
  120. to use a space as a thousand separator, instead of the default for English,
  121. a comma.
  122. Limitations of the provided locale formats
  123. ==========================================
  124. Some locales use context-sensitive formats for numbers, which Djangos
  125. localization system cannot handle automatically.
  126. Switzerland (German)
  127. --------------------
  128. The Swiss number formatting depends on the type of number that is being
  129. formatted. For monetary values, a comma is used as the thousand separator and
  130. a decimal point for the decimal separator, for all other numbers, a comma is
  131. used as decimal separator and a space as thousand separator. The locale format
  132. provided by Django uses the generic separators, a comma for decimal and a space
  133. for thousand separators.