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