[PATCH 1 of 3] i18n: extract strings with xgettext

Martin Geisler mg at daimi.au.dk
Sun Jan 25 14:18:27 CST 2009


Martin Geisler <mg at daimi.au.dk> writes:

> # HG changeset patch
> # User Martin Geisler <mg at daimi.au.dk>
> # Date 1232910681 -3600
> # Node ID b51466dae345e99d84a3b5b33eda7d752c0cf0c5
> # Parent  752325f2208d4279a4fc892adddd1bfbc61b0ad1
> i18n: extract strings with xgettext

Oh, by the way... xgettext will spew out a ton of warnings with this
patch. They are quite harmless... :-)

It is worried about strings that uses two or more conversion specifiers
without letting the translator switch their order. Like this one:

        raise util.Abort(_('%s is not a parent of %s') %
                         (short(p), short(node)))

The correct way to code this before Python 2.6[*] would be

        raise util.Abort(_('%(parent)s is not a parent of %(child)s') %
                         {'parent': short(p), 'child': short(node)})

That would allow a translator to switch the order by translating it into
something like this:

  '%(child)s is not a child of %(parent)s'

Since writing code like that is a major pain I suggest that we suppress
the warnings somehow (xgettext does not seem to have any -q flag...) and
deal with it on a case-by-case basis if a translator find that he cannot
possible translate one of the messages into his mother tongue.

An alternative is the way they do in Mailman -- they have a _ f function
which uses some magic to grab the local variables and substitute them
line 109 here:

  http://bazaar.launchpad.net/~mailman-coders/mailman/3.0/annotate/head%3A/src/mailman/i18n.py

That would would allow the above example to be coded as:

        parent = short(p)
        child = short(node)
        raise util.Abort(_('$parent is not a parent of $child'))


[*] With Python 2.6 one can do:

        raise util.Abort(_('{0} is not a parent of {1}')
                         .format(short(p), short(node)))

    which is easier than making a dictionary, but looks kind of silly
    compared to the good old % syntax...

    http://docs.python.org/library/stdtypes.html#str.format

-- 
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20090125/f21c3d46/attachment.pgp 


More information about the Mercurial-devel mailing list