Django Translation: Agile Localization for Devs
In this article, you can read about Django translation basics, the importance of a localized version of the app for your users, and how Crowdin can help you to implement an agile localization workflow.
Join the DZone community and get the full member experience.Join For Free
Let’s discover Django translation basics and the importance of a localized app version.
If the app language is not handled automatically by your location, you will usually check the language switcher first.
Furthermore, it became a standard expectation for any top-tier app to have a multilingual interface.
The topic of this article is the localization peculiarities of Django-based apps and how you can localize your web app efficiently, considering the features you currently have and future updates as well.
Connect GitHub, GitLab, Bitbucket, and More
Business is global, application users are worldwide. So, the software must be as well. Developers who use continuous localization can make an app global quickly and easily.
Integrating localization into your development workflow is the key. The best way to do this is to use a localization service that integrates with your software repository.
Integration ensures that all the new texts from pull requests are sent to localization right away, and 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.
When you have multiple versions of your product or just want to minimize deployment delays, you can try our version’s management feature. It allows you to create branches. Branches enable translators to collaborate and 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.
It's possible to integrate Crowdin with your version control system (VCS) as well as create branches manually.
Read more about versions management and branch creation.
Django - GetText Translation Basics
If you’re interested in implementing 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.
How to Get Started
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 mark and “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.
Translation works on computed values and variables too.
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:
The comment will appear in the resulting .po file above the translatable piece.
- 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.
The .po file will show the following:
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:
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:
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
A context-based translation process makes the job easier for translators and ensures a high quality localized version.
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.
Published at DZone with permission of Diana Voroniak. See the original article here.
Opinions expressed by DZone contributors are their own.