views:

127

answers:

2

In Mercurial, many of the extensions wrap their help/syntax string in a call to an underscore function, like so:

 _('[OPTION] [QUEUE]')

This confuses me, because it does not seem necessary (the Writing Extensions instructions don't mention it) and there doesn't seem to be a _ defined in the class, so I'm wondering if this is some special syntax that I don't understand, perhaps another way to say lambda, or maybe the identity function? Additionally I'm wondering what the benefit of this methodology (whatever it is) is over just the raw string like the documentation suggests.

Nothing I've seen in the Python documentation mentions such a function, so I'm not sure if this is really a Python question, or a Mercurial question.

Here are two examples that use this structure (look at the cmdtable dictionary near the bottom of the file) http://selenic.com/repo/hg/file/42408cd43f55/hgext/mq.py http://selenic.com/repo/hg/file/42408cd43f55/hgext/graphlog.py

+6  A: 

Look on line 45:

from mercurial.i18n import _

This is the usual abbreviation in the internationalization package gettext, and possibly other packages too, for the function that returns a translation of its argument to the language the program is currently running in. It's abbreviated to _ for convenience, since it's used for just about every message displayed to the user.

Looks like Mercurial wraps it in their own module. ("i18n" stands for "internationalization" because there are 18 letters in between "i" and "n".)

ptomato
Ahaha! Ok thank you! So this is for convenience if someone wanted to run it in their own language - but they'd still have to provide / commit their own translations, right? If I use gettext I'm not expected to provide my own, right?
dimo414
@dimo414, with the standard library's `gettext` (and with GNU's own for other languages), you're _definitely_ expected to provide your own translations -- what `gettext`'s code does is to fetch and use those translated phrases, it most assuredly **doesn't** do automatic machine translation among different natural languages!!!
Alex Martelli
@Alex, sorry I wasn't clear - the question was about expectations, not functionality. From a "providing a completed program" perspective, am I expected to create these translations myself, or is it considered alright for me to release code without them?
dimo414
@dimo414, it's fine to release code without the translations, but to allow _others_ to write and install their own translations you should distribute the basic `.po` files, see http://www.gnu.org/software/gettext/manual/gettext.html#Installing-Localizations for more.
Alex Martelli
So *thats* the reason for calling it "i18n". I've been wondering about that, so thanks :-)
THC4k
+2  A: 

_ (a function name of a single underscore) is often associated with internationalization, due to the precedent of gettext, a GNU approach that has also found a place in Python's standard library (same architecture, completely different implementation) -- per the module's docs,

gettext.install(domain[, localedir[, unicode[, codeset[, names]]]])

This installs the function _() in Python’s builtins namespace, based on domain, localedir, and codeset which are passed to the function translation(). The unicode flag is passed to the resulting translation object’s install() method.

For the names parameter, please see the description of the translation object’s install() method.

As seen below, you usually mark the strings in your application that are candidates for translation, by wrapping them in a call to the _() function, like this:

print _('This string will be translated.') 

For convenience, you want the _() function to be installed in Python’s builtins namespace, so it is easily accessible in all modules of your application.

As @ptomato mentions, Mercurial has followed this tradition by naming _ the equivalent function of their own that they use for the same internationalization purposes.

There is also a separate tradition to use _ as the "I don't care" identifier, as in

fee, fie, _, _, foo, _, fum = thesevenitemstuple

but of course you'd better not be using both traditions at once in the same code;-)

Alex Martelli
You meant "Python's standard library", right?
tonfa
@tonfa, yep -- fixing, thanks.
Alex Martelli