| 1 |
--- |
| 2 |
title: lang.Translate |
| 3 |
description: Translates a string using the translation tables in the i18n directory. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [T, i18n] |
| 9 |
returnType: string |
| 10 |
signatures: ['lang.Translate KEY [CONTEXT]'] |
| 11 |
aliases: [/functions/i18n] |
| 12 |
--- |
| 13 |
|
| 14 |
The `lang.Translate` function returns the value associated with the given key by searching the current language's [translation tables](#translation-tables), then those for the [`defaultContentLanguage`][]. |
| 15 |
|
| 16 |
If not found, the function returns an empty string. |
| 17 |
|
| 18 |
> [!NOTE] |
| 19 |
> To list missing and fallback translations, set [`printI18nWarnings`][] to `true` in your project configuration, or use the `--printI18nWarnings` flag when building your project. |
| 20 |
> |
| 21 |
> To render placeholders for missing and fallback translations, set [`enableMissingTranslationPlaceholders`][] to `true` in your project configuration. |
| 22 |
|
| 23 |
## Translation tables |
| 24 |
|
| 25 |
{{% glossary-term "translation table" %}} |
| 26 |
|
| 27 |
For example: |
| 28 |
|
| 29 |
```text |
| 30 |
i18n/en.toml |
| 31 |
i18n/pt-BR.toml |
| 32 |
``` |
| 33 |
|
| 34 |
Hugo searches for a matching translation table using the following base names, in order: |
| 35 |
|
| 36 |
1. The [`locale`][] of the current language |
| 37 |
1. The [key][] of the current language |
| 38 |
1. The locale of the [`defaultContentLanguage`][] |
| 39 |
1. The key of the [`defaultContentLanguage`][] |
| 40 |
|
| 41 |
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: |
| 42 |
|
| 43 |
```text |
| 44 |
i18n/art-x-hugolang.toml |
| 45 |
i18n/hugolang.toml |
| 46 |
``` |
| 47 |
|
| 48 |
> [!NOTE] |
| 49 |
> Private use subtags must not exceed 8 alphanumeric characters. |
| 50 |
|
| 51 |
## Simple translations |
| 52 |
|
| 53 |
Let's say your multilingual project supports two languages, English and Polish. Create a translation table for each language in the `i18n` directory. |
| 54 |
|
| 55 |
```tree |
| 56 |
i18n/ |
| 57 |
├── en.toml |
| 58 |
└── pl.toml |
| 59 |
``` |
| 60 |
|
| 61 |
The English translation table: |
| 62 |
|
| 63 |
{{< code-toggle file=i18n/en >}} |
| 64 |
privacy = 'privacy' |
| 65 |
security = 'security' |
| 66 |
{{< /code-toggle >}} |
| 67 |
|
| 68 |
The Polish translation table: |
| 69 |
|
| 70 |
{{< code-toggle file=i18n/pl >}} |
| 71 |
privacy = 'prywatność' |
| 72 |
security = 'bezpieczeństwo' |
| 73 |
{{< /code-toggle >}} |
| 74 |
|
| 75 |
> [!NOTE] |
| 76 |
> The examples below use the `T` alias for brevity. |
| 77 |
|
| 78 |
When viewing the English language site: |
| 79 |
|
| 80 |
```go-html-template |
| 81 |
{{ T "privacy" }} → privacy |
| 82 |
{{ T "security" }} → security |
| 83 |
```` |
| 84 |
|
| 85 |
When viewing the Polish language site: |
| 86 |
|
| 87 |
```go-html-template |
| 88 |
{{ T "privacy" }} → prywatność |
| 89 |
{{ T "security" }} → bezpieczeństwo |
| 90 |
``` |
| 91 |
|
| 92 |
## Translations with pluralization |
| 93 |
|
| 94 |
Let's say your multilingual project supports two languages, English and Polish. Create a translation table for each language in the `i18n` directory. |
| 95 |
|
| 96 |
```tree |
| 97 |
i18n/ |
| 98 |
├── en.toml |
| 99 |
└── pl.toml |
| 100 |
``` |
| 101 |
|
| 102 |
The Unicode [CLDR Plural Rules chart][CLDR] describes the pluralization categories for each language. |
| 103 |
|
| 104 |
The English translation table: |
| 105 |
|
| 106 |
{{< code-toggle file=i18n/en >}} |
| 107 |
[day] |
| 108 |
one = 'day' |
| 109 |
other = 'days' |
| 110 |
|
| 111 |
[day_with_count] |
| 112 |
one = '{{ . }} day' |
| 113 |
other = '{{ . }} days' |
| 114 |
{{< /code-toggle >}} |
| 115 |
|
| 116 |
The Polish translation table: |
| 117 |
|
| 118 |
{{< code-toggle file=i18n/pl >}} |
| 119 |
[day] |
| 120 |
one = 'miesiąc' |
| 121 |
few = 'miesiące' |
| 122 |
many = 'miesięcy' |
| 123 |
other = 'miesiąca' |
| 124 |
|
| 125 |
[day_with_count] |
| 126 |
one = '{{ . }} miesiąc' |
| 127 |
few = '{{ . }} miesiące' |
| 128 |
many = '{{ . }} miesięcy' |
| 129 |
other = '{{ . }} miesiąca' |
| 130 |
{{< /code-toggle >}} |
| 131 |
|
| 132 |
> [!NOTE] |
| 133 |
> The examples below use the `T` alias for brevity. |
| 134 |
|
| 135 |
When viewing the English language site: |
| 136 |
|
| 137 |
```go-html-template |
| 138 |
{{ T "day" 0 }} → days |
| 139 |
{{ T "day" 1 }} → day |
| 140 |
{{ T "day" 2 }} → days |
| 141 |
{{ T "day" 5 }} → days |
| 142 |
|
| 143 |
{{ T "day_with_count" 0 }} → 0 days |
| 144 |
{{ T "day_with_count" 1 }} → 1 day |
| 145 |
{{ T "day_with_count" 2 }} → 2 days |
| 146 |
{{ T "day_with_count" 5 }} → 5 days |
| 147 |
```` |
| 148 |
|
| 149 |
When viewing the Polish language site: |
| 150 |
|
| 151 |
```go-html-template |
| 152 |
{{ T "day" 0 }} → miesięcy |
| 153 |
{{ T "day" 1 }} → miesiąc |
| 154 |
{{ T "day" 2 }} → miesiące |
| 155 |
{{ T "day" 5 }} → miesięcy |
| 156 |
|
| 157 |
{{ T "day_with_count" 0 }} → 0 miesięcy |
| 158 |
{{ T "day_with_count" 1 }} → 1 miesiąc |
| 159 |
{{ T "day_with_count" 2 }} → 2 miesiące |
| 160 |
{{ T "day_with_count" 5 }} → 5 miesięcy |
| 161 |
``` |
| 162 |
|
| 163 |
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. |
| 164 |
|
| 165 |
Translation table: |
| 166 |
|
| 167 |
{{< code-toggle file=i18n/en >}} |
| 168 |
[age] |
| 169 |
one = '{{ .name }} is {{ .count }} year old.' |
| 170 |
other = '{{ .name }} is {{ .count }} years old.' |
| 171 |
{{< /code-toggle >}} |
| 172 |
|
| 173 |
Template code: |
| 174 |
|
| 175 |
```go-html-template |
| 176 |
{{ T "age" (dict "name" "Will" "count" 1) }} → Will is 1 year old. |
| 177 |
{{ T "age" (dict "name" "John" "count" 3) }} → John is 3 years old. |
| 178 |
``` |
| 179 |
|
| 180 |
> [!NOTE] |
| 181 |
> Translation tables may contain both simple translations and translations with pluralization. |
| 182 |
|
| 183 |
## Reserved keys |
| 184 |
|
| 185 |
Hugo uses the [`nicksnyder/go-i18n`][] package to look up values in translation tables. This package reserves the following keys for internal use: |
| 186 |
|
| 187 |
`id` |
| 188 |
: (`string`) Uniquely identifies the message. |
| 189 |
|
| 190 |
`description` |
| 191 |
: (`string`) Describes the message to give additional context to translators that may be relevant for translation. |
| 192 |
|
| 193 |
`hash` |
| 194 |
: (`string`) Uniquely identifies the content of the message that this message was translated from. |
| 195 |
|
| 196 |
`leftdelim` |
| 197 |
: (`string`) The left Go template delimiter. |
| 198 |
|
| 199 |
`rightdelim` |
| 200 |
: (`string`) The right Go template delimiter. |
| 201 |
|
| 202 |
`zero` |
| 203 |
: (`string`) The content of the message for the [CLDR][] plural form "zero". |
| 204 |
|
| 205 |
`one` |
| 206 |
: (`string`) The content of the message for the [CLDR][] plural form "one". |
| 207 |
|
| 208 |
`two` |
| 209 |
: (`string`) The content of the message for the [CLDR][] plural form "two". |
| 210 |
|
| 211 |
`few` |
| 212 |
: (`string`) The content of the message for the [CLDR][] plural form "few". |
| 213 |
|
| 214 |
`many` |
| 215 |
: (`string`) The content of the message for the [CLDR][] plural form "many". |
| 216 |
|
| 217 |
`other` |
| 218 |
: (`string`) The content of the message for the [CLDR][] plural form "other". |
| 219 |
|
| 220 |
If you need to provide a translation for one of the reserved keys, you can prepend the word with an underscore. For example: |
| 221 |
|
| 222 |
{{< code-toggle file=i18n/es >}} |
| 223 |
_description = 'descripción' |
| 224 |
_few = 'pocos' |
| 225 |
_many = 'muchos' |
| 226 |
_one = 'uno' |
| 227 |
_other = 'otro' |
| 228 |
_two = 'dos' |
| 229 |
_zero = 'cero' |
| 230 |
{{< /code-toggle >}} |
| 231 |
|
| 232 |
Then in your templates: |
| 233 |
|
| 234 |
```go-html-template |
| 235 |
{{ T "_description" }} → descripción |
| 236 |
{{ T "_few" }} → pocos |
| 237 |
{{ T "_many" }} → muchos |
| 238 |
{{ T "_one" }} → uno |
| 239 |
{{ T "_two" }} → dos |
| 240 |
{{ T "_zero" }} → cero |
| 241 |
{{ T "_other" }} → otro |
| 242 |
``` |
| 243 |
|
| 244 |
[CLDR]: https://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html |
| 245 |
[RFC 5646 § 2.2.7]: https://datatracker.ietf.org/doc/html/rfc5646#section-2.2.7 |
| 246 |
[`defaultContentLanguage`]: /configuration/all/#defaultcontentlanguage |
| 247 |
[`enableMissingTranslationPlaceholders`]: /configuration/all/#enablemissingtranslationplaceholders |
| 248 |
[`locale`]: /configuration/all/#locale |
| 249 |
[`nicksnyder/go-i18n`]: https://github.com/nicksnyder/go-i18n |
| 250 |
[`printI18nWarnings`]: /configuration/all/#printi18nwarnings |
| 251 |
[key]: /configuration/languages/#language-keys |