Package turbogears :: Package i18n :: Package pygettext :: Module pygettext

Module pygettext

source code

pygettext -- Python equivalent of xgettext(1)

Many systems (Solaris, Linux, Gnu) provide extensive tools that ease the
internationalization of C programs. Most of these tools are independent of
the programming language and can be used from within Python programs.
Martin von Loewis' work[1] helps considerably in this regard.

There's one problem though; xgettext is the program that scans source code
looking for message strings, but it groks only C (or C++). Python
introduces a few wrinkles, such as dual quoting characters, triple quoted
strings, and raw strings. xgettext understands none of this.

Enter pygettext, which uses Python's standard tokenize module to scan
Python source code, generating .pot files identical to what GNU xgettext[2]
generates for C and C++ code. From there, the standard GNU tools can be
used.

A word about marking Python strings as candidates for translation. GNU
xgettext recognizes the following keywords: gettext, dgettext, dcgettext,
and gettext_noop. But those can be a lot of text to include all over your
code. C and C++ have a trick: they use the C preprocessor. Most
internationalized C source includes a #define for gettext() to _() so that
what has to be written in the source is much less. Thus these are both
translatable strings:

    gettext("Translatable String")
    _("Translatable String")

Python of course has no preprocessor so this doesn't work so well.  Thus,
pygettext searches only for _() by default, but see the -k/--keyword flag
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

NOTE: pygettext attempts to be option and feature compatible with GNU
xgettext where ever possible. However some options are still missing or are
not fully implemented. Also, xgettext's use of command line switches with
option arguments is broken, and in these cases, pygettext just defines
additional switches.

Usage: pygettext [options] inputfile ...

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.

If `inputfile' is -, standard input is read.


Version: 1.5

Classes
  TokenEater
Functions
 
_(s) source code
 
usage(code, msg='') source code
 
make_escapes(pass_iso8859) source code
 
escape_ascii(s)
Escape all non-ascii text plus control chars and Python literals.
source code
 
escape_unicode(s)
Escape control chars and Python literals, leave non-ascii text intact.
source code
 
safe_eval(s) source code
 
normalize(s, escape=False) source code
 
containsAny(str, set)
Check whether 'str' contains ANY of the chars in 'set'
source code
 
getFilesForName(name)
Get a list of module files for a filename, a module or package name, or a directory.
source code
 
extract_genshi_strings(filename, options=None)
Extract translatable strings from a Genshi template.
source code
 
main() source code
Variables
  __doc__ = _("""pygettext -- Python equivalent of xgettext...
  kid_parser = None
hash(x)
  default_keywords = ['_']
  DEFAULTKEYWORDS = '_'
  EMPTYSTRING = ''
  pot_header = '# SOME DESCRIPTIVE TITLE.\n# Copyright (C) YEAR ...
  escapes = []
  __package__ = 'turbogears.i18n.pygettext'
Function Details

extract_genshi_strings(filename, options=None)

source code 
Extract translatable strings from a Genshi template.

The extractor will get all the text inside all elements which are
not in the ignore list (see options) and the values of all
attributes named in the include list.

Options:

`ignore_tags` -- `'script style'`
    List of element names. Content inside elements named in
    this list is not extracted as translatable text. Can be a
    space-separated string or a list of string.
`include_attrs` -- `'abbr alt label prompt standby summary title'`
    List of attribute names. Only values of the attributes named in this
    list are extracted as translatable text. Can be a space-separated
    string or a list of string.

See http://genshi.edgewall.org/wiki/Documentation/0.5.x/i18n.html for
more information.


Variables Details

__doc__

Value:
_("""pygettext -- Python equivalent of xgettext(1)

Many systems (Solaris, Linux, Gnu) provide extensive tools that ease t\
he
internationalization of C programs. Most of these tools are independen\
t of
the programming language and can be used from within Python programs.
Martin von Loewis' work[1] helps considerably in this regard.
...

pot_header

Value:
'''# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\\n"
"POT-Creation-Date: %(time)s\\n"
...