表示形式のローカル化Link to this heading
概要Link to this heading
Django の表示形式システムは、カレント ロケール に合った表示形式を使って日付、時刻、数値をテンプレート内で表示することができます。また、フォームでのローカル化された入力も処理します。
この機能が有効のとき、同じコンテンツにアクセスする 2 人のユーザは、違う形で日付、時刻、数値を目にすることになるでしょう。これは、現在のロケールの表示形式に依存します。
The formatting system is enabled by default. To disable it, it's
necessary to set USE_L10N = False in your settings file.
フォームでのロケールを認識する入力Link to this heading
表示形式機能が有効のとき、Django がフォーム内で日付、時刻、数値を描画する際に、ローカル化された表示形式を使用することができます。フォームにデータが入力されたとき、ユーザによって使用される表示形式を推測し、異なるロケールに対する異なる表示形式を試みることを意味します。
To enable a form field to localize input and output data use its localize
argument:
class CashRegisterForm(forms.Form):
product = forms.CharField()
revenue = forms.DecimalField(max_digits=4, decimal_places=2, localize=True)
テンプレート内でローカル化をコントロールするLink to this heading
USE_L10N で表示形式を有効にしたとき、Django はテンプレート内で値が出力されたときはいつでもロケールに対応した表示形式を使います。
しかし、ローカル化された値を使うことが適切ではないこともあります -- 例えば、機械が読み込むためにデザインされた JavaScript や XML を出力している場合、どんなときでも非ローカルの値が必要でしょう。また、全ての場所でローカル化を適用するのではなく、特定のテンプレートだけで使いたいときもあるかもしれません。
ローカル化の仕様について細かいコントロールをするために、Django は l10n テンプレートライブラリを提供しています。これは、以下のタグやフィルタを含んでいます。
テンプレートフィルタLink to this heading
localizeLink to this heading
単数のローカル化を強制します。
例:
{% load l10n %}
{{ value|localize }}
単数のローカル化を無効化するには、unlocalize を使ってください。テンプレートの大きな範囲を通じてローカル化をコントロールするには、localize テンプレートタグを使ってください。
unlocalizeLink to this heading
単数形がローカル化されずに表示されるように強制します。
例:
{% load l10n %}
{{ value|unlocalize }}
単数のローカル化を強制するには、localize を使ってください。テンプレートの大きな範囲を通じてローカル化をコントロールするには、localize テンプレートタグを使ってください。
Returns a string representation for unlocalized numbers (int, float,
or Decimal).
独自の表示形式ファイルを作成するLink to this heading
Django provides format definitions for many locales, but sometimes you might want to create your own, because a format file doesn't exist for your locale, or because you want to overwrite some of the values.
To use custom formats, specify the path where you'll place format files
first. To do that, set your FORMAT_MODULE_PATH setting to the
package where format files will exist, for instance:
FORMAT_MODULE_PATH = [
'mysite.formats',
'some_app.formats',
]
Files are not placed directly in this directory, but in a directory named as
the locale, and must be named formats.py. Be careful not to put sensitive
information in these files as values inside can be exposed if you pass the
string to django.utils.formats.get_format() (used by the date
template filter).
To customize the English formats, a structure like this would be needed:
mysite/
formats/
__init__.py
en/
__init__.py
formats.py
where formats.py contains custom format definitions. For example:
THOUSAND_SEPARATOR = '\xa0'
to use a non-breaking space (Unicode 00A0) as a thousand separator,
instead of the default for English, a comma.
提供されているロケール表示形式の限界Link to this heading
いくつかのロケールは、数字に対して文脈依存の表示形式を使います。これは、Django のローカル化システムでは自動的に扱うことはできません。
スイス (ドイツ語)Link to this heading
スイスの数値の表示形式は、対象となる数字の種類によって決まります。貨幣の値に対しては、カンマが 1000 区切りに使われ、小数点が小数区切りに使われます。他の数字に対しては、カンマは小数区切りとして使われ、空白が 1000 区切りとして使われます。Django によって提供されるロケール表示形式は、一般的な区切り文字、つまり小数にはカンマそして1000 区切りには空白を使用します。