7

I am translating a django website to 6 languages. This is the first time I have worked on translating.

It is an e-commerce site. How do I translate the model's fields? eg: category name which is actually in the db and is not getting written into po file when I try {% trans cat.name %} or ugettext(cat.name)

  • 1
    Good question. Here's a similar question asked: http://stackoverflow.com/questions/2019364/django-whats-the-best-recommended-way-to-translate-db-values which points to a django app that might solve your problem. – Yuji 'Tomita' Tomita Dec 17 '11 at 07:04

7 Answers7

4

Use verbose_name:

class Book(models.Model):
    title = models.CharField(verbose_name=_('Title'),max_length=50)

    class Meta:
       verbose_name = _('Book')
       verbose_name_plural = _('Books')

Now when you pull the translations, you'll get Book, Title and Books as translatable strings.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
3

You need something like https://github.com/geomin/django-lingua

mik_os
  • 670
  • 1
  • 6
  • 10
2

What's missing from all the answers, is which type of gettext you should use. It turns out that you need to use gettext_lazy, here's my working code

from django.utils.translation import gettext_lazy as _

class UnitCategory(models.Model):
    id = models.AutoField(verbose_name=_('Category ID'), primary_key=True)
    type = models.CharField(verbose_name=_(
        'Category Type'), max_length=30, blank=False)

    class Meta:
        verbose_name = _('Unit Category')
        verbose_name_plural = _('Unit Categories')

    def get_absolute_url(self):
        return reverse('core:units_categories_update', args=[self.id])
Rami Alloush
  • 2,308
  • 2
  • 27
  • 33
2
from django.utils.translation import ugettext as _
class Book(models.Model):
    title = models.CharField(_('title'),max_length=50)

you can also do it this way. title will become a translatasble string

mossplix
  • 3,783
  • 2
  • 26
  • 31
0

Despite being old this project seems to be the most used and well maintained solution:

https://github.com/deschler/django-modeltranslation/

https://django-modeltranslation.readthedocs.io/en/latest/

David Schumann
  • 13,380
  • 9
  • 75
  • 96
0

Option A:

It's quite simple actually. Assuming you don't want to keep reviewing your locale files (or relying on 3rd party edits like rosetta) which can be tedious, you can simply translate all your model-fields dynamically. One way to do this is via the app: django-parler, which allows you to leverage its TranslatableModel, TranslatedFields classes. ----More info on this can be found in the book Django 3 by Example: Chapter 9 (very exhaustive).

Option B

If your options are constricted by choices (IT HAPPENS)...& your model fields rely on a list of pre-determined choices, you can simply make the choices translatable like so:

in models.py

from django.utils.translation import gettext_lazy as _     
class Products(models.Model):
  class ProductName(models.TextChoices):
  RAD = 'RD', _('Radio')
  TEL = 'TV', _('Television')

details: [https://docs.djangoproject.com/en/3.1/ref/models/fields/][1]

or if you're using enums

in enums.py

from django.utils.translation import gettext_lazy as _
@unique
class StudentChoice(Enum):
  rad = _('Radio')
  tel = _('Television')

After defining your choices, you simply follow the internationalization/localization recommendations for the framework.

Magere
  • 99
  • 6
0

There is another popular project called django-parler. I found it overcomplicated a bit but definitely worth to have a look.

P.S. supports Django 4 but latest update was in November 2021