Re: gettext_noop usage?

868 views
Skip to first unread message

Mike

unread,
Feb 15, 2008, 3:30:28 AM2/15/08
to Django users
On Sep 18 2007, 11:09 pm, Amit Ramon <amit.ra...@gmail.com> wrote:
>
> I read the documentation of gettext_noop and its template counterpart {%
> trans "value" noop %} and I must admit I still don't understand their use
> cases and usage.
>
> This is what djangobook says:
>
> --------x-------------------
> "Marking strings as no-op
> Use the function django.utils.translation.gettext_noop() to mark a string as a
> translation string without translating it. The string is later translated
> from a variable.
>
> Use this if you have constant strings that should be stored in the source
> language because they are exchanged over systems or users -- such as strings
> in a database -- but should be translated at the last possible point in time,
> such as when the string is presented to the user."
> --------x-------------------
>
> I might be dumb, but could someone shed some light on this, and show an
> example of no-op'ed strings and late translation of it for the novice user?
> What is the use case here? How a string is translated from a variable? When?

Me is stupid, too. What is that mysterious variable? Somebody, please
help!

Mike

Malcolm Tredinnick

unread,
Feb 15, 2008, 3:44:32 AM2/15/08
to django...@googlegroups.com

There's little practical usage for this in Django. It's something that
was copied over from the standard gettext API. Because there's nothing
equivalent to gettext_lazy in C (or Javascript, see below), you need to
use gettext_noop in situations like this:

msgs = [
gettext_noop('hello'),
gettext_noop('goodbye')
]


def foo(...):
print gettext(msgs[index])

When foo() is called, the msgs[index] entry will be translated using the
currently active locale. That's all well and good, but you have to let
the string-extraction tools (xgettext, or make-messages.py in Django's
parlance) know that the strings inside the msgs lists need to pulled out
for translators. So gettext_noop is used to mark the strings: xgettext
knows to look for things wrapped in gettext_noop and pull then out.
Other than that, it does nothing (it's a no-op function).

This only place where you might this inside Django is in Javascript
files, if you're building up a list of messages that are later going to
be run through gettext() before printing. Note that it's important to
only run the strings through gettext() at the time of output, because at
the time you're creating/compiling the file, you don't know what the
currently active locale is. So you might have an array of Javascript
strings that are canned responsed or something. Marking them with
gettext_noop means that make-messages.py knows to pull them into the PO
file.

I hope that makes things a bit clearer.

Regards,
Malcolm

--
For every action there is an equal and opposite criticism.
http://www.pointy-stick.com/blog/

Reply all
Reply to author
Forward
0 new messages