{"title":"表示形式のローカル化","version":"4.2","locale":"ja","docname":"topics/i18n/formatting","url":"/ja/4.2/topics/i18n/formatting/","canonical":"https://djangodocs.dev/ja/4.2/topics/i18n/formatting/","summary":"概要 Link to this heading # Django の表示形式システムは、カレント ロケール に合った表示形式を使って日付、時刻、数値をテンプレート内で表示することができます。また、フォームでのローカル化された入力も処理します。 この機能が有効のとき、同じコンテンツにアクセスする 2…","html":"<h1>表示形式のローカル化<a class=\"heading-anchor\" href=\"#format-localization\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"overview\">\n<h2>概要<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django の表示形式システムは、カレント <a class=\"reference internal\" href=\"/ja/4.2/topics/i18n/#term-locale-name\"><span class=\"xref std std-term\">ロケール</span></a> に合った表示形式を使って日付、時刻、数値をテンプレート内で表示することができます。また、フォームでのローカル化された入力も処理します。</p>\n<p>この機能が有効のとき、同じコンテンツにアクセスする 2 人のユーザは、違う形で日付、時刻、数値を目にすることになるでしょう。これは、現在のロケールの表示形式に依存します。</p>\n<p>The formatting system is enabled by default. To disable it, it's\nnecessary to set <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-USE_L10N\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_L10N</span> <span class=\"pre\">=</span> <span class=\"pre\">False</span></code></a> in your settings file.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>To enable number formatting with thousand separators, it is necessary to\nset <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-USE_THOUSAND_SEPARATOR\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_THOUSAND_SEPARATOR</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code></a> in\nyour settings file. Alternatively, you could use <a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/humanize/#std-templatefilter-intcomma\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">intcomma</span></code></a> to\nformat numbers in your template.</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>There is a related <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-USE_I18N\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_I18N</span></code></a> setting that controls if Django\nshould activate translation. See <a class=\"reference internal\" href=\"/ja/4.2/topics/i18n/translation/\"><span class=\"doc\">翻訳</span></a> for more\ndetails.</p>\n</aside>\n</section>\n<section id=\"locale-aware-input-in-forms\">\n<h2>フォームでのロケールを認識する入力<a class=\"heading-anchor\" href=\"#locale-aware-input-in-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>表示形式機能が有効のとき、Django がフォーム内で日付、時刻、数値を描画する際に、ローカル化された表示形式を使用することができます。フォームにデータが入力されたとき、ユーザによって使用される表示形式を推測し、異なるロケールに対する異なる表示形式を試みることを意味します。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>Django は、データを描画するために使うものに対して、データを表示するための異なる表示形式を使います。特に、日付を描画するための表示形式は、<code class=\"docutils literal notranslate\"><span class=\"pre\">%a</span></code> (省略された曜日名)、<code class=\"docutils literal notranslate\"><span class=\"pre\">%A</span></code> (曜日名), <code class=\"docutils literal notranslate\"><span class=\"pre\">%b</span></code> (省略された月名)、<code class=\"docutils literal notranslate\"><span class=\"pre\">%B</span></code> (月名)、<code class=\"docutils literal notranslate\"><span class=\"pre\">%p</span></code> (AM/PM) を使うことができません。</p>\n</aside>\n<p>To enable a form field to localize input and output data use its <code class=\"docutils literal notranslate\"><span class=\"pre\">localize</span></code>\nargument:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">CashRegisterForm</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Form</span><span class=\"p\">):</span>\n    <span class=\"n\">product</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">()</span>\n    <span class=\"n\">revenue</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">DecimalField</span><span class=\"p\">(</span><span class=\"n\">max_digits</span><span class=\"o\">=</span><span class=\"mi\">4</span><span class=\"p\">,</span> <span class=\"n\">decimal_places</span><span class=\"o\">=</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"n\">localize</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"controlling-localization-in-templates\">\n<span id=\"topic-l10n-templates\"></span><h2>テンプレート内でローカル化をコントロールする<a class=\"heading-anchor\" href=\"#controlling-localization-in-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-USE_L10N\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_L10N</span></code></a> で表示形式を有効にしたとき、Django はテンプレート内で値が出力されたときはいつでもロケールに対応した表示形式を使います。</p>\n<p>しかし、ローカル化された値を使うことが適切ではないこともあります -- 例えば、機械が読み込むためにデザインされた JavaScript や XML を出力している場合、どんなときでも非ローカルの値が必要でしょう。また、全ての場所でローカル化を適用するのではなく、特定のテンプレートだけで使いたいときもあるかもしれません。</p>\n<p>ローカル化の仕様について細かいコントロールをするために、Django は <code class=\"docutils literal notranslate\"><span class=\"pre\">l10n</span></code> テンプレートライブラリを提供しています。これは、以下のタグやフィルタを含んでいます。</p>\n<section id=\"template-tags\">\n<h3>テンプレートタグ<a class=\"heading-anchor\" href=\"#template-tags\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"localize\">\n<span id=\"std-templatetag-localize\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">localize</span></code><a class=\"heading-anchor\" href=\"#localize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>ブロック内で、テンプレート変数のローカル化を有効化または無効化します。</p>\n<p>このタグでは、<a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-USE_L10N\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_L10N</span></code></a> よりも細かい粒度のローカル化のコントロールができます。</p>\n<p>To activate or deactivate localization for a template block, use:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">l10n</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">localize</span> <span class=\"nv\">on</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">value</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endlocalize</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">localize</span> <span class=\"nv\">off</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">value</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endlocalize</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p><a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-USE_L10N\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_L10N</span></code></a> の値は、<code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">localize</span> <span class=\"pre\">%}</span></code> ブロック内では尊重されません。</p>\n</aside>\n<p>各変数ベースで同じ仕事をするテンプレートフィルタについて、<a class=\"reference internal\" href=\"#std-templatefilter-localize\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">localize</span></code></a> と <a class=\"reference internal\" href=\"#std-templatefilter-unlocalize\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">unlocalize</span></code></a> を参照してください。</p>\n</section>\n</section>\n<section id=\"template-filters\">\n<h3>テンプレートフィルタ<a class=\"heading-anchor\" href=\"#template-filters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"std-templatefilter-localize\">\n<span id=\"id1\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">localize</span></code><a class=\"heading-anchor\" href=\"#std-templatefilter-localize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>単数のローカル化を強制します。</p>\n<p>例えば:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">l10n</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">localize</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>単数のローカル化を無効化するには、<a class=\"reference internal\" href=\"#std-templatefilter-unlocalize\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">unlocalize</span></code></a> を使ってください。テンプレートの大きな範囲を通じてローカル化をコントロールするには、<a class=\"reference internal\" href=\"#std-templatetag-localize\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">localize</span></code></a> テンプレートタグを使ってください。</p>\n</section>\n<section id=\"unlocalize\">\n<span id=\"std-templatefilter-unlocalize\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">unlocalize</span></code><a class=\"heading-anchor\" href=\"#unlocalize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>単数形がローカル化されずに表示されるように強制します。</p>\n<p>例えば:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">l10n</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">unlocalize</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>単数のローカル化を強制するには、<a class=\"reference internal\" href=\"#std-templatefilter-localize\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">localize</span></code></a> を使ってください。テンプレートの大きな範囲を通じてローカル化をコントロールするには、<a class=\"reference internal\" href=\"#std-templatetag-localize\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">localize</span></code></a> テンプレートタグを使ってください。</p>\n<p>Returns a string representation for unlocalized numbers  (<code class=\"docutils literal notranslate\"><span class=\"pre\">int</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">float</span></code>,\nor <code class=\"docutils literal notranslate\"><span class=\"pre\">Decimal</span></code>).</p>\n</section>\n</section>\n</section>\n<section id=\"creating-custom-format-files\">\n<span id=\"custom-format-files\"></span><h2>独自の表示形式ファイルを作成する<a class=\"heading-anchor\" href=\"#creating-custom-format-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django provides format definitions for many locales, but sometimes you might\nwant to create your own, because a format file doesn't exist for your locale,\nor because you want to overwrite some of the values.</p>\n<p>To use custom formats, specify the path where you'll place format files\nfirst.  To do that, set your <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-FORMAT_MODULE_PATH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FORMAT_MODULE_PATH</span></code></a> setting to the\npackage where format files will exist, for instance:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">FORMAT_MODULE_PATH</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;mysite.formats&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;some_app.formats&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Files are not placed directly in this directory, but in a directory named as\nthe locale, and must be named <code class=\"docutils literal notranslate\"><span class=\"pre\">formats.py</span></code>. Be careful not to put sensitive\ninformation in these files as values inside can be exposed if you pass the\nstring to <code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.formats.get_format()</span></code> (used by the <a class=\"reference internal\" href=\"/ja/4.2/ref/templates/builtins/#std-templatefilter-date\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">date</span></code></a>\ntemplate filter).</p>\n<p>To customize the English formats, a structure like this would be needed:</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Text code\"><code>mysite/\n    formats/\n        __init__.py\n        en/\n            __init__.py\n            formats.py\n</code></pre></div>\n<p>where <code class=\"file docutils literal notranslate\"><span class=\"pre\">formats.py</span></code> contains custom format definitions. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">THOUSAND_SEPARATOR</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;</span><span class=\"se\">\\xa0</span><span class=\"s2\">&quot;</span>\n</code></pre></div>\n<p>to use a non-breaking space (Unicode <code class=\"docutils literal notranslate\"><span class=\"pre\">00A0</span></code>) as a thousand separator,\ninstead of the default for English, a comma.</p>\n</section>\n<section id=\"limitations-of-the-provided-locale-formats\">\n<h2>提供されているロケール表示形式の限界<a class=\"heading-anchor\" href=\"#limitations-of-the-provided-locale-formats\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>いくつかのロケールは、数字に対して文脈依存の表示形式を使います。これは、Django のローカル化システムでは自動的に扱うことはできません。</p>\n<section id=\"switzerland-german\">\n<h3>スイス (ドイツ語)<a class=\"heading-anchor\" href=\"#switzerland-german\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>スイスの数値の表示形式は、対象となる数字の種類によって決まります。貨幣の値に対しては、カンマが 1000 区切りに使われ、小数点が小数区切りに使われます。他の数字に対しては、カンマは小数区切りとして使われ、空白が 1000 区切りとして使われます。Django によって提供されるロケール表示形式は、一般的な区切り文字、つまり小数にはカンマそして1000 区切りには空白を使用します。</p>\n</section>\n</section>","rootId":"format-localization","toc":[{"title":"概要","anchor":"overview","children":[]},{"title":"フォームでのロケールを認識する入力","anchor":"locale-aware-input-in-forms","children":[]},{"title":"テンプレート内でローカル化をコントロールする","anchor":"controlling-localization-in-templates","children":[{"title":"テンプレートタグ","anchor":"template-tags","children":[{"title":"localize","anchor":"localize","children":[]}]},{"title":"テンプレートフィルタ","anchor":"template-filters","children":[{"title":"localize","anchor":"std-templatefilter-localize","children":[]},{"title":"unlocalize","anchor":"unlocalize","children":[]}]}]},{"title":"独自の表示形式ファイルを作成する","anchor":"creating-custom-format-files","children":[]},{"title":"提供されているロケール表示形式の限界","anchor":"limitations-of-the-provided-locale-formats","children":[{"title":"スイス (ドイツ語)","anchor":"switzerland-german","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Django を使う","url":"/ja/4.2/topics/"},{"docname":"topics/i18n/index","title":"国際化とローカル化","url":"/ja/4.2/topics/i18n/"}],"prev":{"docname":"topics/i18n/translation","title":"翻訳","url":"/ja/4.2/topics/i18n/translation/"},"next":{"docname":"topics/i18n/timezones","title":"タイムゾーン","url":"/ja/4.2/topics/i18n/timezones/"},"formats":{"html":"/ja/4.2/topics/i18n/formatting/","markdown":"/ja/4.2/topics/i18n/formatting.md","json":"/ja/4.2/topics/i18n/formatting.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/topics/i18n/formatting.txt","official":"https://docs.djangoproject.com/ja/4.2/topics/i18n/formatting/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}