読者です 読者をやめる 読者になる 読者になる

似非プログラマのうんちく

「似非プログラマの覚え書き」出張版

巳年じゃないけど Python やろうぜ(その 17)

Python Django

国際化と翻訳

多くのフレームワークがそうであるように、Django にも国際化の仕組みが備わっています。

下準備

あらかじめ settings.pyUSE_I18NTrue になっていることを確認します(デフォルトでそうなっていると思います)。

今回は customer_search に適用してみます。customer_search フォルダの直下に locale フォルダをあらかじめ作成しておきます。

翻訳するフレーズをマークする

customer_search/forms.py

from django import forms
from .models import Customer
from django.utils.translation import ugettext as _


class CustomerForm(forms.Form):
    customer_id = forms.IntegerField(
        label=_('Customer ID'),
        widget=forms.TextInput(attrs={'size': 5}),
        min_value=1,
    )
    customer_name = forms.CharField(
        required=False,
        label=_('Customer name'),
        widget=forms.TextInput(attrs={'size': 20, 'readonly': True}),
    )
    customer_phone = forms.CharField(
        required=False,
        label=_('Phone'),
        widget=forms.TextInput(attrs={'size': 20, 'readonly': True}),
    )

customer_search/templates/customer_search/search.html

{% load static i18n %}
{% trans 'customer search' as page_title %}
<!doctype html>
<html>
  <head>
    <title>{{ page_title | title }}</title>
    <link rel="stylesheet" href="{% static 'customer_search/css/search.css' %}">
  </head>
  <body>
    <h1>{{ page_title | title }}</h1>
    <form method="post" action=".">
      {% csrf_token %}
      <table>
        {% for field in form %}
          <tr>
            <th>{{ field.label_tag }}</th>
            <td>
              {{ field }}
              {% if field.errors %}
                <span class="error">
                  {% trans 'Error!' %}
                  {% for error in field.errors %}
                    {{ error }}
                  {% endfor %}
                </span>
              {% endif %}
            </td>
          </tr>
        {% endfor %}
      </table>
      <input type="submit" value="{% trans 'Search' %}">
    </form>
  </body>
</html>

テンプレート内では i18n を読み込むことにより trans という組み込みタグが使えるようになります。

メッセージファイルの作成とコンパイル

$ django-admin makemessages -l ja

を実行すると customer_search/locale/ja/LC_MESSAGES/django.po というファイルができています。

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-02-20 13:45+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"

#: customer_search/forms.py:8
msgid "Customer ID"
msgstr ""

#: customer_search/forms.py:14
msgid "Customer name"
msgstr ""

#: customer_search/forms.py:19
msgid "Phone"
msgstr ""

#: customer_search/templates/customer_search/search.html:2
msgid "customer search"
msgstr ""

#: customer_search/templates/customer_search/search.html:21
msgid "Error!"
msgstr ""

#: customer_search/templates/customer_search/search.html:31
msgid "Search"
msgstr ""

これを次のように編集します。19 行目までの部分は編集しないように気をつけてください。

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-02-20 13:45+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"

#: customer_search/forms.py:8
msgid "Customer ID"
msgstr "顧客ID"

#: customer_search/forms.py:14
msgid "Customer name"
msgstr "顧客名"

#: customer_search/forms.py:19
msgid "Phone"
msgstr "連絡先"

#: customer_search/templates/customer_search/search.html:2
msgid "customer search"
msgstr "顧客検索"

#: customer_search/templates/customer_search/search.html:21
msgid "Error!"
msgstr "エラー!"

#: customer_search/templates/customer_search/search.html:31
msgid "Search"
msgstr "検索"

編集が終わったら

$ django-admin compilemessages

を実行します。すると customer_search/locale/ja/LC_MESSAGES/django.mo というファイルが出来上がります。

確認してみる

LANGUAGE_CODE = 'en-us' のとき

LANGUAGE_CODE = 'ja-jp' のとき