Django Multilingual interface Django.How

Author avatar wrote on 26/05/2022

1.Change ‘Setting.py’ of our project


#default langueage of the site
LANGUAGE_CODE = 'en'



# list of avaible languages
LANGUAGES = (
    ('en', 'Englich'),
    ('ru', 'Русский'),
)

# enable django translate system
USE_I18N = True

# set location of files with translations
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

MIDDLEWARE = [
  ...
  'django.middleware.locale.LocaleMiddleware',
  ...
]

 

2.Change ‘urls.py’ of our project:


from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns(
    ...
    path('', include('pages.urls')),
    path('accounts/', include('accounts.urls')),  
    path('admin/', admin.site.urls),
    ...
    prefix_default_language = False
)

 

3.Create ‘local’ folder in our project root

Now it’s working. To set place for translation, in template use it in different ways:

When you create all interface run command in terminal


django-admin.py makemessages -l ru

 

!Set lang you want to create translation at the end

After this django will generate some files ‘*.po’ in our ‘local’ folder. In this files you can set translation for all words you set by ‘{% trans %}’ tag.

At the end run ‘django-admin.py compilemessages’ command in our terminal

Django translation tutorial: https://docs.djangoproject.com/en/3.0/topics/i18n/translation/

Important for windos need to install ‘gettext’

to do thi just download 2 packages from https://download.gnome.org/binaries/win32/dependencies/

  • gettext-runtime-X.zip
  • gettext-tools-X.zip

Where ‘X’ is version (i have install 0.17)

Unzip them to ‘C:\Program Files\gettext-utils’ (need only bin folders, put them all together)

and add ‘;C:\Program Files\gettext-utils\bin’ our PATH at the end

More detaile here – https://overcoder.net/q/215868/как-установить-gnu-gettext-gt-015-в-windows-так-что-я-могу-создавать-файлы-po