Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 749
Martin Tóth Avatar asked Jun 20 '17 12:06

Martin Tóth


People also ask

What are translation hooks in Django?

These hooks are called translation strings. They tell Django: “This text should be translated into the end user’s language, if a translation for this text is available in that language.” It’s your responsibility to mark translatable strings; the system can only translate strings it knows about.

What is the Django translation mechanisms?

The Django translation mechanisms can be used to translate arbitrary texts to any language that is supported by Django (as long as an appropriate translation catalog exists, of course).

What happens if you don’t supply a user in Django?

If you do not supply a user, the command will attempt to change the password whose username matches the current user. Specifies the database to query for the user. Defaults to default. This command is only available if Django’s authentication system ( django.contrib.auth) is installed. Creates a superuser account (a user who has all permissions).

How do I translate a Django website into another language?

When comes to using multiple languages in one single site, Django is very handy. You can use .po file to do your translation for you. Process is very simple: First create .po file. To make .po file I would suggest to use poedit or Rosetta. Here is another option that is using django’s very own Localisation.


1 Answers

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.

like image 65
Ove Avatar answered Sep 20 '22 12:09

Ove