Michael
Michael

Reputation: 4461

Django: translation in templates not working

Django 1.11

I can't make Django translate templates in my project. Messages were compiled and recompiled.

Could you give me a kick here?

settings.py

INSTALLED_APPS = [
...
    'frame',
...
]

LANGUAGE_CODE = 'ru-RU'
USE_I18N = True

frame/templates/frame/frame_form.html

{% extends 'general/cbv/general_form.html' %}
{% load i18n %}

{% block title %}
    <h1>{% trans "Create frame" %}</h1>
{% endblock %}

frame/locale/ru_RU/LC_MESSAGES/django.po

#: templates/frame/frame_form.html:5
msgid "Create frame"
msgstr "Создать сюжет"

tree

├── frame
...
│   ├── locale
│   │   └── ru_RU
│   │       └── LC_MESSAGES
│   │           ├── django.mo
│   │           └── django.po
...
│   ├── templates
│   │   └── frame
│   │       ├── frame_form.html

Upvotes: 6

Views: 9862

Answers (1)

ccsv
ccsv

Reputation: 8649

Check for the following:

Settings.py

In your settings.py

1) Make sure Middleware classes is in the following order

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

]

2) Set your language code default:

LANGUAGE_CODE = 'en-us'

USE_I18N = True

3) Put the language you want to translate to a list can be found here

from django.utils.translation import gettext_lazy as _

#Example for English and German
LANGUAGES =[ ('en', _('English')),
('de', _('German')),]

4) Set your Locale path folder (This is where your language translations will appear)

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

URL.py

Check you have this link in the main URL conf file url.py

urlpatterns = [
    path('', include('YOUR APP.urls')),
    path('admin/', admin.site.urls),
    path('i18n/', include('django.conf.urls.i18n')),  #Make sure this is present
]

HTML template

After you have this checked and your HTML template done with the {% load i18n %} before any of the {% trans "FOO" %} on your html page and some way to change the languages. You might also want something like this dropdown: Django change language in html to debug language changes

Creating and modifying .po files

1) Create django.po files in the locale directory that you specify in the settings.py (LOCALE_PATHS) using the following command in the command line to create the .po files for example english would be:

   python manage.py makemessages -l en   #en = english replace en for other languages

This creates a directory with YOUR_LANGUAGE/LC_MESSAGES/django.po

Note that some languages will not show up unless it is capitalized for some reason such as simplified chinese zh-Hans instead of zh-hans not really sure why.

2) Go into the django.po of the language you want to change and change the msgstr ""

msgid "Create frame"
msgstr "Создать сюжет" #This area had translated string added

3) Compile the messages and restart the server in the commandline

# Compile messages 
django-admin compilemessages #this creates a django.mo file in LC_MESSAGES folder

#Run server
python manage.py runserver

The text should change when you switch languages using the dropdown menu or whatever way you chose.

4) If you add new translation tags you have to remake the messages and recompile them in the commandline and then runserver

#Make messages for all your languages
django-admin makemessages -a

# Compile messages
django-admin compilemessages

Upvotes: 15

Related Questions