Django Noop use case


Example

(u)gettext_noop allows you to mark a string as translatable without actually translating it.

A typical use case is when you want to log a message for developers (in English) but also want to display it to the client (in the requested language). You can pass a variable to gettext, but its content won't be discovered as a translatable string because it is, per definition, variable..

# THIS WILL NOT WORK AS EXPECTED
import logging 
from django.contrib import messages

logger = logging.getLogger(__name__)

error_message = "Oops, something went wrong!"
logger.error(error_message)
messages.error(request, _(error_message))

The error message won't appear in the .po file and you will have to remember it exists to add it manually. To fix this, you can use gettext_noop.

error_message = ugettext_noop("Oops, something went wrong!")
logger.error(error_message)
messages.error(request, _(error_message))

Now the string "Oops, something went wrong!" will be discovered and available in the .po file when generated. And the error will still be logged in English for developers.