DEV Community 👩‍💻👨‍💻

Cover image for Django Internationalization Tutorial-2
ajitdhulam for Python Discipline @EPAM India

Posted on • Updated on

Django Internationalization Tutorial-2


Depth about settings and translation from view.py file

In the previous tutorial of Django Internationalization, we developed a basic view that returns a message defined in the output variable.

In this post our objective will be to execute the interpretation of that message. For this we will utilize the Django project made in the past post.

Settings configuration:

Naturally, Django projects made with preparation are as of now set up for internationalization (i18n) and confinement (l10n). Notwithstanding that, open the venture settings at langtests/settings.py and ensure that USE_I18N and USE_L10N are set to true

# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'

USE_I18N = True
USE_L10N = True
USE_TZ = True
Enter fullscreen mode Exit fullscreen mode

One significant setting to note is the LANGUAGE_CODE, which characterizes the default language for the whole project. As a matter of course, it is set to 'en-us' nevertheless later we will change it to 'pt-pt' to test that translation is working.

The second thing we should do is add the laguages application (The one we created in first tutorial) to the rundown of Installed apps in settings.py. Although we could get to the view without it, for projects it will be essential:

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'languages'
]
Enter fullscreen mode Exit fullscreen mode

Translation:
In Django, all translatable strings must be expressly stamped. In Python code, we determine interpretation strings by utilizing the ugettext() function or underscore (_) as alias of it. For this application, open languages/views.py and mark the result string as translatable.

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

def index(request):
    output = _('Welcome to my site.')
    return HttpResponse(output)
Enter fullscreen mode Exit fullscreen mode

Extra Changes: In addition to the import statement, the only significant change is the use of the underscore alias for ugettext.

Create Translation Files:
Since we have distinguished the translatable strings, we want to make the interpretation documents themselves.  In Django, translation files are called message files and have a .po file extension.

Django accompanies a device (makemessages) that robotizes the creation and updates of these documents. Even though you can execute this content from a few spots, we will save it basic for this instructional exercise and use it inside the lanagues application since it is the main application we have.

make sure to create the locale directory inside the language's directory (using mkdir locale).

For instance, let us create a message file for Portuguese. Inside the languages directory do:

django-admin makemessages -l pt_pt
Enter fullscreen mode Exit fullscreen mode

This will create the following structure in
the language's application.

Image description

Note: It is worth noting that I used an underscore for the language directory (pt_pt rather than pt-pt). Although we use dashes such as pt-pt in Django settings, we must use underscores in folders.

Writing Translation Messages:
Assuming you really look at the substance of django.po you will view the string prepared as meant Portuguese. In basic terms, the Django makemessages script looks for events of ugettext() and takes out all strings set apart for translation.

To do the translation, just fill in the content of msgstr:

#: views.py:5
msgid "Welcome to my site."
msgstr "Bem-vindo ao meu site"
Enter fullscreen mode Exit fullscreen mode

After translating all strings, we must compile the strings with the compilemessages script:

django-admin compilemessages
Enter fullscreen mode Exit fullscreen mode

A new file django.mo will appear next to the message file.

Image description

Testing Sample Translation
Although everything appears to be prepared for interpretations, assuming we direct the program toward http://localhost:8000/languages/, we are going to get the "Welcome to my site." string in English. This is on the grounds that we have not indicated anyplace to which language we ought to interpret the string.

# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/


LANGUAGE_CODE = 'pt-pt'
TIME_ZONE = 'UTC'

USE_I18N = True
USE_L10N = True
USE_TZ = True
Enter fullscreen mode Exit fullscreen mode

Now point the browser to http://localhost:8000/languages/:

Image description

Django will return the string translated into Portuguese, as Google translation giving option you are viewing in Portuguese and want to convert into English.

Disclaimer: This is a personal [blog, post, statement, opinion]. The views and opinions expressed here are only those of the author and do not represent those of any organization or any individual with whom the author may be associated, professionally or personally.

Top comments (0)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