HUGO ja 非公式

  • ニュース
  • ドキュメント
  • テーマ
  • コミュニティ
  • GitHub
gohugoio Star づけ
  暫定公開中 2024/09/16 (94d7f576a 対応, 2024/09/15)
  • Hugo について
    • 本節
    • Introduction
    • Hugo の機能
    • Privacy
    • セキュリティ
    • ライセンス
  • インストール
    • 本節
    • macOS
    • Linux
    • Windows
    • BSD
  • はじめよう
    • 本節
    • クイックスタート
    • ディレクトリ構造
    • 基本操作
    • 設定
    • Configure markup
    • 用語集
    • 本書以外の学習リソース
  • クイックリファレンス
    • 本節
    • Emojis
    • Functions
    • Methods
    • Page collections
  • コンテント管理
    • 本節
    • コンテントの構成
    • ページバンドル
    • コンテントフォーマット
    • フロントマター
    • ビルドオプション
    • ページリソース
    • イメージ処理
    • ショートコード
    • 関連コンテント
    • Sections
    • Content types
    • アーキタイプ
    • 分類
    • Summaries
    • Links and cross references
    • URL 管理
    • メニュー
    • コメント
    • マルチ言語
    • Markdown attributes
    • シンタックスハイライト
    • Diagrams
    • Mathematics
    • Data sources
    • Content adapters
  • テンプレート
    • 本節
    • はじめに
    • Template types
    • Lookup order
    • 基本テンプレート
    • Home templates
    • Single templates
    • Section templates
    • Taxonomy templates
    • Term templates
    • 部分テンプレート
    • コンテントビューテンプレート
    • ショートコードテンプレート
    • サイトマップテンプレート
    • RSS テンプレート
    • 404 テンプレート
    • robots.txt templates
    • メニュー
    • ページネーション
    • Embedded templates
    • Custom output formats
  • 関数
    • 本節
    • cast
    • collections
    • compare
    • crypto
    • css
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hash
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • メソッド
    • 本節
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pager
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • レンダーフック
    • 本節
    • Introduction
    • Blockquotes
    • Code blocks
    • Headings
    • Images
    • Links
    • Passthrough
    • Tables
  • Hugo モジュール
    • In this section
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo パイプ
    • 本節
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • トラブルシューティング
    • 本節
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • 開発ツール
    • 本節
    • Editor plugins
    • Front-ends
    • 検索
    • Migrations
    • Other projects
  • ホスティングと開発
    • 本節
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • 貢献
    • 本節
    • Development
    • ドキュメント
    • Themes
  • メンテナンス
関数 LANG FUNCTIONS

lang.Translate

Translates a string using the translation tables in the i18n directory.

文法

lang.Translate KEY [CONTEXT]

戻り値

string

エイリアス

T
i18n

The lang.Translate function returns the value associated with given key as defined in the translation table for the current language.

If the key is not found in the translation table for the current language, the lang.Translate function falls back to the translation table for the defaultContentLanguage.

If the key is not found in the translation table for the defaultContentLanguage, the lang.Translate function returns an empty string.

To list missing and fallback translations, use the --printI18nWarnings flag when building your site.

To render placeholders for missing and fallback translations, set enableMissingTranslationPlaceholders to true in your site configuration.

Translation tables

Create translation tables in the i18n directory, naming each file according to RFC 5646. Translation tables may be JSON, TOML, or YAML. For example:

i18n/en.toml
i18n/en-US.toml

The base name must match the language key as defined in your site configuration.

Artificial languages with private use subtags as defined in RFC 5646 § 2.2.7 are also supported. You may omit the art-x- prefix for brevity. For example:

i18n/art-x-hugolang.toml
i18n/hugolang.toml

Private use subtags must not exceed 8 alphanumeric characters.

Simple translations

Let’s say your multilingual site supports two languages, English and Polish. Create a translation table for each language in the i18n directory.

i18n/
├── en.toml
└── pl.toml

The English translation table:

i18n/en.
     
privacy: privacy
security: security
privacy = 'privacy'
security = 'security'
{
   "privacy": "privacy",
   "security": "security"
}

The Polish translation table:

i18n/pl.
     
privacy: prywatność
security: bezpieczeństwo
privacy = 'prywatność'
security = 'bezpieczeństwo'
{
   "privacy": "prywatność",
   "security": "bezpieczeństwo"
}

The examples below use the T alias for brevity.

When viewing the English language site:

{{ T "privacy" }} → privacy
{{ T "security" }} → security

When viewing the Polish language site:

{{ T "privacy" }} → prywatność
{{ T "security" }} → bezpieczeństwo

Translations with pluralization

Let’s say your multilingual site supports two languages, English and Polish. Create a translation table for each language in the i18n directory.

i18n/
├── en.toml
└── pl.toml

The Unicode CLDR Plural Rules chart describes the pluralization categories for each language.

The English translation table:

i18n/en.
     
day:
  one: day
  other: days
day_with_count:
  one: '{{ . }} day'
  other: '{{ . }} days'
[day]
  one = 'day'
  other = 'days'
[day_with_count]
  one = '{{ . }} day'
  other = '{{ . }} days'
{
   "day": {
      "one": "day",
      "other": "days"
   },
   "day_with_count": {
      "one": "{{ . }} day",
      "other": "{{ . }} days"
   }
}

The Polish translation table:

i18n/pl.
     
