localization.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. .. _topics-i18n-localization:
  2. ============
  3. Localization
  4. ============
  5. This document covers two localization-related topics: `Creating language
  6. files`_ and `locale aware date, time and numbers input/output in forms`_
  7. .. _`Creating language files`: how-to-create-language-files_
  8. .. _`locale aware date, time and numbers input/output in forms`: format-localization_
  9. .. seealso::
  10. The :ref:`howto-i18n` document included with the Django HOW-TO documents collection.
  11. .. _how-to-create-language-files:
  12. How to create language files
  13. ============================
  14. Once the string literals of an application have been tagged for later
  15. translation, the translation themselves need to be written (or obtained). Here's
  16. how that works.
  17. .. _locale-restrictions:
  18. .. admonition:: Locale restrictions
  19. Django does not support localizing your application into a locale for which
  20. Django itself has not been translated. In this case, it will ignore your
  21. translation files. If you were to try this and Django supported it, you
  22. would inevitably see a mixture of translated strings (from your application)
  23. and English strings (from Django itself). If you want to support a locale
  24. for your application that is not already part of Django, you'll need to make
  25. at least a minimal translation of the Django core.
  26. A good starting point is to copy the Django English ``.po`` file and to
  27. translate at least some :term:`translation strings <translation string>`.
  28. Message files
  29. -------------
  30. The first step is to create a :term:`message file` for a new language. A message
  31. file is a plain-text file, representing a single language, that contains all
  32. available translation strings and how they should be represented in the given
  33. language. Message files have a ``.po`` file extension.
  34. Django comes with a tool, ``django-admin.py makemessages``, that automates the
  35. creation and upkeep of these files.
  36. .. admonition:: A note to Django veterans
  37. The old tool ``bin/make-messages.py`` has been moved to the command
  38. ``django-admin.py makemessages`` to provide consistency throughout Django.
  39. .. admonition:: Gettext utilities
  40. The ``makemessages`` command (and ``compilemessages`` discussed later) use
  41. commands from the GNU gettext toolset: ``xgetetxt``, ``msgfmt``,
  42. ``msgmerge`` and ``msguniq``.
  43. .. versionchanged:: 1.2
  44. The minimum version of the ``gettext`` utilities supported is 0.15.
  45. To create or update a message file, run this command::
  46. django-admin.py makemessages -l de
  47. ...where ``de`` is the language code for the message file you want to create.
  48. The language code, in this case, is in :term:`locale format<locale name>`. For
  49. example, it's ``pt_BR`` for Brazilian Portuguese and ``de_AT`` for Austrian
  50. German.
  51. The script should be run from one of two places:
  52. * The root directory of your Django project.
  53. * The root directory of your Django app.
  54. The script runs over your project source tree or your application source tree
  55. and pulls out all strings marked for translation. It creates (or updates) a
  56. message file in the directory ``locale/LANG/LC_MESSAGES``. In the ``de``
  57. example, the file will be ``locale/de/LC_MESSAGES/django.po``.
  58. By default ``django-admin.py makemessages`` examines every file that has the
  59. ``.html`` file extension. In case you want to override that default, use the
  60. ``--extension`` or ``-e`` option to specify the file extensions to examine::
  61. django-admin.py makemessages -l de -e txt
  62. Separate multiple extensions with commas and/or use ``-e`` or ``--extension``
  63. multiple times::
  64. django-admin.py makemessages -l de -e html,txt -e xml
  65. When :ref:`creating message files from JavaScript source code
  66. <creating-message-files-from-js-code>` you need to use the special 'djangojs'
  67. domain, **not** ``-e js``.
  68. .. admonition:: No gettext?
  69. If you don't have the ``gettext`` utilities installed, ``django-admin.py
  70. makemessages`` will create empty files. If that's the case, either install
  71. the ``gettext`` utilities or just copy the English message file
  72. (``locale/en/LC_MESSAGES/django.po``) if available and use it as a starting
  73. point; it's just an empty translation file.
  74. .. admonition:: Working on Windows?
  75. If you're using Windows and need to install the GNU gettext utilities so
  76. ``django-admin makemessages`` works see :ref:`gettext_on_windows` for more
  77. information.
  78. The format of ``.po`` files is straightforward. Each ``.po`` file contains a
  79. small bit of metadata, such as the translation maintainer's contact
  80. information, but the bulk of the file is a list of **messages** -- simple
  81. mappings between translation strings and the actual translated text for the
  82. particular language.
  83. For example, if your Django app contained a translation string for the text
  84. ``"Welcome to my site."``, like so::
  85. _("Welcome to my site.")
  86. ...then ``django-admin.py makemessages`` will have created a ``.po`` file
  87. containing the following snippet -- a message::
  88. #: path/to/python/module.py:23
  89. msgid "Welcome to my site."
  90. msgstr ""
  91. A quick explanation:
  92. * ``msgid`` is the translation string, which appears in the source. Don't
  93. change it.
  94. * ``msgstr`` is where you put the language-specific translation. It starts
  95. out empty, so it's your responsibility to change it. Make sure you keep
  96. the quotes around your translation.
  97. * As a convenience, each message includes, in the form of a comment line
  98. prefixed with ``#`` and located above the ``msgid`` line, the filename and
  99. line number from which the translation string was gleaned.
  100. Long messages are a special case. There, the first string directly after the
  101. ``msgstr`` (or ``msgid``) is an empty string. Then the content itself will be
  102. written over the next few lines as one string per line. Those strings are
  103. directly concatenated. Don't forget trailing spaces within the strings;
  104. otherwise, they'll be tacked together without whitespace!
  105. .. admonition:: Mind your charset
  106. When creating a PO file with your favorite text editor, first edit
  107. the charset line (search for ``"CHARSET"``) and set it to the charset
  108. you'll be using to edit the content. Due to the way the ``gettext`` tools
  109. work internally and because we want to allow non-ASCII source strings in
  110. Django's core and your applications, you **must** use UTF-8 as the encoding
  111. for your PO file. This means that everybody will be using the same
  112. encoding, which is important when Django processes the PO files.
  113. To reexamine all source code and templates for new translation strings and
  114. update all message files for **all** languages, run this::
  115. django-admin.py makemessages -a
  116. Compiling message files
  117. -----------------------
  118. After you create your message file -- and each time you make changes to it --
  119. you'll need to compile it into a more efficient form, for use by ``gettext``.
  120. Do this with the ``django-admin.py compilemessages`` utility.
  121. This tool runs over all available ``.po`` files and creates ``.mo`` files, which
  122. are binary files optimized for use by ``gettext``. In the same directory from
  123. which you ran ``django-admin.py makemessages``, run ``django-admin.py
  124. compilemessages`` like this::
  125. django-admin.py compilemessages
  126. That's it. Your translations are ready for use.
  127. .. admonition:: A note to Django veterans
  128. The old tool ``bin/compile-messages.py`` has been moved to the command
  129. ``django-admin.py compilemessages`` to provide consistency throughout
  130. Django.
  131. .. admonition:: Working on Windows?
  132. If you're using Windows and need to install the GNU gettext utilities so
  133. ``django-admin compilemessages`` works see :ref:`gettext_on_windows` for more
  134. information.
  135. .. _creating-message-files-from-js-code:
  136. Creating message files from JavaScript source code
  137. ==================================================
  138. You create and update the message files the same way as the other Django message
  139. files -- with the ``django-admin.py makemessages`` tool. The only difference is
  140. you need to provide a ``-d djangojs`` parameter, like this::
  141. django-admin.py makemessages -d djangojs -l de
  142. This would create or update the message file for JavaScript for German.
  143. After updating message files, just run ``django-admin.py compilemessages``
  144. the same way as you do with normal Django message files.
  145. .. _gettext_on_windows:
  146. ``gettext`` on Windows
  147. ======================
  148. This is only needed for people who either want to extract message IDs or compile
  149. message files (``.po``). Translation work itself just involves editing existing
  150. files of this type, but if you want to create your own message files, or want to
  151. test or compile a changed message file, you will need the ``gettext`` utilities:
  152. * Download the following zip files from the GNOME servers
  153. http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/ or from one
  154. of its mirrors_
  155. * ``gettext-runtime-X.zip``
  156. * ``gettext-tools-X.zip``
  157. ``X`` is the version number, we are requiring ``0.15`` or higher.
  158. * Extract the contents of the ``bin\`` directories in both files to the
  159. same folder on your system (i.e. ``C:\Program Files\gettext-utils``)
  160. * Update the system PATH:
  161. * ``Control Panel > System > Advanced > Environment Variables``.
  162. * In the ``System variables`` list, click ``Path``, click ``Edit``.
  163. * Add ``;C:\Program Files\gettext-utils\bin`` at the end of the
  164. ``Variable value`` field.
  165. .. _mirrors: http://ftp.gnome.org/pub/GNOME/MIRRORS
  166. You may also use ``gettext`` binaries you have obtained elsewhere, so long as
  167. the ``xgettext --version`` command works properly. Do not attempt to use Django
  168. translation utilities with a ``gettext`` package if the command ``xgettext
  169. --version`` entered at a Windows command prompt causes a popup window saying
  170. "xgettext.exe has generated errors and will be closed by Windows".
  171. .. _format-localization:
  172. Format localization
  173. ===================
  174. Django's formatting system is disabled by default. To enable it, it's necessary
  175. to set :setting:`USE_L10N = True <USE_L10N>` in your settings file.
  176. When using Django's formatting system, dates and numbers on templates will be
  177. displayed using the format specified for the current locale. Two users
  178. accessing the same content, but in different language, will see date and
  179. number fields formatted in different ways, depending on the format for their
  180. current locale.
  181. Django will also use localized formats when parsing data in forms. That means
  182. Django uses different formats for different locales when guessing the format
  183. used by the user when inputting data on forms. Note that Django uses different
  184. formats for displaying data, and for parsing it.
  185. Creating custom format files
  186. ----------------------------
  187. Django provides format definitions for many locales, but sometimes you might
  188. want to create your own, because a format files doesn't exist for your locale,
  189. or because you want to overwrite some of the values.
  190. To use custom formats, first thing to do, is to specify the path where you'll
  191. place format files. To do that, just set your :setting:`FORMAT_MODULE_PATH`
  192. setting to the path (in the format ``'foo.bar.baz``) where format files
  193. will exists.
  194. Files are not placed directly in this directory, but in a directory named as
  195. the locale, and must be named ``formats.py``.
  196. To customize the English formats, a structure like this would be needed::
  197. mysite/
  198. formats/
  199. __init__.py
  200. en/
  201. __init__.py
  202. formats.py
  203. where :file:`formats.py` contains custom format definitions. For example::
  204. THOUSAND_SEPARATOR = ' '
  205. to use a space as a thousand separator, instead of the default for English,
  206. a comma.