Debian Codegraph @

docs/content/en/content-management/multilingual.md

raw
1 ---
2 title: Multilingual mode
3 linkTitle: Multilingual
4 description: Localize your project for each language and region, including translations, images, dates, currencies, numbers, percentages, and collation sequence. Hugo's multilingual framework supports single-host and multihost configurations.
5 categories: []
6 keywords: []
7 aliases: [/content/multilingual/,/tutorials/create-a-multilingual-site/]
8 ---
9
10 ## Configuration
11
12 See [configure languages][].
13
14 ## Translate your content
15
16 There are two ways to manage your content translations. Both ensure each page is assigned a language and is linked to its counterpart translations.
17
18 ### Translation by file name
19
20 Considering the following example:
21
22 1. `/content/about.en.md`
23 1. `/content/about.fr.md`
24
25 The first file is assigned the English language and is linked to the second.
26 The second file is assigned the French language and is linked to the first.
27
28 Their language is assigned according to the language code added as a suffix to the file name.
29
30 By having the same path and base file name, the content pieces are linked together as translated pages.
31
32 > [!NOTE]
33 > The language code in a file name must be lowercase. For example, use `about.en-us.md` instead of `about.en-US.md`.
34
35 > [!NOTE]
36 > If a file has no language code, it will be assigned the default language.
37
38 ### Translation by directory
39
40 This system uses different content directories for each of the languages. Each language's `content` directory is set using the `contentDir` parameter.
41
42 {{< code-toggle file=hugo >}}
43 [languages.en]
44 contentDir = 'content/english'
45 label = "English"
46 weight = 10
47
48 [languages.fr]
49 contentDir = 'content/french'
50 label = "Français"
51 weight = 20
52 {{< /code-toggle >}}
53
54 The value of `contentDir` can be any valid path -- even absolute path references. The only restriction is that the content directories cannot overlap.
55
56 Considering the following example in conjunction with the configuration above:
57
58 1. `/content/english/about.md`
59 1. `/content/french/about.md`
60
61 The first file is assigned the English language and is linked to the second.
62 The second file is assigned the French language and is linked to the first.
63
64 Their language is assigned according to the `content` directory they are placed in.
65
66 By having the same path and basename (relative to their language `content` directory), the content pieces are linked together as translated pages.
67
68 ### Bypassing default linking
69
70 Any pages sharing the same `translationKey` set in front matter will be linked as translated pages regardless of basename or location.
71
72 Considering the following example:
73
74 1. `/content/about-us.en.md`
75 1. `/content/om.nn.md`
76 1. `/content/presentation/a-propos.fr.md`
77
78 {{< code-toggle file=hugo >}}
79 translationKey: "about"
80 {{< /code-toggle >}}
81
82 By setting the `translationKey` front matter parameter to `about` in all three pages, they will be linked as translated pages.
83
84 ### Localizing permalinks
85
86 Because paths and file names are used to handle linking, all translated pages will share the same URL (apart from the language subdirectory).
87
88 To localize URLs:
89
90 - For a regular page, set either [`slug`][] or [`url`][] in front matter
91 - For a section page, set [`url`][] in front matter
92
93 For example, a French translation can have its own localized slug.
94
95 {{< code-toggle file=content/about.fr.md fm=true >}}
96 title: A Propos
97 slug: "a-propos"
98 {{< /code-toggle >}}
99
100 At render, Hugo will build both `/about/` and `/fr/a-propos/` without affecting the translation link.
101
102 ### Page bundles
103
104 To avoid the burden of having to duplicate files, each Page Bundle inherits the resources of its linked translated pages' bundles except for the content files (Markdown files, HTML files etc.).
105
106 Therefore, from within a template, the page will have access to the files from all linked pages' bundles.
107
108 If, across the linked bundles, two or more files share the same basename, only one will be included and chosen as follows:
109
110 - File from current language bundle, if present.
111 - First file found across bundles by order of language `Weight`.
112
113 > [!NOTE]
114 > Page Bundle resources follow the same language assignment logic as content files, both by file name (`image.jpg`, `image.fr.jpg`) and by directory (`english/about/header.jpg`, `french/about/header.jpg`).
115
116 ## Translation of strings
117
118 See the [`lang.Translate`][] function.
119
120 ## Localization
121
122 The following localization examples assume your project's primary language is English, with translations to French and German.
123
124 {{< code-toggle file=hugo >}}
125 defaultContentLanguage = 'en'
126
127 [languages]
128 [languages.en]
129 contentDir = 'content/en'
130 label = 'English'
131 weight = 1
132 [languages.fr]
133 contentDir = 'content/fr'
134 label = 'Français'
135 weight = 2
136 [languages.de]
137 contentDir = 'content/de'
138 label = 'Deutsch'
139 weight = 3
140
141 {{< /code-toggle >}}
142
143 ### Dates
144
145 With this front matter:
146
147 {{< code-toggle file=hugo >}}
148 date = 2021-11-03T12:34:56+01:00
149 {{< /code-toggle >}}
150
151 And this template code:
152
153 ```go-html-template
154 {{ .Date | time.Format ":date_full" }}
155 ```
156
157 The rendered page displays:
158
159 Language|Value
160 :--|:--
161 English|Wednesday, November 3, 2021
162 Français|mercredi 3 novembre 2021
163 Deutsch|Mittwoch, 3. November 2021
164
165 See [`time.Format`][] for details.
166
167 ### Currency
168
169 With this template code:
170
171 ```go-html-template
172 {{ 512.5032 | lang.FormatCurrency 2 "USD" }}
173 ```
174
175 The rendered page displays:
176
177 Language|Value
178 :--|:--
179 English|$512.50
180 Français|512,50 $US
181 Deutsch|512,50 $
182
183 See [lang.FormatCurrency][] and [lang.FormatAccounting][] for details.
184
185 ### Numbers
186
187 With this template code:
188
189 ```go-html-template
190 {{ 512.5032 | lang.FormatNumber 2 }}
191 ```
192
193 The rendered page displays:
194
195 Language|Value
196 :--|:--
197 English|512.50
198 Français|512,50
199 Deutsch|512,50
200
201 See [lang.FormatNumber][] and [lang.FormatNumberCustom][] for details.
202
203 ### Percentages
204
205 With this template code:
206
207 ```go-html-template
208 {{ 512.5032 | lang.FormatPercent 2 }}
209 ```
210
211 The rendered page displays:
212
213 Language|Value
214 :--|:--
215 English|512.50%
216 Français|512,50 %
217 Deutsch|512,50 %
218
219 See [lang.FormatPercent][] for details.
220
221 ## Menus
222
223 Localization of menu entries depends on how you define them:
224
225 - When you define menu entries [automatically][] using the section pages menu, you must use translation tables to localize each entry.
226 - When you define menu entries in [front matter][], they are already localized based on the front matter itself. If the front matter values are insufficient, use translation tables to localize each entry.
227 - When you define menu entries in your [project configuration][], you must create language-specific menu entries under each language key. If the names of the menu entries are insufficient, use translation tables to localize each entry.
228
229 ### Language-specific menus
230
231 Define language-specific menu entries using a single configuration file, or using the configuration directory structure.
232
233 #### Single configuration file
234
235 For a simple menu with a small number of entries, use a single configuration file. For example:
236
237 {{< code-toggle file=hugo >}}
238 [languages.de]
239 label = 'Deutsch'
240 locale = 'de-DE'
241 weight = 1
242
243 [[languages.de.menus.main]]
244 name = 'Produkte'
245 pageRef = '/products'
246 weight = 10
247
248 [[languages.de.menus.main]]
249 name = 'Leistungen'
250 pageRef = '/services'
251 weight = 20
252
253 [languages.en]
254 label = 'English'
255 locale = 'en-US'
256 weight = 2
257
258 [[languages.en.menus.main]]
259 name = 'Products'
260 pageRef = '/products'
261 weight = 10
262
263 [[languages.en.menus.main]]
264 name = 'Services'
265 pageRef = '/services'
266 weight = 20
267 {{< /code-toggle >}}
268
269 #### Configuration directory
270
271 With a more complex menu structure, create a [configuration directory][] and split the menu entries into multiple files, one file per language. For example:
272
273 ```tree
274 config/
275 └── _default/
276 ├── menus.de.toml
277 ├── menus.en.toml
278 └── hugo.toml
279 ```
280
281 {{< code-toggle file=config/_default/menus.de >}}
282 [[main]]
283 name = 'Produkte'
284 pageRef = '/products'
285 weight = 10
286 [[main]]
287 name = 'Leistungen'
288 pageRef = '/services'
289 weight = 20
290 {{< /code-toggle >}}
291
292 {{< code-toggle file=config/_default/menus.en >}}
293 [[main]]
294 name = 'Products'
295 pageRef = '/products'
296 weight = 10
297 [[main]]
298 name = 'Services'
299 pageRef = '/services'
300 weight = 20
301 {{< /code-toggle >}}
302
303 ### Use translation tables
304
305 When rendering the text that appears in menu each entry, the [example menu template][] does this:
306
307 ```go-html-template
308 {{ or (T .Identifier) .Name | safeHTML }}
309 ```
310
311 It queries the translation table for the current language using the menu entry's `identifier` and returns the translated string. If the translation table does not exist, or if the `identifier` key is not present in the translation table, it falls back to `name`.
312
313 The `identifier` depends on how you define menu entries:
314
315 - If you define the menu entry [automatically][] using the section pages menu, the `identifier` is the page's `.Section`.
316 - If you define the menu entry in your [project configuration][] or in [front matter][], set the `identifier` property to the desired value.
317
318 For example, if you define menu entries in project configuration:
319
320 {{< code-toggle file=hugo >}}
321 [[menus.main]]
322 identifier = 'products'
323 name = 'Products'
324 pageRef = '/products'
325 weight = 10
326 [[menus.main]]
327 identifier = 'services'
328 name = 'Services'
329 pageRef = '/services'
330 weight = 20
331 {{< / code-toggle >}}
332
333 Create corresponding entries in the translation tables:
334
335 {{< code-toggle file=i18n/de >}}
336 products = 'Produkte'
337 services = 'Leistungen'
338 {{< / code-toggle >}}
339
340 ## Missing translations
341
342 If a string does not have a translation for the current language, Hugo will use the value from the default language. If no default value is set, an empty string will be shown.
343
344 While translating a Hugo website, it can be helpful to have a visual indicator of missing translations. The [`enableMissingTranslationPlaceholders`][] configuration setting will flag all untranslated strings with the placeholder `[i18n] identifier`, where `identifier` is the id of the missing translation.
345
346 > [!NOTE]
347 > Hugo will generate your website with these missing translation placeholders. It might not be suitable for production environments.
348
349 For merging of content from other languages (i.e. missing content translations), see [lang.Merge].
350
351 To track down missing translation strings, run Hugo with the `--printI18nWarnings` flag:
352
353 ```sh
354 hugo build --printI18nWarnings | grep i18n
355 i18n|MISSING_TRANSLATION|en|wordCount
356 ```
357
358 ## Multilingual themes support
359
360 To support Multilingual mode in your themes, some considerations must be taken for the URLs in the templates. If there is more than one language, URLs must meet the following criteria:
361
362 - Come from the built-in `.Permalink` or `.RelPermalink`
363 - Be constructed with the [`urls.RelLangURL`][] or [`urls.AbsLangURL`][] function, or be prefixed with `{{ .LanguagePrefix }}`
364
365 If there is more than one language defined, the `LanguagePrefix` method will return `/en` (or whatever the current language is). If not enabled, it will be an empty string (and is therefore harmless for single-language Hugo websites).
366
367 ## Generate multilingual content
368
369 If you organize content with translations in the same directory:
370
371 ```sh
372 hugo new content post/test.en.md
373 hugo new content post/test.de.md
374 ```
375
376 If you organize content with translations in different directories:
377
378 ```sh
379 hugo new content content/en/post/test.md
380 hugo new content content/de/post/test.md
381 ```
382
383 [`enableMissingTranslationPlaceholders`]: /configuration/all/#enablemissingtranslationplaceholders
384 [`lang.Translate`]: /functions/lang/translate/
385 [`slug`]: /content-management/urls/#slug
386 [`time.Format`]: /functions/time/format/
387 [`url`]: /content-management/urls/#url
388 [`urls.AbsLangURL`]: /functions/urls/abslangurl/
389 [`urls.RelLangURL`]: /functions/urls/rellangurl/
390 [automatically]: /content-management/menus/#define-automatically
391 [configuration directory]: /configuration/introduction/#configuration-directory
392 [configure languages]: /configuration/languages/
393 [example menu template]: /templates/menu/#example
394 [front matter]: /content-management/menus/#define-in-front-matter
395 [lang.FormatAccounting]: /functions/lang/formataccounting/
396 [lang.FormatCurrency]: /functions/lang/formatcurrency/
397 [lang.FormatNumberCustom]: /functions/lang/formatnumbercustom/
398 [lang.FormatNumber]: /functions/lang/formatnumber/
399 [lang.FormatPercent]: /functions/lang/formatpercent/
400 [lang.Merge]: /functions/lang/merge/
401 [project configuration]: /content-management/menus/#define-in-project-configuration