day:
  few: miesiące
  many: miesięcy
  one: miesiąc
  other: miesiąca
day_with_count:
  few: '{{ . }} miesiące'
  many: '{{ . }} miesięcy'
  one: '{{ . }} miesiąc'
  other: '{{ . }} miesiąca'
[day]
  few = 'miesiące'
  many = 'miesięcy'
  one = 'miesiąc'
  other = 'miesiąca'
[day_with_count]
  few = '{{ . }} miesiące'
  many = '{{ . }} miesięcy'
  one = '{{ . }} miesiąc'
  other = '{{ . }} miesiąca'
{
   "day": {
      "few": "miesiące",
      "many": "miesięcy",
      "one": "miesiąc",
      "other": "miesiąca"
   },
   "day_with_count": {
      "few": "{{ . }} miesiące",
      "many": "{{ . }} miesięcy",
      "one": "{{ . }} miesiąc",
      "other": "{{ . }} miesiąca"
   }
}

The examples below use the T alias for brevity.

When viewing the English language site:

{{ T "day" 0 }} → days
{{ T "day" 1 }} → day
{{ T "day" 2 }} → days
{{ T "day" 5 }} → days

{{ T "day_with_count" 0 }} → 0 days
{{ T "day_with_count" 1 }} → 1 day
{{ T "day_with_count" 2 }} → 2 days
{{ T "day_with_count" 5 }} → 5 days

When viewing the Polish language site:

{{ T "day" 0 }} → miesięcy
{{ T "day" 1 }} → miesiąc
{{ T "day" 2 }} → miesiące
{{ T "day" 5 }} → miesięcy

{{ T "day_with_count" 0 }} → 0 miesięcy
{{ T "day_with_count" 1 }} → 1 miesiąc
{{ T "day_with_count" 2 }} → 2 miesiące
{{ T "day_with_count" 5 }} → 5 miesięcy

In the pluralization examples above, we passed an integer in context (the second argument). You can also pass a map in context, providing a count key to control pluralization.

Translation table:

i18n/en.
     
age:
  one: '{{ .name }} is {{ .count }} year old.'
  other: '{{ .name }} is {{ .count }} years old.'
[age]
  one = '{{ .name }} is {{ .count }} year old.'
  other = '{{ .name }} is {{ .count }} years old.'
{
   "age": {
      "one": "{{ .name }} is {{ .count }} year old.",
      "other": "{{ .name }} is {{ .count }} years old."
   }
}

Template code:

{{ T "age" (dict "name" "Will" "count" 1) }} → Will is 1 year old.
{{ T "age" (dict "name" "John" "count" 3) }} → John is 3 years old.

Translation tables may contain both simple translations and translations with pluralization.

Reserved keys

Hugo uses the go-i18n package to look up values in translation tables. This package reserves the following keys for internal use:

id
(string) Uniquely identifies the message.
description
(string) Describes the message to give additional context to translators that may be relevant for translation.
hash
(string) Uniquely identifies the content of the message that this message was translated from.
leftdelim
(string) The left Go template delimiter.
rightdelim
(string) The right Go template delimiter.
zero
(string) The content of the message for the CLDR plural form “zero”.
one
(string) The content of the message for the CLDR plural form “one”.
two
(string) The content of the message for the CLDR plural form “two”.
few
(string) The content of the message for the CLDR plural form “few”.
many
(string) The content of the message for the CLDR plural form “many”.
other
(string) The content of the message for the CLDR plural form “other”.

If you need to provide a translation for one of the reserved keys, you can prepend the word with an underscore. For example:

i18n/es.
     
_description: descripción
_few: pocos
_many: muchos
_one: uno
_other: otro
_two: dos
_zero: cero
_description = 'descripción'
_few = 'pocos'
_many = 'muchos'
_one = 'uno'
_other = 'otro'
_two = 'dos'
_zero = 'cero'
{
   "_description": "descripción",
   "_few": "pocos",
   "_many": "muchos",
   "_one": "uno",
   "_other": "otro",
   "_two": "dos",
   "_zero": "cero"
}

Then in your templates:

{{ T "_description" }} → descripción
{{ T "_few" }} → pocos
{{ T "_many" }} → muchos
{{ T "_one" }} → uno
{{ T "_two" }} → dos
{{ T "_zero" }} → cero
{{ T "_other" }} → otro

このページ内

  • Translation tables
  • Simple translations
  • Translations with pluralization
  • Reserved keys

このセクション内

  • lang.FormatAccounting
  • lang.FormatCurrency
  • lang.FormatNumber
  • lang.FormatNumberCustom
  • lang.FormatPercent
  • lang.Merge
  • lang.Translate
最終更新日付: 0001/01/01
ページの変更
Hugo 作者より
Hugo Logo
  • Issue 報告
  • ヘルプ
  • @GoHugoIO
  • @spf13
  • @bepsays
 

Hugo Sponsors

Route4Me
Your Company?
 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • ニュース
  • ドキュメント
  • テーマ
  • コミュニティ
  • GitHub
  • Hugo について
  • インストール
  • はじめよう
  • クイックリファレンス
  • コンテント管理
  • テンプレート
  • 関数
  • メソッド
  • レンダーフック
  • Hugo モジュール
  • Hugo パイプ
  • CLI
  • トラブルシューティング
  • 開発ツール
  • ホスティングと開発
  • 貢献
  • メンテナンス