If you implement a service that is used in many countries or regions with Django, you will want a system that automatically translates by specifying the language of that region. django has a useful translation function, so I'll try using that function.
Prepared here Use Minimum configuration website environment with django.
https://github.com/yu-sa/my_site I'll clone Django from.
# git clone https://github.com/yu-sa/my_site.git
# cd my_site
# pip install virtualenvwrapper
# mkvirtualenv --python=/path/to/python/2.7.11/bin/python i18n
# pip install django==1.9
# pip install mysql-python
# mysql -u root
> CREATE DATABASE my_site;
> exit
# python manage.py migrate
# python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
April 08, 2016 - 17:46:13
Django version 1.9, using settings 'my_site.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
http://127.0.0.1:8000/views/ When you access, a page like the one below will be displayed.
At this point, both the page title and the page body are in Japanese. Let's fix this so that it will be automatically converted to English.
Use django-admin-tooles to create a translation list, so install
pip install django-admin-tools
Create locale folder in the specified hierarchy
my_site
└─my_site
└─locale
Set the path of the local folder in settings Added "django.middleware.locale.LocaleMiddleware" to MIDDLEWARE_CLASSES. Please note that you can only go to the position to add as follows.
settings.py
# locale
LOCALE_PATHS = (
os.path.join(FILE_DIR, 'locale'),
)
…………
MIDDLEWARE_CLASSES = [
………,
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
………,
]
Create a message file (django.po) needed for translation Creates a translated message file for the language specified in the -l option. en is English. For Japanese ja.
# django-admin.py makemessages -l en
processing locale en
In django.po, you'll find the words "TOP Page" and "Hello World" currently displayed on the page. I will put the translated characters of these two strings.
django.po
#: my_site/views/index.py:13
msgid "TOP page"
msgstr "Top Page"
#: my_site/views/index.py:14
msgid "Hello World"
msgstr "Hello world"
Then compile this message file to create django.mo.
# django-admin.py compilemessages
You can choose what language you want the body in html to display Set up the form.
index.html
………
<span>
<form action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}" />
<select name="language">
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}" {% if request.LANGUAGE_CODE == language.code %}selected{% endif%}>{{ language.name_local }} ({{ language.code }})</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>
</span>
………
Register the url set on the form side
my_site/views/urls.py
from django.conf.urls import url, include
from .index import IndexView
urlpatterns = [
url(r'^$', IndexView.as_view(), name='api-index'),
url(r'^i18n/', include('django.conf.urls.i18n')),
]
First, the translation flow is as follows
Create a message file for the language you want to translate
↓
Enter the translation content in the message in the message file
↓
Compile message file
↓
Translate the wording into the language you set by setting the translation template tag
Create a message file for the language you want to translate Create a django.po file using the makemessages command.
Enter the translation content in the message in the message file Translate the list of translated words in the django.po file.
Compile message file Compile with the compile messages command. Create a django.mo file
Set translation template tags to translate words into any language you set. I prepared it in the template of github in advance, but I prepared it on the template side If you specify a translation wording in the {% trans%} template tag, django will automatically translate that wording. In addition, you can set the information of the language you want to translate in the form set in index.html above to session on the django side. However, if you do not create a translation file, translation will not be performed, so if you have a language you want to translate, try creating it again with the make messages command.
GitHub https://github.com/yu-sa/my_site/tree/i18n
I have uploaded the source code implemented this time to branch i18 of the my_site repository. If you are interested, please try cloning.
Recommended Posts