Skip to content

Commit 78b2f0f

Browse files
committed
Documentation fixes.
1 parent 0581d7f commit 78b2f0f

File tree

1 file changed

+81
-25
lines changed

1 file changed

+81
-25
lines changed

README.md

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Tools for using Babel with Django
22

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:
55

66
* A message extraction plugin for Django templates.
77
* A middleware class that adds the Babel `Locale` object to requests.
@@ -10,23 +10,41 @@ Django web framework:
1010

1111
## Extracting Messages
1212

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.
1419

1520
### Extraction Method Mapping
1621

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:
1827

1928
```ini
2029
[django: templates/**.*]
2130
[django: mypkg/*/templates/**.*]
2231
[python: mypkg/**.py]
2332
```
2433

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.
2639

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.
2843

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:
3048

3149
```ini
3250
[extractors]
@@ -44,7 +62,9 @@ encoding = iso-8859-1
4462

4563
### Running the Extraction Process
4664

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:
4868

4969
```bash
5070
$ cd projectdir
@@ -55,46 +75,60 @@ This creates the PO file template in `mypkg/locale/django.pot`.
5575

5676
### Creating and Updating Translations Catalogs
5777

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
79+
is done using the `pybabel init` command:
5980

6081
```bash
6182
$ pybabel init -D django -i mypkg/locale/django.pot -d mypkg/locale -l en_US
6283
$ pybabel init -D django -i mypkg/locale/django.pot -d mypkg/locale -l de_DE
6384
```
6485

65-
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.
6689

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.
6894

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
96+
translation catalogs:
7097

71-
```bash
72-
$ pybabel update -D django -i mypkg/locale/django.pot -d mypkg/locale
98+
```bash $ pybabel update -D django -i mypkg/locale/django.pot -d mypkg/locale
7399
```
74100

75101
This will update all the `.po` files found in the `mypkg/locale` directory.
76102

77103
### Compiling Translations Catalogs
78104

79-
Finally, you need to compile those `.po` files to binary `.mo` files. Use the `pybabel compile` command for that:
105+
Finally, you need to compile those `.po` files to binary `.mo` files. Use the
106+
`pybabel compile` command for that:
80107

81108
```bash
82109
$ pybabel compile -D django -d mypkg/locale
83110
```
84111

85-
Add the `--statistics` option to get information about the completeness of your translations:
112+
Add the `--statistics` option to get information about the completeness of your
113+
translations:
86114

87115
```bash
88116
$ pybabel compile -D django -d mypkg/locale --statistics
89117
```
90118

91119
### Using `setup.py`
92120

93-
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+
94125

95126
## Using the Middleware
96127

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:
98132

99133
```python
100134
MIDDLEWARE_CLASSES = (
@@ -105,13 +139,22 @@ MIDDLEWARE_CLASSES = (
105139
)
106140
```
107141

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+
109148

110149
## Using the Template Tags
111150

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.
113155

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:
115158

116159
```python
117160
INSTALLED_APPS = (
@@ -121,15 +164,18 @@ INSTALLED_APPS = (
121164
)
122165
```
123166

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:
125169

126170
```jinja
127171
{% load babel %}
128172
```
129173

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].
131176

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`.
133179

134180
### `datefmt`
135181

@@ -138,7 +184,8 @@ Renders a string representation of a date.
138184
* __Input__: `datetime.date`, `datetime.datetime`, or a float/int timestamp
139185
* __Parameters__: the format name or pattern (optional)
140186

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:
142189

143190
```jinja
144191
{{ book.pubdate|datefmt:"short" }}
@@ -268,3 +315,12 @@ Assuming `book.numsold` would return 1.000.000,
268315
```
269316

270317
would render **10E5**.
318+
319+
320+
321+
[Babel]: http://babel.pocoo.org/
322+
[Django]: https://www.djangoproject.com/
323+
[Django_i18n]: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#localization-how-to-create-language-files
324+
[Setup]: http://babel.pocoo.org/docs/setup/
325+
[Dates]: http://babel.pocoo.org/docs/dates/
326+
[Numbers]: http://babel.pocoo.org/docs/numbers/

0 commit comments

Comments
 (0)