[Python-Dev] The "i" string-prefix: I18n'ed strings

Martin Blais blais at furius.ca
Fri Apr 7 02:35:51 CEST 2006


On 4/6/06, Alex Martelli <aleaxit at gmail.com> wrote:
> On 4/6/06, Martin Blais <blais at furius.ca> wrote:
>
> > - We could also have a prefix "I" for strings to be marked but not
> > runtime-translated, to replace the N_() strings.
>
> I'm more dubious about this one, because I don't really see the point.
>  Expand pls?  With a couple of use cases, maybe?

N_() is used for marking up strings to be extracted for the catalogs,
but that will get expanded programmatically later on, explicitly using
_(variable) syntax or with a call to gettext().

For example, using my web forms library (wink, wink, open source at
http://furius.ca/atocha/), I declare forms at module-level, and so
this gets evaluated only once at import time, when the i18n
environment is not determined yet::

  form1 = Form(
      'test-form',
      StringField('name', N_("Person's Name")),
      ...

The point of declaring these at module or class level is that once the
apache child is loaded, none of the forms ever need be rebuilt, i.e.
it's fast.

N_() is a no-op (i.e. lambda x: x).  Later on, when a request is
processed (when we've setup the i18n target environment), the label
gets translated when used (i.e. from the Atocha code)::

    def _get_label( self, field ):
        """
        Returns a printable label for the given field.
        """
        return (field.label and _(field.label) # <------ translate the
label here
                or field.name.capitalize().decode('ascii'))

In summary: sometimes you need to mark-for-translate AND translate a
piece of string, two separate tasks handled by the _() function, and
sometimes you just need to mark-for-translate, handled by the N_()
function, under the assumption that you will be a good boy and not
forget to take care of it yourself later :-)  This is pretty standard
getttext stuff, if you used _() a lot I'm surprised you don't have a
need for N_(), I always needed it when I used i18n (or maybe I
misunderstood your question?).  It wouldn't make sense to me to add
i'' and not I'' (or equivalents).

There is another example in gloriously verbose C code version from the
GNU manual:
http://www.gnu.org/software/gettext/manual/html_node/gettext_19.html#SEC19

cheers,


More information about the Python-Dev mailing list