Django i18n: Agile Localization for Developers

8 mins read

Django Translation: Agile Localization for Developers

Let’s discover Django i18n and translation basics and the importance of a localized version of the app for your users.

A language switcher is usually the first or one of the first things you check if the app language is not handled automatically by your location. Moreover, it became a standard expectation for all top-tier apps to have a multilingual interface.

In this article, we’ll cover the localization peculiarities of Django-based apps and how to localize your web app efficiently, considering not only the app features you have now, but the future updates too.

Localize Continuously From GitHub, GitLab, Bitbucket, and More

Business is worldwide, and application users are worldwide. So, the software must be as well. Developers who use continuous localization can make an app global quickly and easily.

The secret is to integrate the localization process into your development workflow. The best way to do this is to use a localization service that integrates with your software repository.

In Crowdin, you can integrate with GitHub, Bitbucket or Crowdin Console Client (CLI) that allows you to integrate with GIT, SVN, Mercurial, and more.

Integration ensures that:

  • all the new texts from pull requests are sent to localization right away,
  • all the translations are downloaded automatically and available for review at your repo.

And the best news, all of these steps are synchronized and automated.

Branches

When you have multiple versions of your product or simply want to minimize deployment delays, you can try our version’s management feature that allows you to create branches. This allows translators to work in parallel with the development team.

How? Simply put, every string created or modified by the developer becomes available to the translation team almost immediately, even if several teams are working on different app improvements.

You can integrate Crowdin with your version control system (VCS) or create branches manually.

Read more about versions management and branch creation.

Django - GetText Translation Basics

If you’re interested in how to implement localization in Django – the popular Python web framework, let’s go over the basics with us. We’ll cover the key principles of Django localization and what you need to get started.

i18n Django: The First Steps

First and foremost, you have to enable localization of your Django project.

For this, install gettext and edit the base.py settings file to make sure that USE_L10N = True.

Now, you can “tell” Django which strings you want to translate later.

To specify which parts of the app should be translated or formatted for local languages, you need to specify a translation string using the function gettext() or import gettext utility with an underscore used as an alias.

In the following example, you can see that the text “Welcome to my app.” is marked as a translation string.

from django.http import HttpResponse
from django.utils.translation import gettext as _

def my_view(request):
    output = gettext("Welcome to my app.")
    return HttpResponse(output)

Translation works on computed values and variables too.

def my_view(request):
    sentence = 'Welcome to my app.'
    output = gettext(sentence)
    return HttpResponse(output)

Django i18n: Context for Translators

To give translators hints about a translatable string, you can add a comment. It should be prefixed with the Translators keyword. For example:

def my_view(request):
    # Translators: This message appears on the home page only
    output = gettext("Welcome to my app.")

The comment will appear in the resulting .po file above the translatable piece.

#. Translators: This message appears on the home page only
# path/to/python/file.py:123
msgid "Welcome to my app."
msgstr ""

More details:

  • msgid is the translation string, which appears in the source. You shouldn’t change it.
  • msgstr is where you put the translation. The translation starts blank and needs to be filled in. Don’t forget to keep quotes around your translation.
  • PO Gettext at Crowdin.

For easier translation of multiple-meaning words, use contextual marker – django.utils.translation.pgettext() function.

For example, the word “email” in English refers to a noun and a verb. Here, you can use django.utils.translation.pgettext() function. The context will appear on the msgctxt line in the resulting .po file.

from django.utils.translation import pgettext

noun = pgettext("noun", "email")

The .po file will show the following:

msgctxt "noun"
msgid "email"
msgstr ""

No-op Strings

To mark a string as a translation string without translating it, use the function django.utils.translation.gettext_noop().

Create Translation Files

To create the translation files for every locale you want to support, you need to:

  • Run:
 $ django-admin makemessage -l fr

You can replace fr (French language code) with the locale code of any language you like to add.

  • Fill the translations at the msgstr strings.

  • Compile everything by running $ django-admin compilemessages when the translation is ready.

To check your translations, you have to change the language code inside /settings.py:

/settings.py

LANGUAGE_CODE = 'fr'

When you open your application inside the browser, you should see the French language version.

Here it is. Now you know the main aspects of Django localization. To find more details, visit the official Django documentation.

Context Determines the Quality

Context not only makes the translation process easier for translators but ensures the quality of the localized version of your app.

By context, we mean that a word or sentence cannot be interpreted without considering the environment in which it is used. Because of this, effective translation often involves analyzing the context of the source text.

Context of the original text is especially important when sentences are short or consist of 1 -2 words, as often happens in web applications.

To make it easier for you to provide context to translators and reduce manual actions, Crowdin has created an In-Context Localization tool.

The tool provides an overlay for your app and allows translators to work in real-time. In-context localization is connected with the actual project created in Crowdin, which contains the translatable files. Translators can work as if in the real app interface and preview the translations they make right there. Translation files are stored within your Crowdin project, and you can decide when to pull them to your application(for example, you can pull them only after the approval).

Read our article to learn more about the In-Context tool, and other ways to provide context for translators in Crowdin.

In-Context

Localize Your Product with Crowdin

With Crowdin, you can automate localization, release several multilingual versions of your app simultaneously, and provide an enhanced experience for your global customers. To get started, register a Crowdin or Crowdin Enterprise account.

Reach new markets with Crowdin

Find out how you can build a successful localization workflow. Start your free 14-day trial.
Diana Voroniak

Link
Previous Post
What's New at Crowdin: October 2021
Next Post
Celebrating 2 Million Users