Reputation:
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)
Upvotes: 9
Views: 15351
Reputation: 53
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
Upvotes: 0
Reputation: 99
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.
Upvotes: 0
Reputation: 14793
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/
Upvotes: 0
Reputation: 2626
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])
Upvotes: 2
Reputation: 3865
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
Upvotes: 2
Reputation: 174614
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.
Upvotes: 4