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