translation in three different languages for portfolio website

Hello,

I am building a portfolio website with a lot of content and I would like to have the website content available in three languages (English, German and French). That what I have seen and done is really time consuming and really intensive and I hope there is a easier way of doing the translation.

My settings:

# settings.py

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.locale.LocaleMiddleware",
    "django.middleware.common.CommonMiddleware",
...
]

LANGUAGE_CODE = "en-us"
TIME_ZONE = "Europe/Paris"
USE_I18N = True
USE_L10N = True
USE_TZ = True
LANGUAGES = (
    ("en", _("English")),
    ("de", _("German")),
    ("fr", _("French")),
)
LOCALE_PATHS = [
    os.path.join(BASE_DIR, "locale"),
]

I have created the ‘locale’ folder in root directory, the ‘django.po’ files for German and French are generated with command: django-admin makemessages -a. For simple gettext_lazy translations in models.py files and in settings.py file is everything working fine. Even I think to translate everything by yourself is really time consuming! If there is an easier way of translating these texts, it would be very much appreciated if you can tell me.

What problem am I facing?

The view looks like:

#views.py

class IndexView(TemplateView):
    template_name = "index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["facts"] = Fact.objects.filter(published=True).values_list(
            "content", flat=True
        )
        return context

In index.html file I use a for loop to display facts.

      {% if facts %}
      <div class="row mt-4 container">
        <div class="col-lg-7 content">
          {% for fact in facts %}
          <ul>
            <li>
              <i class="bi bi-chevron-right"></i>
              {% blocktrans %}{{ fact }}{% endblocktrans %}
            </li>
          </ul>
          {% endfor %}
        </div>
      </div>
      {% endif %}

For the index.html file, I get this ‘template’ (I do not know how to call it) after I use django-admin makemessages -a

#: app/templates/index.html:21
#, python-format
msgid "%(fact)s"
msgstr ""

Now my question: How do I use this ‘template’ to create 3 facts in the django.po file?

What have I tried?

# django.po

#: app/templates/index.html:21
#, python-format
msgid "%(enter the text)s"
msgstr "%(gebe die Übersetzung ein)s"

#: app/templates/index.html:21
#, python-format
msgid "%(enter the second text)s"
msgstr "%(gebe die zweite Übersetzung ein)s"

#: app/templates/index.html:21
#, python-format
msgid "%(enter the third text)s"
msgstr "%(gebe die dritte Übersetzung ein)s"
# django.po

#: app/templates/index.html:21
#, python-format
msgid "enter the text"
msgstr "gebe die Übersetzung ein"

#: app/templates/index.html:21
#, python-format
msgid "enter the second text"
msgstr "gebe die zweite Übersetzung ein"

#: app/templates/index.html:21
#, python-format
msgid "enter the third text"
msgstr "gebe die dritte Übersetzung ein"

output when using command: django-admin compilemessages:

File “/home/doro/Desktop/django_portfolio/.venv/lib/python3.11/site-packages/django/contrib/flatpages/locale/af/LC_MESSAGES/django.po” is already compiled and up to date.
CommandError: compilemessages generated one or more errors.

this red message CommandError: compilemessages generated one or more errors. is at the end of the command and no indications why, where, etc.

Is it possible to generate with this html template the complied messages?

Thanks in advance,
Doro

The question is: translating “static” text is one thing.
Translating content that is on your database, is another thing.
You can use something like: django-modeltranslation to that

That was a really nice advice. Thank you very much.

I can recommend this tool. It was really simple to use.

  • install the package
  • add the package into INSTALLED_APPS in settings.py
  • create the translation.py files and add the fields which should be translated
  • add the TranslationAdmin into admin.py file
  • makemigrations
  • migrate

and that was it…

1 Like

I have never used it, but always considered using it.
Good to know that it worked for you!
You can always find some awesome django packages on django-packages