0

Is it possible make translations for Django objects?

Let's say I have model Category and Product. When user adds new product, they have to choose from Category objects. I would like to translate those names.

class Category(models.Model):
    name = models.CharField(max_length=100, verbose_name=_('Category'))

    def __unicode__(self):
        return u'{}'.format(self.name)

    class Meta:
        verbose_name_plural = _('Categories')

class Product(models.Model):
    user = models.ForeignKey(User, verbose_name=_('Company'), related_name='products')
    name = models.CharField(max_length=200, verbose_name=_('Name'),)
    category = models.ForeignKey('Category', verbose_name=_('Category'), related_name='products')

    class Meta:
        verbose_name = _('Product')
        verbose_name_plural = _('Products')

    def __unicode__(self):
        return u'{}'.format(self.name)

So before running the server, I create couple of categories like 'Electronics'.

What should I do to make name 'Electronics' translated to name 'Elektronika' for SK language?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • Does this [SO answer](http://stackoverflow.com/a/2030653/2689986), answer your question? – shad0w_wa1k3r Feb 06 '17 at 12:01
  • You mean you want to translate user entered inputs?.. – Sayse Feb 06 '17 at 13:00
  • @AshishNitinPatil It seems good, I will try it. – Milano Feb 06 '17 at 13:08
  • @Sayse No user entered, but kind of.. admin creates a categories with names in english and I want to translate those names, somethink like there was: english_name, czech_name, german_name etc in the Category model. – Milano Feb 06 '17 at 13:09
  • 1
    Possible duplicate of [The best/recommended way to translate Django database values](http://stackoverflow.com/questions/2019364/the-best-recommended-way-to-translate-django-database-values) – Sayse Feb 06 '17 at 13:10

1 Answers1

0

I assume that you put from django.utils.translation import ugettext_lazy as _ in the models.py. Next thing to do is set language and locale settings to settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LANGUAGES = (
    ('en', ugettext('English')),
    ('tr', ugettext('Turkish')),
    ('fr', ugettext('French')),
)
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),

)

After that you must put 'django.template.context_processors.i18n', into your context_processors in the settings.py. If you do all of that, all you have to do is run the following commands in terminal;

django-admin makemessages --locale=tr
django-admin compilemessages --locale=tr

When you do that django will create folder called locate into your project directory. Inside of the locale directory, you will see language codes that you apply makemessages into. enter image description here

Inside that you can edit django.po files. django.po file will be look like this.

msgid "Content"
msgstr "İçerik"

After you translate "Content" to "İçerik"(in Turkish), you should do makemessages and compilemessages again. After that whenever django sees "Content" while your site language is in Turkish(in this example), it will render it to you as "İçerik".

Brkyrn
  • 387
  • 3
  • 11