Skip to content

PhungXuanAnh/django-multi-language-sample

Repository files navigation

Django multiple language translate

##content 1. Add configuration for translate to settings 2. Change model for translate 3. Change templates for translate 4. Make translate files 5. Force default language for site 6. References


1. Add configuration for translate to settings

In settings.py, add the following(In my case I created folder named locale inside BASE_DIR you can choose another location and specify the location in settings.py).

from django.utils.translation import gettext_lazy as _

LANGUAGE_CODE = 'vi-VI'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

LANGUAGES = (
    ('vi', _('Vietnamese')),
    ('en', _('English')),
)

MULTILINGUAL_LANGUAGES = (
    "en-us",
    "vi",
)

Add to midlleware:

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

2. Change model for translate

Add:

from django.utils.translation import gettext_lazy as _

Change any text that you want to translate as below sample:

verbose_name = "Question"

change to:

verbose_name = _("Question")

Click here for sample file

3. Change templates for translate

Add:

{% load i18n %}

Change any text that you want to translate as below sample:

Welcom to django admin dashboard

change to:

{% trans "Welcom to django admin dashboard" %}

Click here for sample file

4. Make translate files

Create all locale directories same as you delared at LOCALE_PATHS above

Run the makemessages command from any one of the 2 places based on the requirement as the command looks for the translation texts through all the child directories from which it is run.

a. Project's root directory where manage.py resides.

b. App's root directory where models.py, views.py resides.

Run command:

django-admin.py makemessages -l de
django-admin.py makemessages -l en
django-admin.py makemessages -l fr
django-admin.py makemessages -l es
django-admin.py makemessages -l pt

Click here to check the language codes

or run command for make all language:

django-admin.py makemessages --all

The above command will create a directory named ar inside locale directory with the following folder structure.

locale
└── vi
    └── LC_MESSAGES
        └── django.po

A small piece in django.po

#: templates/admin/base_site.1.html:12 templates/admin/base_site.html:16
msgid "Welcom to django admin dashboard"
msgstr "Chao mung den voi bang dieu khien django admin"
  • msgid contain the origin text
  • msgstr you must fill your translated text here

Now run the compilemessages from the same directory as mentioned above, it will generate django.mo file.

django-admin.py compilemessages

The directory structure of loacle directory will look like this.

locale
└── vi
    └── LC_MESSAGES
        ├── django.mo
        └── django.po

Start server:

python manage.py runserver

Visit http://127.0.0.1:8000/en/admin/ to view/access the English based Admin site Visit http://127.0.0.1:8000/vi/admin/ to view/access the Vietnam based Admin site

5. Force default language for site

Create middleware.py in project directory as link

Modify in settings.py:

  • Add midlleware:
MIDDLEWARE = [
    'django_translation_sample.middleware.force_default_language_middleware',
    'django.middleware.locale.LocaleMiddleware',
  • change language code to language that you want to set as default
LANGUAGE_CODE = 'vi'

Note: django_translation_sample.middleware.force_default_language_middleware must be declared before django.middleware.locale.LocaleMiddleware

Start server:

python manage.py runserver

Visit http://127.0.0.1:8000/admin/ you will be redirected to Vietname page: http://127.0.0.1:8000/vi/admin/

Use can access English version by visit http://127.0.0.1:8000/en/admin/

6. References

https://docs.djangoproject.com/en/2.0/topics/i18n/translation/ https://djangobook.com/localization-create-language-files/ https://djangobook.com/internationalization-python-code/ https://djangobook.com/internationalization-template-code/ https://djangobook.com/internationalization-javascript-code/ http://www.marinamele.com/taskbuster-django-tutorial/internationalization-localization-languages-time-zones

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published