Skip to content

Commit 0581d7f

Browse files
committed
Make the template extraction method honor the encoding option.
1 parent 86c5abb commit 0581d7f

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,31 @@ Babel provides a message extraction framework similar to GNU `xgettext`, but mor
1717
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:
1818

1919
```ini
20-
[django: templates/**.*, mypkg/*/templates/**.*]
20+
[django: templates/**.*]
21+
[django: mypkg/*/templates/**.*]
2122
[python: mypkg/**.py]
2223
```
2324

2425
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.
2526

2627
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.
2728

28-
If you don't use setuptools, or for some reason haven't installed !BabelDjango using setuptools/easy_install, 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:
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:
2930

3031
```ini
3132
[extractors]
3233
django = babeldjango.extract:extract_django
3334
```
3435

36+
The encoding of the templates is assumed to be UTF-8. If you are using a
37+
different encoding, you will need to specify it in the configuration. For
38+
example:
39+
40+
```ini
41+
[django: templates/**.*]
42+
encoding = iso-8859-1
43+
```
44+
3545
### Running the Extraction Process
3646

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

babeldjango/extract.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ def extract_django(fileobj, keywords, comment_tags, options):
2626
singular = []
2727
plural = []
2828
lineno = 1
29-
for t in Lexer(fileobj.read(), None).tokenize():
29+
30+
encoding = options.get('encoding', 'utf8')
31+
text = fileobj.read().decode(encoding)
32+
33+
for t in Lexer(text, None).tokenize():
3034
lineno += t.contents.count('\n')
3135
if intrans:
3236
if t.token_type == TOKEN_BLOCK:

0 commit comments

Comments
 (0)