You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+81-25Lines changed: 81 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# Tools for using Babel with Django
2
2
3
-
This package contains various utilities for integration of Babel into the
4
-
Django web framework:
3
+
This package contains various utilities for integration of [Babel][] into the
4
+
[Django][] web framework:
5
5
6
6
* A message extraction plugin for Django templates.
7
7
* A middleware class that adds the Babel `Locale` object to requests.
@@ -10,23 +10,41 @@ Django web framework:
10
10
11
11
## Extracting Messages
12
12
13
-
Babel provides a message extraction framework similar to GNU `xgettext`, but more extensible and geared towards Python applications. While Django does provide [http://www.djangoproject.com/documentation/i18n/#how-to-create-language-files wrapper scripts] for making the use of `xgettext` more convenient, the extraction functionality is rather limited. For example, you can't use template files with an extension other than `.html`, and everything needs to be in your project package directory.
13
+
Babel provides a message extraction framework similar to GNU `xgettext`, but
14
+
more extensible and geared towards Python applications. While Django does
15
+
provide [wrapper scripts][Django_i18n] for making the use of `xgettext` more
16
+
convenient, the extraction functionality is rather limited. For example, you
17
+
can't use template files with an extension other than `.html`, and everything
18
+
needs to be in your project package directory.
14
19
15
20
### Extraction Method Mapping
16
21
17
-
So !BabelDjango comes with an extraction method plugin that can extract localizable messages from Django template files. Python is supported out of the box by Babel. To use this extraction functionality, create a file called `babel.cfg` in your project directory (the directory above your project package), with the content:
22
+
So `django-babel` comes with an extraction method plugin that can extract
23
+
localizable messages from Django template files. Python is supported out of the
24
+
box by Babel. To use this extraction functionality, create a file called
25
+
`babel.cfg` in your project directory (the directory above your project
26
+
package), with the content:
18
27
19
28
```ini
20
29
[django: templates/**.*]
21
30
[django: mypkg/*/templates/**.*]
22
31
[python: mypkg/**.py]
23
32
```
24
33
25
-
This instructs Babel to look for any files in the top-level `templates` directory, or any files in application `templates` directories, and use the extraction method named “django” to extract messages from those template files. You'll need to adjust those glob patterns to wherever you my be storing your templates.
34
+
This instructs Babel to look for any files in the top-level `templates`
35
+
directory, or any files in application `templates` directories, and use the
36
+
extraction method named “django” to extract messages from those template files.
37
+
You'll need to adjust those glob patterns to wherever you my be storing your
38
+
templates.
26
39
27
-
Also, any files with the extension `.py` inside your package directory (replace “mypkg” with the actual name of your Django project package) are processed by the “python” extraction method.
40
+
Also, any files with the extension `.py` inside your package directory (replace
41
+
“mypkg” with the actual name of your Django project package) are processed by
42
+
the “python” extraction method.
28
43
29
-
If you don't use setuptools, or for some reason haven't installed !BabelDjango using setuptools/pip, you'll need to define what function the extraction method “django” maps to. This is done in an extra section at the top of the configuration file:
44
+
If you don't use setuptools, or for some reason haven't installed !BabelDjango
45
+
using setuptools/pip, you'll need to define what function the extraction method
46
+
“django” maps to. This is done in an extra section at the top of the
47
+
configuration file:
30
48
31
49
```ini
32
50
[extractors]
@@ -44,7 +62,9 @@ encoding = iso-8859-1
44
62
45
63
### Running the Extraction Process
46
64
47
-
Once you've set up the configuration file, the actual extraction is performed by executing the command-line program `pybabel` which is installed alongside the Babel package:
65
+
Once you've set up the configuration file, the actual extraction is performed
66
+
by executing the command-line program `pybabel` which is installed alongside
67
+
the Babel package:
48
68
49
69
```bash
50
70
$ cd projectdir
@@ -55,46 +75,60 @@ This creates the PO file template in `mypkg/locale/django.pot`.
55
75
56
76
### Creating and Updating Translations Catalogs
57
77
58
-
If you don't already have translation catalogs, you need to create them. This is done using the `pybabel init` command:
78
+
If you don't already have translation catalogs, you need to create them. This
This should create two files: `mypkg/locale/en_US/django.po` and `mypkg/locale/de_DE/django.po`. These files are where you put the actual translations.
86
+
This should create two files: `mypkg/locale/en_US/django.po` and
87
+
`mypkg/locale/de_DE/django.po`. These files are where you put the actual
88
+
translations.
66
89
67
-
When you modify your Python source files or your templates, you genereally need to sync the translation catalogs. For that, you first perform a fresh extraction as described in the previous section, so that the `django.pot` file gets updated.
90
+
When you modify your Python source files or your templates, you genereally need
91
+
to sync the translation catalogs. For that, you first perform a fresh
92
+
extraction as described in the previous section, so that the `django.pot` file
93
+
gets updated.
68
94
69
-
Then, you run the `pybabel update` command to merge the changes into the translation catalogs:
95
+
Then, you run the `pybabel update` command to merge the changes into the
Much of the above process can be automated if you add a `setup.py` script to your project and use the distutils/setuptools commands that come with Babel. This is described at [wiki:Documentation/setup.html Distutils/Setuptools Integration].
121
+
Much of the above process can be automated if you add a `setup.py` script to
122
+
your project and use the distutils/setuptools commands that come with Babel.
123
+
This is described at [Distutils/Setuptools Integration][setup].
124
+
94
125
95
126
## Using the Middleware
96
127
97
-
To use the Babel middleware, add it to the list of `MIDDLEWARE_CLASSES` in your settings module. If you're also using Django's own `LocaleMiddleware` to vary the locale based on user preference, the Babel middleware must be inserted after the Django one:
128
+
To use the Babel middleware, add it to the list of `MIDDLEWARE_CLASSES` in your
129
+
settings module. If you're also using Django's own `LocaleMiddleware` to vary
130
+
the locale based on user preference, the Babel middleware must be inserted
131
+
after the Django one:
98
132
99
133
```python
100
134
MIDDLEWARE_CLASSES= (
@@ -105,13 +139,22 @@ MIDDLEWARE_CLASSES = (
105
139
)
106
140
```
107
141
108
-
This adds a `locale` attribute to the request object, which is an instance of the Babel `Locale` class. You can access the locale via `request.locale` when the request object is available, or otherwise use the `babeldjango.middleware.get_current_locale()` function to get the current locale from a thread-local cache.
142
+
This adds a `locale` attribute to the request object, which is an instance of
143
+
the Babel `Locale` class. You can access the locale via `request.locale` when
144
+
the request object is available, or otherwise use the
145
+
`babeldjango.middleware.get_current_locale()` function to get the current
146
+
locale from a thread-local cache.
147
+
109
148
110
149
## Using the Template Tags
111
150
112
-
The template filters provided by !BabelDjango allow formatting of date/time and number values in a locale-sensitive manner, providing much more powerful alternatives to the `date`, `time`, and `floatformat` filters that come with Django.
151
+
The template filters provided by !BabelDjango allow formatting of date/time and
152
+
number values in a locale-sensitive manner, providing much more powerful
153
+
alternatives to the `date`, `time`, and `floatformat` filters that come with
154
+
Django.
113
155
114
-
To make the template filters/tags available, you need to add !BabelDjango to the list of `INSTALLED_APPS` in your settings module:
156
+
To make the template filters/tags available, you need to add !BabelDjango to
157
+
the list of `INSTALLED_APPS` in your settings module:
115
158
116
159
```python
117
160
INSTALLED_APPS= (
@@ -121,15 +164,18 @@ INSTALLED_APPS = (
121
164
)
122
165
```
123
166
124
-
And in every template you want to use the filters, you need to explicitly load the !BabelDjango library:
167
+
And in every template you want to use the filters, you need to explicitly load
168
+
the !BabelDjango library:
125
169
126
170
```jinja
127
171
{% load babel %}
128
172
```
129
173
130
-
General information on date/time and number formatting can be found at [wiki:Documentation/dates.html Date Formatting] and [wiki:Documentation/numbers.html Number Formatting].
174
+
General information on date/time and number formatting can be found at
175
+
[Date Formatting][Dates] and [Number Formatting][Numbers].
131
176
132
-
The following filters are made available. The examples assume a locale of `en_US`.
177
+
The following filters are made available. The examples assume a locale of
178
+
`en_US`.
133
179
134
180
### `datefmt`
135
181
@@ -138,7 +184,8 @@ Renders a string representation of a date.
138
184
*__Input__: `datetime.date`, `datetime.datetime`, or a float/int timestamp
139
185
*__Parameters__: the format name or pattern (optional)
140
186
141
-
Assuming that `book.pubdate` returns a `datetime.date` or `datetime.datetime` object:
187
+
Assuming that `book.pubdate` returns a `datetime.date` or `datetime.datetime`
188
+
object:
142
189
143
190
```jinja
144
191
{{ book.pubdate|datefmt:"short" }}
@@ -268,3 +315,12 @@ Assuming `book.numsold` would return 1.000.000,
0 commit comments