lang.Translate
Syntax
lang.Translate KEY [CONTEXT] ⟼ string
Aliases
i18n
T
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 translation tables can contain both:
- Simple translations
- Translations with pluralization
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'
privacy: privacy
security: security
privacy = 'privacy'
security = 'security'
[day]
one = 'day'
other = 'days'
[day_with_count]
one = '{{ . }} day'
other = '{{ . }} days'
{
"day": {
"one": "day",
"other": "days"
},
"day_with_count": {
"one": "{{ . }} day",
"other": "{{ . }} days"
},
"privacy": "privacy",
"security": "security"
}
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'
privacy: prywatność
security: bezpieczeństwo
privacy = 'prywatność'
security = 'bezpieczeństwo'
[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"
},
"privacy": "prywatność",
"security": "bezpieczeństwo"
}
When viewing the English language site:
{{ T "privacy" }} → privacy
{{ T "security" }} → security
{{ 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 "privacy" }} → prywatność
{{ T "security" }} → bezpieczeństwo
{{ 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, creating 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:
{{ T "age" (dict "name" "Will" "count" 1) }} → Will is 1 year old.
{{ T "age" (dict "name" "John" "count" 3) }} → John is 3 years old.