[PYTHON] Django 1.9 for internationalization

environment

Ubuntu 14.04.4 LTS Python 3.5.1 Django 1.9.7

Prerequisites

--Django is running in a Japanese environment

By following the steps below, if you prepare a translation file, the language will be automatically selected according to the language environment of your browser.

procedure

Changes to settings.py

--In MIDDLEWARE_CLASSES 'django.middleware.locale.LocaleMiddleware', Added --Add the following to the file

```python:settings.py
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
```

Template change

--Added {% load i18n%} --Change the part you want to translate to {% trans'hoge'%} --If there is no translation data, hoge will be displayed.

Creating a translation list file

--Execute the following on the command line (for English to Japanese)

```
mkdir /DJANGO_BASE_DIR/locale
django-admin.py makemessages -l ja
```

--When executed, / DJANGO_BASE_DIR / locale / ja / LC_MESSAGES / django.po is created.

--Edit the generated django.po

```python:django.po
#: path/to/template/index.html:4
msgid "hoge"
msgstr ""
```

In this way, a file including the file name and line number of the part specified as {% trans'hoge'%} etc. in template is prepared, so specify the translated character string in msgid.

Compiling translation files

--Execute the following on the command line

```
python manage.py compilemessages
```

--When executed, a compiled /DJANGO_BASE_DIR/locale/ja/LC_MESSAGES/django.mo is created.

Addendum: Priority of language setting detection (posted contents of official document)

  1. First, look for the language prefix in the requested URL. This is only done if you use the ʻi18n_patterns function in the root ʻURLconf. Reference: Switch the language displayed in Django 1.9

  2. Look for the django_language key in the current user's session.

  3. Look for cookies. The name of the cookie is set to the value of the LANGUAGE_COOKIE_NAME setting. (The default name is django_language.)

  4. Look at the Accept-Language HTTP header. This header is sent from the browser and tells the user's desired language in order of priority. Django looks at all the languages passed in until it finds a translation available.

  5. If that also fails, use the global LANGUAGE_CODE setting.

Recommended Posts

Django 1.9 for internationalization
Internationalization with django
Django
[Note] [For myself] Django command
Launch notes for existing Django applications
[For beginners] Django -Development environment construction-
Start Django for the first time
Commands for creating SNS with Django
django update
Django note 4
django search
Django installation
Django Summary
Django test
Commands for creating a new django project
Build a TOP screen for Django users
Same-Site attribute setting for cookies in Django
Django # 2 (template)
Django Note 5
Django tutorial summary for beginners by beginners ③ (View)
Django hands-on
Touch django
django notes
Django Summary
Django basics
Django Shoho
Get query parameters for GET requests in Django
Django defaults
Django + Docker
Django Glossary
Django search
(For myself) Django_1 (Basic / Hello World / Template)
Install Django
Django: References
[Django] Settings for sending emails using postfix
Django Note 1
Django note 3
Django tutorial summary for beginners by beginners ⑤ (test)
Django note 2
Best practices for Django views.py and urls.py (?)
Django startup
Write a short if-else for Django Template
Django notes
Django NullCharField
Redo everything for the Django login screen
Django tutorial summary for beginners by beginners ⑦ (Customize Admin)
Django tutorial summary for beginners by beginners ⑥ (static file)
Django Tutorial Summary for Beginners by Beginners (Model, Admin)
Django tutorial summary for beginners by beginners ① (project creation ~)
Create a dashboard for Network devices with Django!
Django with Python Tools 2.2 for Visual Studio (PTVS 2.2)
Django tutorial summary for beginners by beginners ④ (Generic View)
Do an ambiguous search for mysql in Django