Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly add entries for computed values to the django internationalization messages file?

Django documentation states:

The caveat with using variables or computed values, as in the previous two examples, is that Django's translation-string-detecting utility, django-admin.py makemessages, won't be able to find these strings.

That is fine with me, I'm ready to provide translations for all possible values of the translated variable by hand. But how to do that?

Let's say I have in my template code like this:

{% trans var %}

The var is extracted from the database, and I know all of the possible values of it - let's say the possible values are "Alice" and "Bob".

I thought all I need to do is provide entries like these:

msgid "Alice"
msgstr "Alicja"

in django.po file. Unfortunately, whenever i run djangoadmin makemessages after that, these entries are being commented out:

#~ msgid "Alice"
#~ msgstr "Alicja"

What am I doing wrong? Have I misunderstood the idea of translating computed values?

like image 900
Przemysław Pietrzkiewicz Avatar asked Oct 02 '11 11:10

Przemysław Pietrzkiewicz


People also ask

Which among the given below internationalization function in Django marks a string as a translation string without actually translating it at that moment?

Lazy Translation utils. translation. ugettext_lazy() to translate strings lazily – when the value is accessed rather than when the ugettext_lazy() function is called. In this example, ugettext_lazy() stores a lazy reference to the string – not the actual translation.

What is USE_I18N in Django?

Translation and formatting are controlled by USE_I18N and USE_L10N settings respectively. However, both features involve internationalization and localization. The names of the settings are an unfortunate result of Django's history.

How do I translate text in Django?

Use the function django. utils. translation. gettext_noop() to mark a string as a translation string without translating it.

What is gettext in Django?

gettext is a callable within the django. utils. translation module of the Django project.


1 Answers

We're currently in the process of figuring this out as well. While we haven't done so properly, we do have a rather annoyingly ugly hack to get around it.

We simply define a "dummy" function somewhere in the code (for example your models.py or even settings.py) and fill it up with all the strings that we need to have a translation for.

from django.utils.translation import ugettext_lazy as _, pgettext

def dummy_for_makemessages():
    """
    This function allows manage makemessages to find the forecast types for translation.
    Removing this code causes makemessages to comment out those PO entries, so don't do that
    unless you find a better way to do this
    """
    pgettext('forecast type', 'some string')
    pgettext('forecast type', 'some other string')
    pgettext('forecast type', 'yet another string')
    pgettext('forecast type', 'etc')
    pgettext('forecast type', 'etc again')
    pgettext('forecast type', 'and again and again')

This function is never called but simply defining it prevents the message strings from getting commented out by makemessages.

Not the most elegant solution but it works.

like image 66
StFS Avatar answered Nov 15 '22 23:11

StFS