Ubuntu 14.04.4 LTS Python 3.5.1 Django 1.9.7
--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.
--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'),
)
```
--Added {% load i18n%}
--Change the part you want to translate to {% trans'hoge'%}
--If there is no translation data, hoge
will be displayed.
--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.
--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.
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
Look for the django_language
key in the current user's session.
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
.)
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.
If that also fails, use the global LANGUAGE_CODE
setting.
Recommended Posts