Martin Tóth
Martin Tóth

Reputation: 1747

How to ignore already translated strings from Django's .po files when running `django-admin makemessages`

My Django app uses some strings that are already translated in Django. In custom templates for password reset process I'd like to use some of the original texts, like this one prompting user to log in after completing the reset process.

Custom template contains <p>{% trans "Your password has been set. You may go ahead and log in now." %}</p> taken directly from the original form file.

After running django-admin makemessages my .po file contains this:

#: core/templates/auth/password-reset-complete.html:10
msgid "Your password has been set.  You may go ahead and log in now."
msgstr ""

Translation is working, rendered page already contains the correct translated string. Is it possible to ommit this empty translation from .po file automatically? Simply removing it will only work until I run makemessages again. It's already been translated, to duplicate it in my .po file seems unnecessary.

Upvotes: 8

Views: 1185

Answers (2)

Cyrille Pontvieux
Cyrille Pontvieux

Reputation: 2471

Using @Ove idea, I used two wrapped tags trans_done and blocktrans_done:

templatetags/i18n_done.py

from django import template
from django.templatetags.i18n import (
    do_block_translate,
    do_translate,
)

register = template.Library()


@register.tag("translate_done")
@register.tag("trans_done")
def translate_done(parser, token):
    return do_translate(parser, token)


@register.tag("blocktranslate_done")
@register.tag("blocktrans_done")
def block_translate_done(parser, token):
    return do_block_translate(parser, token)

Then in my template, I simply:

{% load i18n_done %}
<p>{% trans_done "Your password has been set.  You may go ahead and log in now." %}</p>

This will still be translated, but gettext will not find those strings.

Simple enough ;-)

Upvotes: 0

Ove
Ove

Reputation: 788

You can try putting your string into a variable before giving it to trans, so that makemessages doesn't notice it. Something like

{% with mystr="Your password has been set.  You may go ahead and log in now." %}
{% trans mystr %}
{% endwith %}

Alternatively, you could create your own custom template tag that simply calls gettext (from django.utils.translation) on its argument, and use that instead of trans.

Upvotes: 5

Related Questions