lang.Translate
lang.Translate
函數會回傳在當前語言的翻譯表中,給定鍵對應的值。
如果當前語言的翻譯表中找不到此鍵,則 lang.Translate
會回退到 [defaultContentLanguage
] 的翻譯表。
如果在 defaultContentLanguage
的翻譯表中也找不到此鍵,則 lang.Translate
會回傳空字串。
翻譯表
在 i18n
目錄中創建翻譯表,並根據 RFC 5646 命名每個檔案。翻譯表可以是 JSON、TOML 或 YAML 格式。例如:
i18n/en.toml
i18n/en-US.toml
基本名稱必須與網站配置中定義的語言鍵匹配。
也支持帶有私人使用子標籤的人工語言,如 RFC 5646 § 2.2.7 所定義的。您可以省略 art-x-
前綴。例如:
i18n/art-x-hugolang.toml
i18n/hugolang.toml
私人使用子標籤不能超過 8 個字母或數字。
簡單翻譯
假設您的多語言網站支持兩種語言:英語和波蘭語。為每種語言在 i18n
目錄中創建翻譯表。
i18n/
├── en.toml
└── pl.toml
英文翻譯表:
i18n/en.
privacy: privacy
security: security
privacy = 'privacy'
security = 'security'
{
"privacy": "privacy",
"security": "security"
}
波蘭文翻譯表:
i18n/pl.
privacy: prywatność
security: bezpieczeństwo
privacy = 'prywatność'
security = 'bezpieczeństwo'
{
"privacy": "prywatność",
"security": "bezpieczeństwo"
}
查看英文網站時:
{{ T "privacy" }} → privacy
{{ T "security" }} → security
查看波蘭文網站時:
{{ T "privacy" }} → prywatność
{{ T "security" }} → bezpieczeństwo
帶有複數形式的翻譯
假設您的多語言網站支持兩種語言:英語和波蘭語。為每種語言在 i18n
目錄中創建翻譯表。
i18n/
├── en.toml
└── pl.toml
英語翻譯表:
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"
}
}
波蘭文翻譯表:
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"
}
}
查看英文網站時:
{{ 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
查看波蘭文網站時:
{{ 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
在上述的複數形式範例中,我們傳遞了一個整數作為上下文(第二個參數)。您也可以傳遞一個映射作為上下文,並提供 count
鍵來控制複數形式。
翻譯表:
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."
}
}
範本程式碼:
{{ T "age" (dict "name" "Will" "count" 1) }} → Will is 1 year old.
{{ T "age" (dict "name" "John" "count" 3) }} → John is 3 years old.
保留鍵
Hugo 使用 go-i18n 包來查找翻譯表中的值。該包為內部使用保留了以下鍵:
id
(string
) 唯一標識消息description
(string
) 描述消息以提供給翻譯者的額外上下文hash
(string
) 唯一標識翻譯的消息leftdelim
(string
) 左側 Go 模板分隔符rightdelim
(string
) 右側 Go 模板分隔符zero
(string
) 用於 CLDR 複數形式 “zero” 的消息內容one
(string
) 用於 CLDR 複數形式 “one” 的消息內容two
(string
) 用於 CLDR 複數形式 “two” 的消息內容few
(string
) 用於 CLDR 複數形式 “few” 的消息內容many
(string
) 用於 CLDR 複數形式 “many” 的消息內容other
(string
) 用於 CLDR 複數形式 “other” 的消息內容
如果您需要為保留鍵提供翻譯,可以將單詞加上下劃線前綴。例如:
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"
}
然後在您的範本中:
{{ T "_description" }} → descripción
{{ T "_few" }} → pocos
{{ T "_many" }} → muchos
{{ T "_one" }} → uno
{{ T "_two" }} → dos
{{ T "_zero" }} → cero
{{ T "_other" }} → otro