---
title: "翻訳"
version: 4.0
locale: ja
source: https://docs.djangoproject.com/ja/4.0/topics/i18n/translation/
canonical: https://djangodocs.dev/ja/4.0/topics/i18n/translation/
---
# 翻訳

## 概要

Django プロジェクトを翻訳可能にするためには、Python コードやテンプレートに、多少のフックを加筆しなければなりません。このフックは [翻訳文字列](/ja/4.0/topics/i18n/#term-translation-string) と呼ばれます。翻訳文字列は Django に「このテキストに対する翻訳が有効な場合、このテキストはエンドユーザの言語に翻訳すべきだ」ということを通知します。翻訳可能な文字列をマークすることは開発者の責任です。システムが翻訳できるのは指定された文字列だけなのです。

Django は、翻訳文字列を [メッセージファイル](/ja/4.0/topics/i18n/#term-message-file) に抽出するユーティリティを提供します。このファイルがあるため、翻訳者が対象の言語において翻訳文字列に対応する文字列を用意することが簡単になります。翻訳者がメッセージファイルの中身を書き終わったら、それをコンパイルしなければなりません。このプロセスは GNU gettext ツー ルキットに依存しています。

Once this is done, Django takes care of translating web apps on the fly in each
available language, according to users' language preferences.

Django の国際化フックはデフォルトで有効になっており、これは、フレームワークの特定の箇所で i18n に関する小さなオーバーヘッドが発生することを意味します。国際化を使わない場合は、 [`USE_I18N = False`](/ja/4.0/ref/settings/#std-setting-USE_I18N) を設定ファイルに記述してください。そうすれば Django は国際化の部品を読み込まないようにして多少の最適化をします。

> **Note**
>
> プロジェクトで翻訳機能が有効になっていることを確認してください (最も早いのは、 [`MIDDLEWARE`](/ja/4.0/ref/settings/#std-setting-MIDDLEWARE) が [`django.middleware.locale.LocaleMiddleware`](/ja/4.0/ref/middleware/#django.middleware.locale.LocaleMiddleware) を含むかどうかチェックすることです) 。まだの場合は、[How Django discovers language preference](#how-django-discovers-language-preference) を参照してください。

## Python コードでの国際化

### 標準的な翻訳

Specify a translation string by using the function
[`gettext()`](/ja/4.0/ref/utils/#django.utils.translation.gettext). It's convention to import this
as a shorter alias, `_`, to save typing.

> **Note**
>
> Python の標準ライブラリ `gettext` モジュールは、グローバルの名前空間に `_()` とい う名前で `gettext()` をインストールします。 Django ではこの方法に従っていませんが、それは以下の理由からです:
>
> 1. Sometimes, you should use [`gettext_lazy()`](/ja/4.0/ref/utils/#django.utils.translation.gettext_lazy)
>    as the default translation method for a particular file. Without `_()`
>    in the global namespace, the developer has to think about which is the
>    most appropriate translation function.
> 2. The underscore character (`_`) is used to represent "the previous
>    result" in Python's interactive shell and doctest tests. Installing a
>    global `_()` function causes interference. Explicitly importing
>    `gettext()` as `_()` avoids this problem.

> **_ として別名をつけられる関数は？**
>
> ([`makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) で使われる) `xgettext` が動作する方法のせいで、単一の文字列引数を持つ関数のみが `_` として import できます。
>
> - [`gettext()`](/ja/4.0/ref/utils/#django.utils.translation.gettext)
> - [`gettext_lazy()`](/ja/4.0/ref/utils/#django.utils.translation.gettext_lazy)

この例では、`"Welcome to my site."` というテキストが翻訳文字列としてマークされています:

```
from django.http import HttpResponse
from django.utils.translation import gettext as _

def my_view(request):
    output = _("Welcome to my site.")
    return HttpResponse(output)
```

You could code this without using the alias. This example is identical to the
previous one:

```
from django.http import HttpResponse
from django.utils.translation import gettext

def my_view(request):
    output = gettext("Welcome to my site.")
    return HttpResponse(output)
```

翻訳は、計算済みの値に適用されます。次の例は、上の2つの例とまったく同一です:

```
def my_view(request):
    words = ['Welcome', 'to', 'my', 'site.']
    output = _(' '.join(words))
    return HttpResponse(output)
```

翻訳は変数にも適用されます。次の例もまた、上の例とまったく同一です:

```
def my_view(request):
    sentence = 'Welcome to my site.'
    output = _(sentence)
    return HttpResponse(output)
```

(上の 2 つの例のように変数や計算済みの値を使う場合の注意点として、Django の翻訳文字列検出ユーティリティ [`django-admin makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) はこれらの文字列を検出できません。詳しくは後の [`makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) の節で説明します。)

The strings you pass to `_()` or `gettext()` can take placeholders,
specified with Python's standard named-string interpolation syntax. Example:

```
def my_view(request, m, d):
    output = _('Today is %(month)s %(day)s.') % {'month': m, 'day': d}
    return HttpResponse(output)
```

This technique lets language-specific translations reorder the placeholder
text. For example, an English translation may be `"Today is November 26."`,
while a Spanish translation may be `"Hoy es 26 de noviembre."` -- with the
month and the day placeholders swapped.

この理由から、引数が複数ある場合には、固定引数を使った補完 (`%s` や `%d`) ではなく、名前付きの補完 (`%(day)s`) を使ってください。固定引数による補完を使った場合、プレースホルダを正しい順番で翻訳できない場合があります。

Since string extraction is done by the `xgettext` command, only syntaxes
supported by `gettext` are supported by Django. In particular, Python
[f-strings](https://docs.python.org/3/reference/lexical_analysis.html#f-strings) are not yet supported by `xgettext`, and
JavaScript template strings need `gettext` 0.21+.

### 翻訳者へのコメント

翻訳可能な文字列についてのヒントを翻訳者に示したい場合、文字列の直前の行で `Translators` キーワードでプレフィックスされたコメントを追加できます。例えば:

```
def my_view(request):
    # Translators: This message appears on the home page only
    output = gettext("Welcome to my site.")
```

コメントは、後に記述された翻訳可能な文章に関連付けられた、結果の `.po` ファイル内に表示され、ほとんどの翻訳ツールで表示されます。

> **Note**
>
> 単に完全性のため、以下は結果の `.po` の断片と一致します:
>
> ```po
> #. Translators: This message appears on the home page only
> # path/to/python/file.py:123
> msgid "Welcome to my site."
> msgstr ""
> ```

これはテンプレート内でも動作します。詳しくは [Comments for translators in templates](#translator-comments-in-templates) を参照してください。

### no-op として文字列をマークする

Use the function [`django.utils.translation.gettext_noop()`](/ja/4.0/ref/utils/#django.utils.translation.gettext_noop) to mark a
string as a translation string without translating it. The string is later
translated from a variable.

ソース言語内に保持すべき構成文字列がある場合、これを使ってください。これは、システムやユーザー (たとえばデータベース内の文字列) を介して交換される一方で、最後の時点 (たとえばユーザに文字列が表示されるとき) で翻訳される必要があるからです。

### 複数形

Use the function [`django.utils.translation.ngettext()`](/ja/4.0/ref/utils/#django.utils.translation.ngettext) to specify
pluralized messages.

`ngettext()` takes three arguments: the singular translation string, the
plural translation string and the number of objects.

この関数は、Django アプリケーションを数と [複数形](https://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms) の複雑さが英語で 2 より大きい (その値にかかわらず、単数には 'object'、そして `count` が 1 ではない全ての場合に 'objects' となります。) 言語にローカル化させる必要がある場合に役立ちます。

例:

```
from django.http import HttpResponse
from django.utils.translation import ngettext

def hello_world(request, count):
    page = ngettext(
        'there is %(count)d object',
        'there are %(count)d objects',
        count,
    ) % {
        'count': count,
    }
    return HttpResponse(page)
```

この例では、オブジェクトの数値は `count` 変数として翻訳言語に渡されます。

複数形は、複雑で言語によって異なる動作をすることに注意してください。`count` と 1 の比較は正しいルールではないことがあります。以下のコードは洗練されて見えますが、いくつかの言語に対しては誤った結果を生み出します:

```
from django.utils.translation import ngettext
from myapp.models import Report

count = Report.objects.count()
if count == 1:
    name = Report._meta.verbose_name
else:
    name = Report._meta.verbose_name_plural

text = ngettext(
    'There is %(count)d %(name)s available.',
    'There are %(count)d %(name)s available.',
    count,
) % {
    'count': count,
    'name': name
}
```

Don't try to implement your own singular-or-plural logic; it won't be correct.
In a case like this, consider something like the following:

```
text = ngettext(
    'There is %(count)d %(name)s object available.',
    'There are %(count)d %(name)s objects available.',
    count,
) % {
    'count': count,
    'name': Report._meta.verbose_name,
}
```

> **Note**
>
> When using `ngettext()`, make sure you use a single name for every
> extrapolated variable included in the literal. In the examples above, note
> how we used the `name` Python variable in both translation strings. This
> example, besides being incorrect in some languages as noted above, would
> fail:
>
> ```
> text = ngettext(
>     'There is %(count)d %(name)s available.',
>     'There are %(count)d %(plural_name)s available.',
>     count,
> ) % {
>     'count': Report.objects.count(),
>     'name': Report._meta.verbose_name,
>     'plural_name': Report._meta.verbose_name_plural,
> }
> ```
>
> [`django-admin compilemessages`](/ja/4.0/ref/django-admin/#django-admin-compilemessages) を実行すると、以下のエラーが発生するでしょう:
>
> ```
> a format specification for argument 'name', as in 'msgstr[0]', doesn't exist in 'msgid'
> ```

### 文脈マーカー

単語は複数の意味を持っている場合があります。例えば、英語の `"May"` は、月名と動詞を指します。翻訳者が異なる文脈でこれらの単語を正しく翻訳できるようにするには、[`django.utils.translation.pgettext()`](/ja/4.0/ref/utils/#django.utils.translation.pgettext) 関数、もしくは文字列が複数形を必要とする場合は  [`django.utils.translation.npgettext()`](/ja/4.0/ref/utils/#django.utils.translation.npgettext) 関数を使うことができます。両方とも、第 1 変数として文脈文字列を取ります。

結果の `.po` ファイルで、文字列は同じ文字列に対する異なる文脈マーカーがあるのと同じ頻度で出現し (コンテキストは `msgctxt` 行に出現します)、これらそれぞれに対して翻訳者が異なる翻訳ができるようにします。

例:

```
from django.utils.translation import pgettext

month = pgettext("month name", "May")
```

もしくは:

```
from django.db import models
from django.utils.translation import pgettext_lazy

class MyThing(models.Model):
    name = models.CharField(help_text=pgettext_lazy(
        'help text for MyThing model', 'This is the help text'))
```

これは `.po` ファイル内に以下のように出現します:

```po
msgctxt "month name"
msgid "May"
msgstr ""
```

Contextual markers are also supported by the [`translate`](#std-templatetag-translate) and
[`blocktranslate`](#std-templatetag-blocktranslate) template tags.

### Lazy な翻訳

Lazy に -- 値が呼ばれたときではなくアクセスされたときに -- 文字列を翻訳するためには、[`django.utils.translation`](/ja/4.0/ref/utils/#module-django.utils.translation) (名前の `lazy` 接尾辞ですぐ分かります) の翻訳関数の lazy バージョンを使ってください、

これらの関数は、Lazy な文字列参照を保持します -- 実際の翻訳ではありません。翻訳自体はテンプレートレンダリング内のように、文字列が文字列コンテキスト内で使われるときに行われます。

これらの関数への呼び出しがモジュールのロード時に実行されるコードパスにある場合は、これは不可欠です。

これは、モデル、フォーム、モデルフォームを定義するときに簡単に発生することができるものです。これは、Django がフィールドを実際にクラスレベルの属性にするように実装するためです。 そのため、以下のような場合は lazy な翻訳を使用してください:

#### モデルフィールドとリレーションシップの `verbose_name` と `help_text` オプションの値

例えば、以下のモデルの *name* フィールドのヘルプテキストを翻訳するためには、以下のようにします:

```
from django.db import models
from django.utils.translation import gettext_lazy as _

class MyThing(models.Model):
    name = models.CharField(help_text=_('This is the help text'))
```

[`verbose_name`](/ja/4.0/ref/models/options/#django.db.models.Options.verbose_name) オプションを使えば、[`ForeignKey`](/ja/4.0/ref/models/fields/#django.db.models.ForeignKey)、[`ManyToManyField`](/ja/4.0/ref/models/fields/#django.db.models.ManyToManyField) や [`OneToOneField`](/ja/4.0/ref/models/fields/#django.db.models.OneToOneField) リレーションシップの名前を翻訳可能にできます:

```
class MyThing(models.Model):
    kind = models.ForeignKey(
        ThingKind,
        on_delete=models.CASCADE,
        related_name='kinds',
        verbose_name=_('kind'),
    )
```

[`verbose_name`](/ja/4.0/ref/models/options/#django.db.models.Options.verbose_name) と同様に、必要に応じて Django が自動的にタイトルケースに直すので、リレーションシップに対する小文字の冗長な名前テキストを提供する必要があります。

#### モデルの冗長な名前の値

フォールバックの英語中心とモデルのクラス名を見ることによって Django が動作する冗長な名前の純粋な決定に頼るよりも、明示的な [`verbose_name`](/ja/4.0/ref/models/options/#django.db.models.Options.verbose_name) と [`verbose_name_plural`](/ja/4.0/ref/models/options/#django.db.models.Options.verbose_name_plural) オプションを指定することが推奨されます:

```
from django.db import models
from django.utils.translation import gettext_lazy as _

class MyThing(models.Model):
    name = models.CharField(_('name'), help_text=_('This is the help text'))

    class Meta:
        verbose_name = _('my thing')
        verbose_name_plural = _('my things')
```

#### Model methods `description` argument to the `@display` decorator

For model methods, you can provide translations to Django and the admin site
with the `description` argument to the [`display()`](/ja/4.0/ref/contrib/admin/#django.contrib.admin.display)
decorator:

```
from django.contrib import admin
from django.db import models
from django.utils.translation import gettext_lazy as _

class MyThing(models.Model):
    kind = models.ForeignKey(
        ThingKind,
        on_delete=models.CASCADE,
        related_name='kinds',
        verbose_name=_('kind'),
    )

    @admin.display(description=_('Is it a mouse?'))
    def is_mouse(self):
        return self.kind.type == MOUSE_TYPE
```

### Lazy 翻訳オブジェクトともに働く

The result of a `gettext_lazy()` call can be used wherever you would use a
string (a [`str`](https://docs.python.org/3/library/stdtypes.html#str) object) in other Django code, but it may not work with
arbitrary Python code. For example, the following won't work because the
[requests](https://pypi.org/project/requests/) library doesn't handle
`gettext_lazy` objects:

```
body = gettext_lazy("I \u2764 Django")  # (Unicode :heart:)
requests.post('https://example.com/send', data={'body': body})
```

You can avoid such problems by casting `gettext_lazy()` objects to text
strings before passing them to non-Django code:

```
requests.post('https://example.com/send', data={'body': str(body)})
```

If you don't like the long `gettext_lazy` name, you can alias it as `_`
(underscore), like so:

```
from django.db import models
from django.utils.translation import gettext_lazy as _

class MyThing(models.Model):
    name = models.CharField(help_text=_('This is the help text'))
```

Using `gettext_lazy()` and `ngettext_lazy()` to mark strings in models
and utility functions is a common operation. When you're working with these
objects elsewhere in your code, you should ensure that you don't accidentally
convert them to strings, because they should be converted as late as possible
(so that the correct locale is in effect). This necessitates the use of the
helper function described next.

#### Lazy translations and plural

When using lazy translation for a plural string (`n[p]gettext_lazy`), you
generally don't know the `number` argument at the time of the string
definition. Therefore, you are authorized to pass a key name instead of an
integer as the `number` argument. Then `number` will be looked up in the
dictionary under that key during string interpolation. Here's example:

```
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ngettext_lazy

class MyForm(forms.Form):
    error_message = ngettext_lazy("You only provided %(num)d argument",
        "You only provided %(num)d arguments", 'num')

    def clean(self):
        # ...
        if error:
            raise ValidationError(self.error_message % {'num': number})
```

If the string contains exactly one unnamed placeholder, you can interpolate
directly with the `number` argument:

```
class MyForm(forms.Form):
    error_message = ngettext_lazy(
        "You provided %d argument",
        "You provided %d arguments",
    )

    def clean(self):
        # ...
        if error:
            raise ValidationError(self.error_message % number)
```

#### Formatting strings: `format_lazy()`

Python's [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format) method will not work when either the
`format_string` or any of the arguments to [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format)
contains lazy translation objects. Instead, you can use
[`django.utils.text.format_lazy()`](/ja/4.0/ref/utils/#django.utils.text.format_lazy), which creates a lazy object
that runs the `str.format()` method only when the result is included
in a string. For example:

```
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy
...
name = gettext_lazy('John Lennon')
instrument = gettext_lazy('guitar')
result = format_lazy('{name}: {instrument}', name=name, instrument=instrument)
```

In this case, the lazy translations in `result` will only be converted to
strings when `result` itself is used in a string (usually at template
rendering time).

#### Other uses of lazy in delayed translations

For any other case where you would like to delay the translation, but have to
pass the translatable string as argument to another function, you can wrap
this function inside a lazy call yourself. For example:

```
from django.utils.functional import lazy
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _

mark_safe_lazy = lazy(mark_safe, str)
```

And then later:

```
lazy_string = mark_safe_lazy(_("<p>My <strong>string!</strong></p>"))
```

### 言語のローカル化された名前

#### `get_language_info(lang_code)`

`get_language_info()` 関数は、言語について詳細な情報を提供します:

```
>>> from django.utils.translation import activate, get_language_info
>>> activate('fr')
>>> li = get_language_info('de')
>>> print(li['name'], li['name_local'], li['name_translated'], li['bidi'])
German Deutsch Allemand False
```

ディクショナリの `name`、`name_local`、`name_translated` 属性は、言語の英語名、言語自体、カレントのアクティブ言語をそれぞれ含んでいます。`bidi` 属性は双方向性の言語に対してのみ True です。

言語情報のソースは `django.conf.locale` モジュールです。テンプレートコードでも、この情報に対して同じようにアクセスすることができます。以下を参照してください。

## ローカル化: テンプレート内

[Django テンプレート](/ja/4.0/ref/templates/language/) 内での翻訳は 2 つのテンプレートタグと Pthon コードとはやや異なるシンタックスを使います。テンプレートでこれらのタグを使えるようにするには、`{% load i18n %}` をテンプレートの先頭に対して設置してください。全てのテンプレートタグと同じように、すでに `i18n` をロードしているテンプレートを extend している場合であっても、翻訳を使用する全てのテンプレートでロードする必要があります。

> **Warning**
>
> Translated strings will not be escaped when rendered in a template.
> This allows you to include HTML in translations, for example for emphasis,
> but potentially dangerous characters (e.g. `"`) will also be rendered
> unchanged.

### `translate` template tag

The `{% translate %}` template tag translates either a constant string
(enclosed in single or double quotes) or variable content:

```html+django
<title>{% translate "This is the title." %}</title>
<title>{% translate myvar %}</title>
```

`noop` オプションが存在する場合、変数はそのままですが翻訳の対象外となります。将来的に翻訳を必要とするコンテンツを "もみ消している" ときに役立ちます:

```html+django
<title>{% translate "myvar" noop %}</title>
```

Internally, inline translations use an
[`gettext()`](/ja/4.0/ref/utils/#django.utils.translation.gettext) call.

テンプレート var (上記の `myvar`) がタグに渡された場合、最初にランタイムで変数から文字列へと解決し、それからメッセージカタログ内でその文字列をルックアップします。

It's not possible to mix a template variable inside a string within
`{% translate %}`. If your translations require strings with variables
(placeholders), use [`{% blocktranslate %}`](#std-templatetag-blocktranslate) instead.

翻訳された文字列を表示させずに取り出したい場合は、以下のシンタックスを使うことができます:

```html+django
{% translate "This is the title" as the_title %}

<title>{{ the_title }}</title>
<meta name="description" content="{{ the_title }}">
```

実際には、テンプレート内の複数の箇所で使用できる文字列を取得するためか、他のテンプレートタグやフィルタに対する引数としてアウトプットを使うために、この方法を使うことになるでしょう:

```html+django
{% translate "starting point" as start %}
{% translate "end point" as end %}
{% translate "La Grande Boucle" as race %}

<h1>
  <a href="/" title="{% blocktranslate %}Back to '{{ race }}' homepage{% endblocktranslate %}">{{ race }}</a>
</h1>
<p>
{% for stage in tour_stages %}
    {% cycle start end %}: {{ stage }}{% if forloop.counter|divisibleby:2 %}<br>{% else %}, {% endif %}
{% endfor %}
</p>
```

`{% translate %}` also supports [contextual markers](#contextual-markers)
using the `context` keyword:

```html+django
{% translate "May" context "month name" %}
```

### `blocktranslate` template tag

Contrarily to the [`translate`](#std-templatetag-translate) tag, the `blocktranslate` tag allows you
to mark complex sentences consisting of literals and variable content for
translation by making use of placeholders:

```html+django
{% blocktranslate %}This string will have {{ value }} inside.{% endblocktranslate %}
```

テンプレート表現を翻訳するには -- 例えば、オブジェクトの属性にアクセスしたりテンプレートフィルタを使う -- 翻訳ブロック内で使うローカル変数に対する表現をくくる必要があります。例えば:

```html+django
{% blocktranslate with amount=article.price %}
That will cost $ {{ amount }}.
{% endblocktranslate %}

{% blocktranslate with myvar=value|filter %}
This will have {{ myvar }} inside.
{% endblocktranslate %}
```

You can use multiple expressions inside a single `blocktranslate` tag:

```html+django
{% blocktranslate with book_t=book|title author_t=author|title %}
This is {{ book_t }} by {{ author_t }}
{% endblocktranslate %}
```

> **Note**
>
> The previous more verbose format is still supported:
> `{% blocktranslate with book|title as book_t and author|title as author_t %}`

Other block tags (for example `{% for %}` or `{% if %}`) are not allowed
inside a `blocktranslate` tag.

If resolving one of the block arguments fails, `blocktranslate` will fall
back to the default language by deactivating the currently active language
temporarily with the [`deactivate_all()`](/ja/4.0/ref/utils/#django.utils.translation.deactivate_all)
function.

This tag also provides for pluralization. To use it:

- Designate and bind a counter value with the name `count`. This value will
  be the one used to select the right plural form.
- Specify both the singular and plural forms separating them with the
  `{% plural %}` tag within the `{% blocktranslate %}` and
  `{% endblocktranslate %}` tags.

実装例:

```html+django
{% blocktranslate count counter=list|length %}
There is only one {{ name }} object.
{% plural %}
There are {{ counter }} {{ name }} objects.
{% endblocktranslate %}
```

A more complex example:

```html+django
{% blocktranslate with amount=article.price count years=i.length %}
That will cost $ {{ amount }} per year.
{% plural %}
That will cost $ {{ amount }} per {{ years }} years.
{% endblocktranslate %}
```

When you use both the pluralization feature and bind values to local variables
in addition to the counter value, keep in mind that the `blocktranslate`
construct is internally converted to an `ngettext` call. This means the
same [notes regarding ngettext variables](#pluralization-var-notes)
apply.

Reverse URL lookups cannot be carried out within the `blocktranslate` and
should be retrieved (and stored) beforehand:

```html+django
{% url 'path.to.view' arg arg2 as the_url %}
{% blocktranslate %}
This is a URL: {{ the_url }}
{% endblocktranslate %}
```

翻訳された文字列を表示させずに取り出したい場合は、以下のシンタックスを使うことができます:

```html+django
{% blocktranslate asvar the_title %}The title is {{ title }}.{% endblocktranslate %}
<title>{{ the_title }}</title>
<meta name="description" content="{{ the_title }}">
```

In practice you'll use this to get a string you can use in multiple places in a
template or so you can use the output as an argument for other template tags or
filters.

`{% blocktranslate %}` also supports [contextual
markers](#contextual-markers) using the `context` keyword:

```html+django
{% blocktranslate with name=user.username context "greeting" %}Hi {{ name }}{% endblocktranslate %}
```

Another feature `{% blocktranslate %}` supports is the `trimmed` option.
This option will remove newline characters from the beginning and the end of
the content of the `{% blocktranslate %}` tag, replace any whitespace at the
beginning and end of a line and merge all lines into one using a space
character to separate them. This is quite useful for indenting the content of a
`{% blocktranslate %}` tag without having the indentation characters end up
in the corresponding entry in the PO file, which makes the translation process
easier.

For instance, the following `{% blocktranslate %}` tag:

```html+django
{% blocktranslate trimmed %}
  First sentence.
  Second paragraph.
{% endblocktranslate %}
```

will result in the entry `"First sentence. Second paragraph."` in the PO file,
compared to `"\n  First sentence.\n  Second paragraph.\n"`, if the
`trimmed` option had not been specified.

### String literals passed to tags and filters

You can translate string literals passed as arguments to tags and filters
by using the familiar `_()` syntax:

```html+django
{% some_tag _("Page not found") value|yesno:_("yes,no") %}
```

In this case, both the tag and the filter will see the translated string,
so they don't need to be aware of translations.

> **Note**
>
> In this example, the translation infrastructure will be passed the string
> `"yes,no"`, not the individual strings `"yes"` and `"no"`. The
> translated string will need to contain the comma so that the filter
> parsing code knows how to split up the arguments. For example, a German
> translator might translate the string `"yes,no"` as `"ja,nein"`
> (keeping the comma intact).

### Comments for translators in templates

Just like with [Python code](#translator-comments), these notes for
translators can be specified using comments, either with the [`comment`](/ja/4.0/ref/templates/builtins/#std-templatetag-comment)
tag:

```html+django
{% comment %}Translators: View verb{% endcomment %}
{% translate "View" %}

{% comment %}Translators: Short intro blurb{% endcomment %}
<p>{% blocktranslate %}A multiline translatable
literal.{% endblocktranslate %}</p>
```

or with the `{#` ... `#}` [one-line comment constructs](/ja/4.0/ref/templates/language/#template-comments):

```html+django
{# Translators: Label of a button that triggers search #}
<button type="submit">{% translate "Go" %}</button>

{# Translators: This is a text of the base template #}
{% blocktranslate %}Ambiguous translatable block of text{% endblocktranslate %}
```

> **Note**
>
> Just for completeness, these are the corresponding fragments of the
> resulting `.po` file:
>
> ```po
> #. Translators: View verb
> # path/to/template/file.html:10
> msgid "View"
> msgstr ""
>
> #. Translators: Short intro blurb
> # path/to/template/file.html:13
> msgid ""
> "A multiline translatable"
> "literal."
> msgstr ""
>
> # ...
>
> #. Translators: Label of a button that triggers search
> # path/to/template/file.html:100
> msgid "Go"
> msgstr ""
>
> #. Translators: This is a text of the base template
> # path/to/template/file.html:103
> msgid "Ambiguous translatable block of text"
> msgstr ""
> ```

### Switching language in templates

If you want to select a language within a template, you can use the
`language` template tag:

```html+django
{% load i18n %}

{% get_current_language as LANGUAGE_CODE %}
<!-- Current language: {{ LANGUAGE_CODE }} -->
<p>{% translate "Welcome to our page" %}</p>

{% language 'en' %}
    {% get_current_language as LANGUAGE_CODE %}
    <!-- Current language: {{ LANGUAGE_CODE }} -->
    <p>{% translate "Welcome to our page" %}</p>
{% endlanguage %}
```

While the first occurrence of "Welcome to our page" uses the current language,
the second will always be in English.

### Other tags

These tags also require a `{% load i18n %}`.

#### `get_available_languages`

`{% get_available_languages as LANGUAGES %}` returns a list of tuples in
which the first element is the [language code](/ja/4.0/topics/i18n/#term-language-code) and the second is the
language name (translated into the currently active locale).

#### `get_current_language`

`{% get_current_language as LANGUAGE_CODE %}` returns the current user's
preferred language as a string. Example: `en-us`. See
[How Django discovers language preference](#how-django-discovers-language-preference).

#### `get_current_language_bidi`

`{% get_current_language_bidi as LANGUAGE_BIDI %}` returns the current
locale's direction. If `True`, it's a right-to-left language, e.g. Hebrew,
Arabic. If `False` it's a left-to-right language, e.g. English, French,
German, etc.

#### `i18n` context processor

If you enable the [`django.template.context_processors.i18n`](/ja/4.0/ref/templates/api/#django.template.context_processors.i18n) context
processor, then each `RequestContext` will have access to `LANGUAGES`,
`LANGUAGE_CODE`, and `LANGUAGE_BIDI` as defined above.

#### `get_language_info`

You can also retrieve information about any of the available languages using
provided template tags and filters. To get information about a single language,
use the `{% get_language_info %}` tag:

```html+django
{% get_language_info for LANGUAGE_CODE as lang %}
{% get_language_info for "pl" as lang %}
```

You can then access the information:

```html+django
Language code: {{ lang.code }}<br>
Name of language: {{ lang.name_local }}<br>
Name in English: {{ lang.name }}<br>
Bi-directional: {{ lang.bidi }}
Name in the active language: {{ lang.name_translated }}
```

#### `get_language_info_list`

You can also use the `{% get_language_info_list %}` template tag to retrieve
information for a list of languages (e.g. active languages as specified in
[`LANGUAGES`](/ja/4.0/ref/settings/#std-setting-LANGUAGES)). See [the section about the set\_language redirect
view](#set-language-redirect-view) for an example of how to display a language
selector using `{% get_language_info_list %}`.

In addition to [`LANGUAGES`](/ja/4.0/ref/settings/#std-setting-LANGUAGES) style list of tuples,
`{% get_language_info_list %}` supports lists of language codes.
If you do this in your view:

```python
context = {'available_languages': ['en', 'es', 'fr']}
return render(request, 'mytemplate.html', context)
```

you can iterate over those languages in the template:

```html+django
{% get_language_info_list for available_languages as langs %}
{% for lang in langs %} ... {% endfor %}
```

#### テンプレートフィルタ

There are also some filters available for convenience:

- `{{ LANGUAGE_CODE|language_name }}` ("German")
- `{{ LANGUAGE_CODE|language_name_local }}` ("Deutsch")
- `{{ LANGUAGE_CODE|language_bidi }}` (False)
- `{{ LANGUAGE_CODE|language_name_translated }}` ("německy", when active language is Czech)

## 国際化: JavaScript コード内

JavaScript に対する翻訳には、いくつか問題があります:

- JavaScript コードは `gettext` 実行にアクセスできません。
- JavaScript コードは `.po` や `.mo` ファイルにアクセスできません; サーバーによって配布される必要があります。
- JavaScript 用の翻訳カタログはできる限り小さくなければなりません。

Django には、これらの問題に対する総合的な解決策が用意されています: 翻訳を JavaScript に渡し、`gettext` などを JavaScript 内から呼び出せるようにする方法です。

主な解決策は以下の `JavaScriptCatalog` ビューです。これは、`gettext` を擬似的に再現する関数と翻訳文字列の配列で JavaScript コードライブラリを生成します。

### `JavaScriptCatalog` ビュー

#### `class JavaScriptCatalog`

`gettext` インターフェイスを擬似的に再現する関数と翻訳文字列の配列で、JavaScriptのコードライブラリを生成するビューです。

**属性**

#### `domain`

ビューの出力に追加する文字列を含む翻訳ドメインです。デフォルトは `'djangojs'` です。

#### `packages`

インストールされたアプリケーション間の [`application names`](/ja/4.0/ref/applications/#django.apps.AppConfig.name) のリストです。これらのアプリケーションは `locale` ディレクトリを含んでいる必要があります。これらのカタログに加えて  (常に含まれている) [`LOCALE_PATHS`](/ja/4.0/ref/settings/#std-setting-LOCALE_PATHS) 内で見つかった全てのカタログは、1 つのカタログに統合されます。デフォルトは `None` で、全ての [`INSTALLED_APPS`](/ja/4.0/ref/settings/#std-setting-INSTALLED_APPS) からの利用可能な翻訳は JavaScript の出力に渡されることを意味します。

**デフォルト値を使った例**:

```python
from django.views.i18n import JavaScriptCatalog

urlpatterns = [
    path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
]
```

**独自のパッケージを使った例**:

```python
urlpatterns = [
    path('jsi18n/myapp/',
         JavaScriptCatalog.as_view(packages=['your.app.label']),
         name='javascript-catalog'),
]
```

ルートの URLconf が [`i18n_patterns()`](#django.conf.urls.i18n.i18n_patterns)、`JavaScriptCatalog` を使っている場合、カタログが正しく生成されるように `i18n_patterns()` によるラップも必要となります。

`i18n_patterns()` **を使った例**:

```python
from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns(
    path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
)
```

翻訳の優先事項は、`packages` 引数内で現れるパッケージに最初に現れるものより高い優先順位を付けることです。これは、同じ利他ラルに対して翻訳がクラッシュする場合に備えて重要です。

サイト上で複数の `JavaScriptCatalog` ビューを使っていてそのうちのいくつかが同じ文字列を定義している場合、最後にロードされたカタログ内の文字列が優先されます。

### JavaScript 翻訳カタログを使う

To use the catalog, pull in the dynamically generated script like this:

```html+django
<script src="{% url 'javascript-catalog' %}"></script>
```

これは、reverse URL を使用して、JavaScript カタログビューの URL をルックアップします。カタログがロードされると、JavaScript コードは以下のメソッドを使えるようになります:

- `gettext`
- `ngettext`
- `interpolate`
- `get_format`
- `gettext_noop`
- `pgettext`
- `npgettext`
- `pluralidx`

#### `gettext`

`gettext` 関数は、Python コード内の標準の `gettext` インターフェースと同じように動作します:

```javascript
document.write(gettext('this is to be translated'));
```

#### `ngettext`

`ngettext` 関数は単語やフレーズを複数形にするインターフェースを提供します:

```javascript
const objectCount = 1 // or 0, or 2, or 3, ...
const string = ngettext(
    'literal for the singular case',
    'literal for the plural case',
    objectCount
);
```

#### `interpolate`

`interpolate` 関数は表示形式の文字列の動的な記入をサポートします。補完シンタックスは  Python を模しているので、`interpolate` 関数は位置および名前付きの補完の両方をサポートします。

- 位置による補完: `obj` は、表示されるのと同じ順番で要素の値が一致する `fmt` プレースホルダ内で連続的に補完される JavaScript Array オブジェクトを含んでいます。例えば:

  ```javascript
  const formats = ngettext(
    'There is %s object. Remaining: %s',
    'There are %s objects. Remaining: %s',
    11
  );
  const string = interpolate(formats, [11, 20]);
  // string is 'There are 11 objects. Remaining: 20'
  ```
- 名前付きの補完: このモードは `named` パラメータを `true` として渡すことで選択されます。`obj` は JavaScript オブジェクトや結合配列を含みます。例えば:

  ```javascript
  const data = {
    count: 10,
    total: 50
  };

  const formats = ngettext(
      'Total: %(total)s, there is %(count)s object',
      'there are %(count)s of a total of %(total)s objects',
      data.count
  );
  const string = interpolate(formats, data, true);
  ```

文字列補完でトップに行ってはいけませんが: とは言えこれは JavaScript なので、コードは繰り返しの正規表現の代替を作る必要があります。これは、Python 内での補完ほど高速ではないので、本当に必要な場合のみ使用するようにしてください (例えば、適切な複数形を生成するための `ngettext` との結合内)。

#### `get_format`

`get_format` 関数は設定済みの i18n 表示形式設定にアクセスでき、与えられた設定名に対して表示形式文字列を取り出します:

```javascript
document.write(get_format('DATE_FORMAT'));
// 'N j, Y'
```

以下の設定に対してアクセスします:

- [`DATE_FORMAT`](/ja/4.0/ref/settings/#std-setting-DATE_FORMAT)
- [`DATE_INPUT_FORMATS`](/ja/4.0/ref/settings/#std-setting-DATE_INPUT_FORMATS)
- [`DATETIME_FORMAT`](/ja/4.0/ref/settings/#std-setting-DATETIME_FORMAT)
- [`DATETIME_INPUT_FORMATS`](/ja/4.0/ref/settings/#std-setting-DATETIME_INPUT_FORMATS)
- [`DECIMAL_SEPARATOR`](/ja/4.0/ref/settings/#std-setting-DECIMAL_SEPARATOR)
- [`FIRST_DAY_OF_WEEK`](/ja/4.0/ref/settings/#std-setting-FIRST_DAY_OF_WEEK)
- [`MONTH_DAY_FORMAT`](/ja/4.0/ref/settings/#std-setting-MONTH_DAY_FORMAT)
- [`NUMBER_GROUPING`](/ja/4.0/ref/settings/#std-setting-NUMBER_GROUPING)
- [`SHORT_DATE_FORMAT`](/ja/4.0/ref/settings/#std-setting-SHORT_DATE_FORMAT)
- [`SHORT_DATETIME_FORMAT`](/ja/4.0/ref/settings/#std-setting-SHORT_DATETIME_FORMAT)
- [`THOUSAND_SEPARATOR`](/ja/4.0/ref/settings/#std-setting-THOUSAND_SEPARATOR)
- [`TIME_FORMAT`](/ja/4.0/ref/settings/#std-setting-TIME_FORMAT)
- [`TIME_INPUT_FORMATS`](/ja/4.0/ref/settings/#std-setting-TIME_INPUT_FORMATS)
- [`YEAR_MONTH_FORMAT`](/ja/4.0/ref/settings/#std-setting-YEAR_MONTH_FORMAT)

これは、Python によってレンダリングされる値と一貫性を持った表示形式を保守するために役立ちます。

#### `gettext_noop`

これは `gettext` 関数をエミュレートしますが、何もせず、渡されたものをそのまま返します:

```javascript
document.write(gettext_noop('this will not be translated'));
```

これは、将来的に翻訳が必要なコードの部分をもみ消しておくのに役立ちます。

#### `pgettext`

`pgettext` 関数は Python の変数 ([`pgettext()`](/ja/4.0/ref/utils/#django.utils.translation.pgettext)) のように動作し、文脈的に翻訳される単語を提供します:

```javascript
document.write(pgettext('month name', 'May'));
```

#### `npgettext`

`npgettext` 関数も Python 変数 ([`npgettext()`](/ja/4.0/ref/utils/#django.utils.translation.npgettext)) のように動作し、**複数形の** 文脈的に翻訳される単語を提供します:

```javascript
document.write(npgettext('group', 'party', 1));
// party
document.write(npgettext('group', 'party', 2));
// parties
```

#### `pluralidx`

`pluralidx` 関数は [`pluralize`](/ja/4.0/ref/templates/builtins/#std-templatefilter-pluralize) テンプレートフィルタと同じように動作し、与えられた `count` が単語の複数形を使うべきかどうかを決めます:

```javascript
document.write(pluralidx(0));
// true
document.write(pluralidx(1));
// false
document.write(pluralidx(2));
// true
```

最も単純なケースでは、独自の複数形が必要ない場合、これは整数 `1` に対して `false` を返し、他の数に対して `true` を返します。

ただし、複数形は全ての言語でこのようにシンプルではありません。言語が複数形をサポートしていない場合、空の値が渡されます。

加えて、複数形に関して複雑なルールが存在する場合、カタログビューは条件付きの表現をレンダリングします。これは、値を `true` (複数形) と `false` (複数形 **ではない** ) のどちらにするかを評価します。

### `JSONCatalog` ビュー

#### `class JSONCatalog`

他のクライアントサイドのライブラリを使って翻訳を扱うためには、`JSONCatalog` を利用するのがいいでしょう。[`JavaScriptCatalog`](#django.views.i18n.JavaScriptCatalog) と似ていますが、JSON のレスポンスを返します。

使える値を知るには、[`JavaScriptCatalog`](#django.views.i18n.JavaScriptCatalog) を参照してください。また、`domain` と `packages` 属性を使用してください。

レスポンスの形式は以下の通りです:

```text
{
    "catalog": {
        # Translations catalog
    },
    "formats": {
        # Language formats for date, time, etc.
    },
    "plural": "..."  # Expression for plural forms, or null.
}
```

### 実行速度についての覚え書き

たくさんの JavaScript/JSON i18n ビューは全てのリクエストで `.mo` ファイルからカタログを生成します。出力は一定なので、少なくとも与えられたサイトのバージョンに対しては、キャッシングが有力な選択肢となります。

サーバーサイトのキャッシングは CPU のロードを軽減します。[`cache_page()`](/ja/4.0/topics/cache/#django.views.decorators.cache.cache_page) デコレータを使うと簡単に実装できます。翻訳を変更したときにキャッシュの無効部分をトリガーするには、以下の例にあるようにバージョンに依存したキープレフィックスを使ってください。 もしくは、バージョンに依存した URL でビューをマッピングしてください:

```python
from django.views.decorators.cache import cache_page
from django.views.i18n import JavaScriptCatalog

# The value returned by get_version() must change when translations change.
urlpatterns = [
    path('jsi18n/',
         cache_page(86400, key_prefix='jsi18n-%s' % get_version())(JavaScriptCatalog.as_view()),
         name='javascript-catalog'),
]
```

Client-side caching will save bandwidth and make your site load faster. If
you're using ETags ([`ConditionalGetMiddleware`](/ja/4.0/ref/middleware/#django.middleware.http.ConditionalGetMiddleware)),
you're already covered. Otherwise, you can apply [conditional decorators](/ja/4.0/topics/conditional-view-processing/#conditional-decorators). In the following example, the cache is invalidated
whenever you restart your application server:

```python
from django.utils import timezone
from django.views.decorators.http import last_modified
from django.views.i18n import JavaScriptCatalog

last_modified_date = timezone.now()

urlpatterns = [
    path('jsi18n/',
         last_modified(lambda req, **kw: last_modified_date)(JavaScriptCatalog.as_view()),
         name='javascript-catalog'),
]
```

また、開発プロシージャの一部として JavaScript カタログを事前に生成して、静的ファイルとして提供することもできます。この抜本的テクニックは [django-statici18n](https://django-statici18n.readthedocs.io/) で実装されます。

## 国際化: URL パターン内

Django には、URL パターンを国際化するための 2 つの仕組みが用意されています:

- [`LocaleMiddleware`](/ja/4.0/ref/middleware/#django.middleware.locale.LocaleMiddleware) が、リクエストされた URL からアクティブにする言語を検出できるようにするため、URL パターンのルートに言語のプレフィックスを追加します。
- Making URL patterns themselves translatable via the
  [`django.utils.translation.gettext_lazy()`](/ja/4.0/ref/utils/#django.utils.translation.gettext_lazy) function.

> **Warning**
>
> この 2 つのうちどちらを使う場合でも、各リクエストに対してアクティブな言語をセットすることが必要となります; 言い換えれば、[`MIDDLEWARE`](/ja/4.0/ref/settings/#std-setting-MIDDLEWARE) 設定内に [`django.middleware.locale.LocaleMiddleware`](/ja/4.0/ref/middleware/#django.middleware.locale.LocaleMiddleware) を記述しておく必要があります。

### URL パターンにおける言語プレフィックス

#### `i18n_patterns(*urls, prefix_default_language=True)`

この関数は、ルートの URLconf で使うことができ、Django は自動的に現在アクティブな言語のコードを [`i18n_patterns()`](#django.conf.urls.i18n.i18n_patterns) で定義された全ての URL パターンの先頭に追加します。

`prefix_default_language` を `False` にセットすると、デフォルト言語 ([`LANGUAGE_CODE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_CODE)) からプレフィックスを除去します。これは、現在の URL を変更しないよう既存のサイトに翻訳を追加するとき、役に立ちます。

URL パターンの例:

```python
from django.conf.urls.i18n import i18n_patterns
from django.urls import include, path

from about import views as about_views
from news import views as news_views
from sitemap.views import sitemap

urlpatterns = [
    path('sitemap.xml', sitemap, name='sitemap-xml'),
]

news_patterns = ([
    path('', news_views.index, name='index'),
    path('category/<slug:slug>/', news_views.category, name='category'),
    path('<slug:slug>/', news_views.details, name='detail'),
], 'news')

urlpatterns += i18n_patterns(
    path('about/', about_views.main, name='about'),
    path('news/', include(news_patterns, namespace='news')),
)
```

After defining these URL patterns, Django will automatically add the
language prefix to the URL patterns that were added by the `i18n_patterns`
function. Example:

```python
>>> from django.urls import reverse
>>> from django.utils.translation import activate

>>> activate('en')
>>> reverse('sitemap-xml')
'/sitemap.xml'
>>> reverse('news:index')
'/en/news/'

>>> activate('nl')
>>> reverse('news:detail', kwargs={'slug': 'news-slug'})
'/nl/news/news-slug/'
```

`prefix_default_language=False` と `LANGUAGE_CODE='en'` を使うと、URL は以下のようになります:

```python
>>> activate('en')
>>> reverse('news:index')
'/news/'

>>> activate('nl')
>>> reverse('news:index')
'/nl/news/'
```

> **Warning**
>
> [`i18n_patterns()`](#django.conf.urls.i18n.i18n_patterns) はルートの URLconf のみで使うことができます。インクルードされた URLconf で使うと [`ImproperlyConfigured`](/ja/4.0/ref/exceptions/#django.core.exceptions.ImproperlyConfigured) 例外を投げます。

> **Warning**
>
> 自動的に追加される言語プレフィックスと重複するようなプレフィックスを持った URL パターンにしないように注意してください。

### URL パターンを翻訳する

URL patterns can also be marked translatable using the
[`gettext_lazy()`](/ja/4.0/ref/utils/#django.utils.translation.gettext_lazy) function. Example:

```python
from django.conf.urls.i18n import i18n_patterns
from django.urls import include, path
from django.utils.translation import gettext_lazy as _

from about import views as about_views
from news import views as news_views
from sitemaps.views import sitemap

urlpatterns = [
    path('sitemap.xml', sitemap, name='sitemap-xml'),
]

news_patterns = ([
    path('', news_views.index, name='index'),
    path(_('category/<slug:slug>/'), news_views.category, name='category'),
    path('<slug:slug>/', news_views.details, name='detail'),
], 'news')

urlpatterns += i18n_patterns(
    path(_('about/'), about_views.main, name='about'),
    path(_('news/'), include(news_patterns, namespace='news')),
)
```

翻訳を作成した後に、[`reverse()`](/ja/4.0/ref/urlresolvers/#django.urls.reverse) 関数はアクティブな言語で URL を返します。例えば:

```python
>>> from django.urls import reverse
>>> from django.utils.translation import activate

>>> activate('en')
>>> reverse('news:category', kwargs={'slug': 'recent'})
'/en/news/category/recent/'

>>> activate('nl')
>>> reverse('news:category', kwargs={'slug': 'recent'})
'/nl/nieuws/categorie/recent/'
```

> **Warning**
>
> In most cases, it's best to use translated URLs only within a language code
> prefixed block of patterns (using
> [`i18n_patterns()`](#django.conf.urls.i18n.i18n_patterns)), to avoid the possibility
> that a carelessly translated URL causes a collision with a non-translated
> URL pattern.

### テンプレート内でリバースする

ローカル化された URL がテンプレート内でリバースされる場合、常に現在の言語が使用されます。他の言語の URL にリンクを貼るには、[`language`](#std-templatetag-language) テンプレートタグを使ってください。これは、テンプレートの囲まれた範囲内で、指定された言語を有効化します:

```html+django
{% load i18n %}

{% get_available_languages as languages %}

{% translate "View this category in:" %}
{% for lang_code, lang_name in languages %}
    {% language lang_code %}
    <a href="{% url 'category' slug=category.slug %}">{{ lang_name }}</a>
    {% endlanguage %}
{% endfor %}
```

[`language`](#std-templatetag-language) タグは引数のみとして言語コードを予期します。

## ローカル化: 言語ファイルを作成する方法

アプリケーションの文字列リテラルが後の翻訳にためにタグづけされ終わったら、翻訳自体を記述する (もしくは取得する) 必要があります。以下は、その方法です。

### メッセージファイル

最初のステップは、新しい言語に対する [message file](/ja/4.0/topics/i18n/#term-message-file) を作成することです。メッセージファイルはプレーンテキストファイルで、単一の言語を記述します。このファイルは、全ての翻訳可能な文字列とそれらがどのように与えられた言語で表示されるかを含みます。メッセージファイルは `.po` ファイル拡張子を持ちます。

Djangoには [`django-admin makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) というツールが用意してあり、これらのファイルの作成と保全を自動化します。

> **Gettext ユーティリティ**
>
> `makemessages` コマンド (と後述する `compilemessages`) は、GNU gettext ツールセットからのコマンドを使用します: `xgettext`、`msgfmt`、`msgmerge`、`msguniq` です。
>
> サポートされている `gettext` ユーティリティの最小バージョンは 0.15 です。

メッセージファイルを作成または更新するには、以下のコマンドを実行してください:

```python
django-admin makemessages -l de
```

...ここでの `de` は作成したいメッセージファイルに対する [locale name](/ja/4.0/topics/i18n/#term-locale-name) です。例えば、ブラジル系のポルトガル語に対する `pt_BR`、オーストリア系のドイツ語に対する `de_AT`、インドネシアに対する `id` などです。

スクリプトは、2 つの場所のうち 1 つから実行する必要があります:

- Django プロジェクトのルートディレクトリ (`manage.py` を含むところ)。
- Django アプリケーションのルートディレクトリ。

スクリプトは、プロジェクトのソースツリーもしくはアプリケーションのソースツリーを通じて実行され、翻訳用にマークされた全ての文字列 ([How Django discovers translations](#how-django-discovers-translations) を参照して [`LOCALE_PATHS`](/ja/4.0/ref/settings/#std-setting-LOCALE_PATHS) を正しく設定してください) を抽出します。ディレクトリ `locale/LANG/LC_MESSAGES` にメッセージファイルを作成 (もしくは更新) します。`de` の例では、ファイルは `locale/de/LC_MESSAGES/django.po` となります。

プロジェクトのルートディレクトリから `makemessages` を実行したとき、抽出された文字列は自動的に適切なメッセージファイルへと分配されます。つまり、`locale` ディレクトリを含むアプリケーションのファイルから抽出された文字列は、そのディレクトリ下のメッセージファイルに格納されます。`locale` ディレクトリがないアプリケーションのファイルから抽出された文字列は、[`LOCALE_PATHS`](/ja/4.0/ref/settings/#std-setting-LOCALE_PATHS) 内に最初にリストアップされたディレクトリ下にあるメッセージファイルか、もしくは [`LOCALE_PATHS`](/ja/4.0/ref/settings/#std-setting-LOCALE_PATHS) が空の場合はエラーを生成します。

By default [`django-admin makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) examines every
file that has the `.html`, `.txt` or `.py` file extension. If you want to
override that default, use the [`--extension`](/ja/4.0/ref/django-admin/#cmdoption-makemessages-extension)
or `-e` option to specify the file extensions to examine:

```python
django-admin makemessages -l de -e txt
```

カンマで複数の拡張子を区切って、そして/あるいは `-e` や `--extension` を複数回使ってください:

```python
django-admin makemessages -l de -e html,txt -e xml
```

> **Warning**
>
> When [creating message files from JavaScript source code](#creating-message-files-from-js-code) you need to use the special
> `djangojs` domain, **not** `-e js`.

> **Jinja2 テンプレートを使う？**
>
> [`makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) doesn't understand the syntax of Jinja2 templates.
> To extract strings from a project containing Jinja2 templates, use [Message
> Extracting](https://babel.pocoo.org/en/latest/messages.html#message-extraction) from [Babel](https://babel.pocoo.org/en/latest/) instead.
>
> Here's an example `babel.cfg` configuration file:
>
> ```python
> # Extraction from Python source files
> [python: **.py]
>
> # Extraction from Jinja2 templates
> [jinja2: **.jinja]
> extensions = jinja2.ext.with_
> ```
>
> Make sure you list all extensions you're using! Otherwise Babel won't
> recognize the tags defined by these extensions and will ignore Jinja2
> templates containing them entirely.
>
> Babel provides similar features to [`makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages), can replace it
> in general, and doesn't depend on `gettext`. For more information, read
> its documentation about [working with message catalogs](https://babel.pocoo.org/en/latest/messages.html).

> **gettext がない？**
>
> If you don't have the `gettext` utilities installed,
> [`makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) will create empty files. If that's the case, either
> install the `gettext` utilities or copy the English message file
> (`locale/en/LC_MESSAGES/django.po`) if available and use it as a starting
> point, which is an empty translation file.

> **Windows で動作する？**
>
> あなたが Windows を使っていて、[`makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) が動作するように GNU gettext ユーティリティをインストールする必要がある場合は、[Windows での gettext](#gettext-on-windows) で詳しい情報を参照してください。

Each `.po` file contains a small bit of metadata, such as the translation
maintainer's contact information, but the bulk of the file is a list of
**messages** -- mappings between translation strings and the actual translated
text for the particular language.

例えば以下のように、Django アプリケーションが `"Welcome to my site."` に対する翻訳文字列を含んでいる場合、:

```python
_("Welcome to my site.")
```

...それから [`django-admin makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) は `.po` ファイルを生成し、そのファイルは以下のようなスニペットを含みます -- メッセージ:

```po
#: path/to/python/module.py:23
msgid "Welcome to my site."
msgstr ""
```

簡単に説明すると:

- `msgid` は翻訳文字列で、ソース内に存在します。変更しないでください。
- `msgstr` はあなたが言語に対応する翻訳を記述する場所です。これは空の状態から始まるので、変更するのはあなたの役割です。翻訳をクオートで囲むようにしてください。
- 利便性のため、各メッセージは、`#` でプレフィックスされ `msgid` 行の直前に位置するコメント行の形式の中に、翻訳文字列がどこから拾い集められたかを示すファイル名と行数を含みます。

長いメッセージは特殊なケースです。そこでは、`msgstr` (もしくは `msgid`) 直後の最初の文字列は空の文字列です。それからコンテンツ自身が行ごとに 1 つの文字列として次の数行にわたって記述されます。これらの文字列は直接連結されます。文字列内で最後のスペースを忘れないでください; さもなくば、空白スペースなしで接合されることになります！

> **文字コードを気にしてください**
>
> `gettext` ツールが内部的に動作する方法および非 ASCII ソースの文字列を Django のコアとあなたのアプリケーション内で許容するために、PO ファイルに対するエンコーディングとして UTF-8 (PO ファイルが生成される際のデフォルトです) を **使わなければなりません**。これは、全員が同じエンコーディングを使用することを意味し、Django が PO ファイルを処理する際に重要となります。

> **Fuzzy entries**
>
> [`makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) sometimes generates translation entries marked as
> fuzzy, e.g. when translations are inferred from previously translated
> strings. By default, fuzzy entries are **not** processed by
> [`compilemessages`](/ja/4.0/ref/django-admin/#django-admin-compilemessages).

新しい翻訳文字列に対して全てのコードとテンプレートを再検査して **全ての** 言語に対する全てのメッセージファイルを更新するには、以下を実行してください:

```python
django-admin makemessages -a
```

### メッセージファイルをコンパイルする

メッセージファイルを作成した後 -- そして変更を加えたときそれぞれで -- `gettext` による使用のために、より効率的な形式にコンパイルする必要があります。[`django-admin compilemessages`](/ja/4.0/ref/django-admin/#django-admin-compilemessages) ユーティリティを利用してください。

このツールは、全ての有効な `.po` ファイルを通じて動作し、`.mo` ファイルを生成します。これは `gettext` による使用のために最適化されたバイナリファイルです。[`django-admin makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) を実行した同じディレクトリで、以下のように [`django-admin compilemessages`](/ja/4.0/ref/django-admin/#django-admin-compilemessages) を実行してください:

```python
django-admin compilemessages
```

これで終わりです。翻訳は使う準備ができました。

> **Windows で動作する？**
>
> あなたが Windows を使っていて、[`django-admin compilemessages`](/ja/4.0/ref/django-admin/#django-admin-compilemessages) が動作するように GNU gettext ユーティリティをインストールする必要がある場合は、[Windows での gettext](#gettext-on-windows) で詳しい情報を参照してください。

> **.po ファイル: エンコーディングと BOM の使用**
>
> Django は、UTF-8 でエンコードされ BOM (Byte Order Mark) を持たない `.po` ファイルのみをサポートします。そのため、テキストエディタがデフォルトでこれらのマークをファイルの最初に追加した場合、再構成する必要があります。

### Troubleshooting: `gettext()` incorrectly detects `python-format` in strings with percent signs

In some cases, such as strings with a percent sign followed by a space and a
[string conversion type](https://docs.python.org/3/library/stdtypes.html#old-string-formatting) (e.g.
`_("10% interest")`), [`gettext()`](/ja/4.0/ref/utils/#django.utils.translation.gettext) incorrectly
flags strings with `python-format`.

誤ってフラグが立てられた文字列でメッセージファイルをコンパイルしようとすると、`number of format specifications in 'msgid' and 'msgstr' does not match` もしくは `'msgstr' is not a valid Python format string, unlike 'msgid'`. といったエラーが発生します。

これを解決するには、パーセント文字を 2 つ重ねることでエスケープすることができます:

```python
from django.utils.translation import gettext as _
output = _("10%% interest")
```

もしくは `no-python-format` を使ってパーセント文字がリテラルとして扱われるようにします:

```python
# xgettext:no-python-format
output = _("10% interest")
```

### JavaScript ソースコードからメッセージファイルを作成する

他の Django メッセージファイルと同じ方法で -- [`django-admin makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) ツールを使って -- 作成や更新を行います。唯一の違いは、`-d djangojs` パラメータを提供することによって、gettext 用語がドメインであること (このケースでは `djangojs` ドメイン) を、以下のように明示的に指定する必要があることです:

```python
django-admin makemessages -d djangojs -l de
```

This would create or update the message file for JavaScript for German. After
updating message files, run [`django-admin compilemessages`](/ja/4.0/ref/django-admin/#django-admin-compilemessages) the same way as you do with normal Django message files.

### Windows での `gettext`

This is only needed for people who either want to extract message IDs or compile
message files (`.po`). Translation work itself involves editing existing
files of this type, but if you want to create your own message files, or want
to test or compile a changed message file, download [a precompiled binary
installer](https://mlocati.github.io/articles/gettext-iconv-windows.html).

You may also use `gettext` binaries you have obtained elsewhere, so long as
the `xgettext --version` command works properly. Do not attempt to use Django
translation utilities with a `gettext` package if the command `xgettext
--version` entered at a Windows command prompt causes a popup window saying
"xgettext.exe has generated errors and will be closed by Windows".

### `makemessages` コマンドをカスタマイズする

`xgettext` に追加のパラメータを渡したい場合、独自の [`makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) コマンドを作成して、その `xgettext_options` 属性をオーバーライドする必要があります:

```python
from django.core.management.commands import makemessages

class Command(makemessages.Command):
    xgettext_options = makemessages.Command.xgettext_options + ['--keyword=mytrans']
```

もっと柔軟に使う必要があるとき場合には、[`makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages) コマンドに新しい引数を追加することもできます:

```python
from django.core.management.commands import makemessages

class Command(makemessages.Command):

    def add_arguments(self, parser):
        super().add_arguments(parser)
        parser.add_argument(
            '--extra-keyword',
            dest='xgettext_keywords',
            action='append',
        )

    def handle(self, *args, **options):
        xgettext_keywords = options.pop('xgettext_keywords')
        if xgettext_keywords:
            self.xgettext_options = (
                makemessages.Command.xgettext_options[:] +
                ['--keyword=%s' % kwd for kwd in xgettext_keywords]
            )
        super().handle(*args, **options)
```

## その他

### The `set_language` redirect view

#### `set_language(request)`

As a convenience, Django comes with a view, [`django.views.i18n.set_language()`](#django.views.i18n.set_language),
that sets a user's language preference and redirects to a given URL or, by default,
back to the previous page.

Activate this view by adding the following line to your URLconf:

```python
path('i18n/', include('django.conf.urls.i18n')),
```

(Note that this example makes the view available at `/i18n/setlang/`.)

> **Warning**
>
> Make sure that you don't include the above URL within
> [`i18n_patterns()`](#django.conf.urls.i18n.i18n_patterns) \- it needs to be
> language-independent itself to work correctly.

The view expects to be called via the `POST` method, with a `language`
parameter set in request. If session support is enabled, the view saves the
language choice in the user's session. It also saves the language choice in a
cookie that is named `django_language` by default. (The name can be changed
through the [`LANGUAGE_COOKIE_NAME`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_NAME) setting.)

After setting the language choice, Django looks for a `next` parameter in the
`POST` or `GET` data. If that is found and Django considers it to be a safe
URL (i.e. it doesn't point to a different host and uses a safe scheme), a
redirect to that URL will be performed. Otherwise, Django may fall back to
redirecting the user to the URL from the `Referer` header or, if it is not
set, to `/`, depending on the nature of the request:

- If the request accepts HTML content (based on its `Accept` HTTP header),
  the fallback will always be performed.
- If the request doesn't accept HTML, the fallback will be performed only if
  the `next` parameter was set. Otherwise a 204 status code (No Content) will
  be returned.

Here's example HTML template code:

```html+django
{% load i18n %}

<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}">
    <select name="language">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
            <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </select>
    <input type="submit" value="Go">
</form>
```

In this example, Django looks up the URL of the page to which the user will be
redirected in the `redirect_to` context variable.

### 明示的にアクティブな言語をセットする

You may want to set the active language for the current session explicitly. Perhaps
a user's language preference is retrieved from another system, for example.
You've already been introduced to [`django.utils.translation.activate()`](/ja/4.0/ref/utils/#django.utils.translation.activate). That
applies to the current thread only. To persist the language for the entire
session in a cookie, set the [`LANGUAGE_COOKIE_NAME`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_NAME) cookie on the
response:

```python
from django.conf import settings
from django.http import HttpResponse
from django.utils import translation
user_language = 'fr'
translation.activate(user_language)
response = HttpResponse(...)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, user_language)
```

You would typically want to use both: [`django.utils.translation.activate()`](/ja/4.0/ref/utils/#django.utils.translation.activate)
changes the language for this thread, and setting the cookie makes this
preference persist in future requests.

### ビューやテンプレートの外で翻訳を使う

While Django provides a rich set of i18n tools for use in views and templates,
it does not restrict the usage to Django-specific code. The Django translation
mechanisms can be used to translate arbitrary texts to any language that is
supported by Django (as long as an appropriate translation catalog exists, of
course). You can load a translation catalog, activate it and translate text to
language of your choice, but remember to switch back to original language, as
activating a translation catalog is done on per-thread basis and such change
will affect code running in the same thread.

例:

```python
from django.utils import translation

def welcome_translated(language):
    cur_language = translation.get_language()
    try:
        translation.activate(language)
        text = translation.gettext('welcome')
    finally:
        translation.activate(cur_language)
    return text
```

Calling this function with the value `'de'` will give you `"Willkommen"`,
regardless of [`LANGUAGE_CODE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_CODE) and language set by middleware.

Functions of particular interest are
[`django.utils.translation.get_language()`](/ja/4.0/ref/utils/#django.utils.translation.get_language) which returns the language used
in the current thread, [`django.utils.translation.activate()`](/ja/4.0/ref/utils/#django.utils.translation.activate) which
activates a translation catalog for the current thread, and
[`django.utils.translation.check_for_language()`](/ja/4.0/ref/utils/#django.utils.translation.check_for_language)
which checks if the given language is supported by Django.

To help write more concise code, there is also a context manager
[`django.utils.translation.override()`](/ja/4.0/ref/utils/#django.utils.translation.override) that stores the current language on
enter and restores it on exit. With it, the above example becomes:

```python
from django.utils import translation

def welcome_translated(language):
    with translation.override(language):
        return translation.gettext('welcome')
```

### Language cookie

A number of settings can be used to adjust language cookie options:

- [`LANGUAGE_COOKIE_NAME`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_NAME)
- [`LANGUAGE_COOKIE_AGE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_AGE)
- [`LANGUAGE_COOKIE_DOMAIN`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_DOMAIN)
- [`LANGUAGE_COOKIE_HTTPONLY`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_HTTPONLY)
- [`LANGUAGE_COOKIE_PATH`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_PATH)
- [`LANGUAGE_COOKIE_SAMESITE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_SAMESITE)
- [`LANGUAGE_COOKIE_SECURE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_SECURE)

## Implementation notes

### Specialties of Django translation

Django's translation machinery uses the standard `gettext` module that comes
with Python. If you know `gettext`, you might note these specialties in the
way Django does translation:

- The string domain is `django` or `djangojs`. This string domain is
  used to differentiate between different programs that store their data
  in a common message-file library (usually `/usr/share/locale/`). The
  `django` domain is used for Python and template translation strings
  and is loaded into the global translation catalogs. The `djangojs`
  domain is only used for JavaScript translation catalogs to make sure
  that those are as small as possible.
- Django doesn't use `xgettext` alone. It uses Python wrappers around
  `xgettext` and `msgfmt`. This is mostly for convenience.

### How Django discovers language preference

Once you've prepared your translations -- or, if you want to use the
translations that come with Django -- you'll need to activate translation for
your app.

Behind the scenes, Django has a very flexible model of deciding which language
should be used -- installation-wide, for a particular user, or both.

To set an installation-wide language preference, set [`LANGUAGE_CODE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_CODE).
Django uses this language as the default translation -- the final attempt if no
better matching translation is found through one of the methods employed by the
locale middleware (see below).

If all you want is to run Django with your native language all you need to do
is set [`LANGUAGE_CODE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_CODE) and make sure the corresponding [message
files](/ja/4.0/topics/i18n/#term-message-file) and their compiled versions (`.mo`) exist.

If you want to let each individual user specify which language they
prefer, then you also need to use the `LocaleMiddleware`.
`LocaleMiddleware` enables language selection based on data from the request.
It customizes content for each user.

To use `LocaleMiddleware`, add `'django.middleware.locale.LocaleMiddleware'`
to your [`MIDDLEWARE`](/ja/4.0/ref/settings/#std-setting-MIDDLEWARE) setting. Because middleware order matters, follow
these guidelines:

- Make sure it's one of the first middleware installed.
- It should come after `SessionMiddleware`, because `LocaleMiddleware`
  makes use of session data. And it should come before `CommonMiddleware`
  because `CommonMiddleware` needs an activated language in order
  to resolve the requested URL.
- If you use `CacheMiddleware`, put `LocaleMiddleware` after it.

For example, your [`MIDDLEWARE`](/ja/4.0/ref/settings/#std-setting-MIDDLEWARE) might look like this:

```python
MIDDLEWARE = [
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.locale.LocaleMiddleware',
   'django.middleware.common.CommonMiddleware',
]
```

(For more on middleware, see the [middleware documentation](/ja/4.0/topics/http/middleware/).)

`LocaleMiddleware` tries to determine the user's language preference by
following this algorithm:

- First, it looks for the language prefix in the requested URL.  This is
  only performed when you are using the `i18n_patterns` function in your
  root URLconf. See [国際化: URL パターン内](#url-internationalization) for more information
  about the language prefix and how to internationalize URL patterns.
- Failing that, it looks for a cookie.

  The name of the cookie used is set by the [`LANGUAGE_COOKIE_NAME`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_COOKIE_NAME)
  setting. (The default name is `django_language`.)
- Failing that, it looks at the `Accept-Language` HTTP header. This
  header is sent by your browser and tells the server which language(s) you
  prefer, in order by priority. Django tries each language in the header
  until it finds one with available translations.
- Failing that, it uses the global [`LANGUAGE_CODE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_CODE) setting.

メモ:

- In each of these places, the language preference is expected to be in the
  standard [language format](/ja/4.0/topics/i18n/#term-language-code), as a string. For example,
  Brazilian Portuguese is `pt-br`.
- If a base language is available but the sublanguage specified is not,
  Django uses the base language. For example, if a user specifies `de-at`
  (Austrian German) but Django only has `de` available, Django uses
  `de`.
- Only languages listed in the [`LANGUAGES`](/ja/4.0/ref/settings/#std-setting-LANGUAGES) setting can be selected.
  If you want to restrict the language selection to a subset of provided
  languages (because your application doesn't provide all those languages),
  set [`LANGUAGES`](/ja/4.0/ref/settings/#std-setting-LANGUAGES) to a list of languages. For example:

  ```python
  LANGUAGES = [
      ('de', _('German')),
      ('en', _('English')),
  ]
  ```

  This example restricts languages that are available for automatic
  selection to German and English (and any sublanguage, like `de-ch` or
  `en-us`).
- If you define a custom [`LANGUAGES`](/ja/4.0/ref/settings/#std-setting-LANGUAGES) setting, as explained in the
  previous bullet, you can mark the language names as translation strings
  -- but use [`gettext_lazy()`](/ja/4.0/ref/utils/#django.utils.translation.gettext_lazy) instead of
  [`gettext()`](/ja/4.0/ref/utils/#django.utils.translation.gettext) to avoid a circular import.

  以下はサンプルの設定ファイルです:

  ```python
  from django.utils.translation import gettext_lazy as _

  LANGUAGES = [
      ('de', _('German')),
      ('en', _('English')),
  ]
  ```

Once `LocaleMiddleware` determines the user's preference, it makes this
preference available as `request.LANGUAGE_CODE` for each
[`HttpRequest`](/ja/4.0/ref/request-response/#django.http.HttpRequest). Feel free to read this value in your view
code. Here's an example:

```python
from django.http import HttpResponse

def hello_world(request, count):
    if request.LANGUAGE_CODE == 'de-at':
        return HttpResponse("You prefer to read Austrian German.")
    else:
        return HttpResponse("You prefer to read another language.")
```

Note that, with static (middleware-less) translation, the language is in
`settings.LANGUAGE_CODE`, while with dynamic (middleware) translation, it's
in `request.LANGUAGE_CODE`.

### How Django discovers translations

At runtime, Django builds an in-memory unified catalog of literals-translations.
To achieve this it looks for translations by following this algorithm regarding
the order in which it examines the different file paths to load the compiled
[message files](/ja/4.0/topics/i18n/#term-message-file) (`.mo`) and the precedence of multiple
translations for the same literal:

1. The directories listed in [`LOCALE_PATHS`](/ja/4.0/ref/settings/#std-setting-LOCALE_PATHS) have the highest
   precedence, with the ones appearing first having higher precedence than
   the ones appearing later.
2. Then, it looks for and uses if it exists a `locale` directory in each
   of the installed apps listed in [`INSTALLED_APPS`](/ja/4.0/ref/settings/#std-setting-INSTALLED_APPS).  The ones
   appearing first have higher precedence than the ones appearing later.
3. Finally, the Django-provided base translation in `django/conf/locale`
   is used as a fallback.

> **See also**
>
> The translations for literals included in JavaScript assets are looked up
> following a similar but not identical algorithm. See
> [`JavaScriptCatalog`](#django.views.i18n.JavaScriptCatalog) for more details.
>
> You can also put [custom format files](/ja/4.0/topics/i18n/formatting/#custom-format-files) in the
> [`LOCALE_PATHS`](/ja/4.0/ref/settings/#std-setting-LOCALE_PATHS) directories if you also set
> [`FORMAT_MODULE_PATH`](/ja/4.0/ref/settings/#std-setting-FORMAT_MODULE_PATH).

In all cases the name of the directory containing the translation is expected to
be named using [locale name](/ja/4.0/topics/i18n/#term-locale-name) notation. E.g. `de`, `pt_BR`, `es_AR`,
etc. Untranslated strings for territorial language variants use the translations
of the generic language. For example, untranslated `pt_BR` strings use `pt`
translations.

This way, you can write applications that include their own translations, and
you can override base translations in your project. Or, you can build a big
project out of several apps and put all translations into one big common
message file specific to the project you are composing. The choice is yours.

All message file repositories are structured the same way. They are:

- All paths listed in [`LOCALE_PATHS`](/ja/4.0/ref/settings/#std-setting-LOCALE_PATHS) in your settings file are
  searched for `<language>/LC_MESSAGES/django.(po|mo)`
- `$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`
- `$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)`

To create message files, you use the [`django-admin makemessages`](/ja/4.0/ref/django-admin/#django-admin-makemessages)
tool. And you use [`django-admin compilemessages`](/ja/4.0/ref/django-admin/#django-admin-compilemessages)
to produce the binary `.mo` files that are used by `gettext`.

You can also run [`django-admin compilemessages
--settings=path.to.settings`](/ja/4.0/ref/django-admin/#django-admin-compilemessages) to make the compiler process all
the directories in your [`LOCALE_PATHS`](/ja/4.0/ref/settings/#std-setting-LOCALE_PATHS) setting.

### Using a non-English base language

Django makes the general assumption that the original strings in a translatable
project are written in English. You can choose another language, but you must be
aware of certain limitations:

- `gettext` only provides two plural forms for the original messages, so you
  will also need to provide a translation for the base language to include all
  plural forms if the plural rules for the base language are different from
  English.
- When an English variant is activated and English strings are missing, the
  fallback language will not be the [`LANGUAGE_CODE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_CODE) of the project,
  but the original strings. For example, an English user visiting a site with
  [`LANGUAGE_CODE`](/ja/4.0/ref/settings/#std-setting-LANGUAGE_CODE) set to Spanish and original strings written in
  Russian will see Russian text rather than Spanish.
