2020 Gilbish Kosma

Multilanguage in Django

How to use multilanguage in django?

Django Multilanguage

How to use multiple languages in your Project

Suppose you are creating a site for one of your client, and suddenly he says i need the site to support Russian + German + English. Now you need to do that cause he wants.

so let's start XD

At starting the django doc for language translation looks a bit hard. But after this article it will look like a piece of cake.

There are two things which comes in handy for language translation

1) ugettext 2) ugettext_lazy

set languages

	LANGUAGES = (
    ('en-us', _('English')),
    ('lt', _('Lithunian')),
    ('ru', _('Russian')),
)

select a default language

	LANGUAGE_CODE = 'en-us'

set LOCAL_PATH in order to save

	LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

set this to true

	USE_I18N = True

	USE_L10N = True

add locale middleware after session and before common

	MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    ]

Now in first load {% load i18n %} in every template after that use {% trans 'string' %} : string will get replaced by default language

	<p>{% trans 'Hello World' %}</p>

Once it is done enter command to makemessages

	python manage.py makemessages -l ru  #lt for russian language 

It will create a .po file inside Locale directory it contain the msg id and msgstring

	msgid "company"
	msgstr "Įmonė" #company in russian

now once you filled manually all the msgstr

	python manage.py compilemessages

It will create a .mo file using which django will trnaslate the strings

Now try to load your site. It will choose the language according to your browser settings

How to not do entry from file what if client want do fill word alternative on his own

For this use: django-rosetta

It will create forms in admin panel to write alternate of words

	pip install django-rosetta

add it to installed apps

	'rosetta',

change the urls.py

	path('rosetta/',include('rosetta.urls')),

now try accessing the localhost/rosetta/ it will lead to the admin page to change words

#How to add language change form in frontend

use i18n_patterns

urls.py

path('i18n/', include('django.conf.urls.i18n')),

This url needs post request with language parameter

Post form:

	<form action="{% url 'set_language' %}" method="post">
	{% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}">
    <select name="language">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
            <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </select>
    <input type="submit" value="Go">
</form>

Now you are good to go