pygettext -- Python equivalent of xgettext(1)
![](/@@/translation-newline)
Many systems (Solaris, Linux, Gnu) provide extensive tools that ease the
![](/@@/translation-newline)
internationalization of C programs. Most of these tools are independent of
![](/@@/translation-newline)
the programming language and can be used from within Python programs.
![](/@@/translation-newline)
Martin von Loewis' work[1] helps considerably in this regard.
![](/@@/translation-newline)
There's one problem though; xgettext is the program that scans source code
![](/@@/translation-newline)
looking for message strings, but it groks only C (or C++). Python
![](/@@/translation-newline)
introduces a few wrinkles, such as dual quoting characters, triple quoted
![](/@@/translation-newline)
strings, and raw strings. xgettext understands none of this.
![](/@@/translation-newline)
Enter pygettext, which uses Python's standard tokenize module to scan
![](/@@/translation-newline)
Python source code, generating .pot files identical to what GNU xgettext[2]
![](/@@/translation-newline)
generates for C and C++ code. From there, the standard GNU tools can be
![](/@@/translation-newline)
used.
![](/@@/translation-newline)
A word about marking Python strings as candidates for translation. GNU
![](/@@/translation-newline)
xgettext recognizes the following keywords: gettext, dgettext, dcgettext,
![](/@@/translation-newline)
and gettext_noop. But those can be a lot of text to include all over your
![](/@@/translation-newline)
code. C and C++ have a trick: they use the C preprocessor. Most
![](/@@/translation-newline)
internationalized C source includes a #define for gettext() to _() so that
![](/@@/translation-newline)
what has to be written in the source is much less. Thus these are both
![](/@@/translation-newline)
translatable strings:
gettext("Translatable String")
_("Translatable String")
![](/@@/translation-newline)
Python of course has no preprocessor so this doesn't work so well. Thus,
![](/@@/translation-newline)
pygettext searches only for _() by default, but see the -k/--keyword flag
![](/@@/translation-newline)
below for how to augment this.
[1] http://www.python.org/workshops/1997-10/proceedings/loewis.html
[2] http://www.gnu.org/software/gettext/gettext.html
![](/@@/translation-newline)
NOTE: pygettext attempts to be option and feature compatible with GNU
![](/@@/translation-newline)
xgettext where ever possible. However some options are still missing or are
![](/@@/translation-newline)
not fully implemented. Also, xgettext's use of command line switches with
![](/@@/translation-newline)
option arguments is broken, and in these cases, pygettext just defines
![](/@@/translation-newline)
additional switches.
![](/@@/translation-newline)
Usage: pygettext [options] inputfile ...
![](/@@/translation-newline)
Options:
-a
--extract-all
Extract all strings.
-d name
--default-domain=name
Rename the default output file from messages.pot to name.pot.
-E
--escape
Replace non-ASCII characters with octal escape sequences.
-D
--docstrings
Extract module, class, method, and function docstrings. These do
not need to be wrapped in _() markers, and in fact cannot be for
Python to consider them docstrings. (See also the -X option).
-h
--help
Print this help message and exit.
-k word
--keyword=word
Keywords to look for in addition to the default set, which are:
%(DEFAULTKEYWORDS)s
You can have multiple -k flags on the command line.
-K
--no-default-keywords
Disable the default set of keywords (see above). Any keywords
explicitly added with the -k/--keyword option are still recognized.
--no-location
Do not write filename/lineno location comments.
-n
--add-location
Write filename/lineno location comments indicating where each
extracted string is found in the source. These lines appear before
each msgid. The style of comments is controlled by the -S/--style
option. This is the default.
-o filename
--output=filename
Rename the default output file from messages.pot to filename. If
filename is `-' then the output is sent to standard out.
-p dir
--output-dir=dir
Output files will be placed in directory dir.
-S stylename
--style stylename
Specify which style to use for location comments. Two styles are
supported:
Solaris # File: filename, line: line-number
GNU #: filename:line
The style name is case insensitive. GNU style is the default.
-v
--verbose
Print the names of the files being processed.
-V
--version
Print the version of pygettext and exit.
-w columns
--width=columns
Set width of output to columns.
-x filename
--exclude-file=filename
Specify a file that contains a list of strings that are not be
extracted from the input files. Each string to be excluded must
appear on a line by itself in the file.
-X filename
--no-docstrings=filename
Specify a file that contains a list of files (one per line) that
should not have their docstrings extracted. This is only useful in
conjunction with the -D option above.
![](/@@/translation-newline)
If `inputfile' is -, standard input is read.