How can we make django automatically translate languages stored in a Language model?

1k Views Asked by At

I'm having a peculiar issue where not all of the languages inside of my language model are being translated by Django automatically. Based on the current user locale, I want the language names to be translated into their language.

For example, if a French user was browsing the languages, the language Japanese would be translated to Japonais instead of 日本語.

My language model is the following:

class Language(models.Model):
    name = models.CharField(
        max_length=20,
    )

    short_name = models.CharField(
        max_length=20,
    )

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['name']

Now it seems to me that Django should already have all the languages translated into all of the different languages? If this is the case, how can I accomplish this? It would save so much time for my volunteer translators to not have to translate every single language.

I use Rosetta as the translation interface, so when I set this up, I had to manually add the supported languages to the base settings like this:

LANGUAGES = [
    ("af", _("Afrikaans")),
    ("ar", _("Arabic")),
    ...
    ("he", _("עברית")),
]

The languages added above are the languages that the volunteers are interested in and want to translate. Not all of the languages are listed there, and I'd say there are roughly 30 or so different languages.

I really don't want to waste their time, so is there a way, I can get Django to automatically translate all of the languages, so that the translator would not have to?

Edit:

Outside of SO, Nick suggested that this can be accomplished by making the following changes:

from django.utils.translation import gettext as _
return _(self.name)

I have edited by models.py with these two lines in mind, but still not all of the languages are being translated:

from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext as _g
from django.core.exceptions import ValidationError

from notifications.models import Notification


class Language(models.Model):
    name = models.CharField(
        max_length=20,
    )

    short_name = models.CharField(
        max_length=20,
    )

    def __str__(self):
        return _g(self.name)

    class Meta:
        ordering = ['name']

Please note the following line in my models.py:

from django.utils.translation import gettext as _g

I imported it as _g because I did not know if it would interfere with the other gettext import statement.

It's rather difficult to understand the problem, so I have taken a screenshot of the problem: Image of the problem

If it helps, I have manually added all the languages in my language model based on the following: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

1

There are 1 best solutions below

1
On

Create file utils/translation.py

from django.utils.translation import ugettext
from django.utils import translation

def get_translation(text: str, locale: str):
    translation.activate(locale)
    val = ugettext(text)
    translation.deactivate()
    return val

and then in your model.py

def __str__(self):
    from utils.translation import get_translation
    return get_translation(self.name, self.short_name)

Be sure that self.short_name is the same as rosetta short name

And you also need to translate the Languages in the rosetta.

en.po

msgid "English"
msgstr "English"

he.po

msgid "Hebrew"
msgstr "עברית"