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 content 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 ### Create language-specific menu entries
230
231 #### Method 1 -- Use a single configuration file
232
233 For a simple menu with a small number of entries, use a single configuration file. For example:
234
235 {{< code-toggle file=hugo >}}
236 [languages.de]
237 label = 'Deutsch'
238 locale = 'de-DE'
239 weight = 1
240
241 [[languages.de.menus.main]]
242 name = 'Produkte'
243 pageRef = '/products'
244 weight = 10
245
246 [[languages.de.menus.main]]
247 name = 'Leistungen'
248 pageRef = '/services'
249 weight = 20
250
251 [languages.en]
252 label = 'English'
253 locale = 'en-US'
254 weight = 2
255
256 [[languages.en.menus.main]]
257 name = 'Products'
258 pageRef = '/products'
259 weight = 10
260
261 [[languages.en.menus.main]]
262 name = 'Services'
263 pageRef = '/services'
264 weight = 20
265 {{< /code-toggle >}}
266
267 #### Method 2 -- Use a configuration directory
268
269 With a more complex menu structure, create a [configuration directory][] and split the menu entries into multiple files, one file per language. For example:
270
271 ```tree
272 config/
273 └── _default/
274 ├── menus.de.toml
275 ├── menus.en.toml
276 └── hugo.toml
277 ```
278
279 {{< code-toggle file=config/_default/menus.de >}}
280 [[main]]
281 name = 'Produkte'
282 pageRef = '/products'
283 weight = 10
284 [[main]]
285 name = 'Leistungen'
286 pageRef = '/services'
287 weight = 20
288 {{< /code-toggle >}}
289
290 {{< code-toggle file=config/_default/menus.en >}}
291 [[main]]
292 name = 'Products'
293 pageRef = '/products'
294 weight = 10
295 [[main]]
296 name = 'Services'
297 pageRef = '/services'
298 weight = 20
299 {{< /code-toggle >}}
300
301 ### Use translation tables
302
303 When rendering the text that appears in menu each entry, the [example menu template][] does this:
304
305 ```go-html-template
306 {{ or (T .Identifier) .Name | safeHTML }}
307 ```
308
309 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`.
310
311 The `identifier` depends on how you define menu entries:
312
313 - If you define the menu entry [automatically][] using the section pages menu, the `identifier` is the page's `.Section`.
314 - If you define the menu entry in your [project configuration][] or in [front matter][], set the `identifier` property to the desired value.
315
316 For example, if you define menu entries in project configuration:
317
318 {{< code-toggle file=hugo >}}
319 [[menus.main]]
320 identifier = 'products'
321 name = 'Products'
322 pageRef = '/products'
323 weight = 10
324 [[menus.main]]
325 identifier = 'services'
326 name = 'Services'
327 pageRef = '/services'
328 weight = 20
329 {{< / code-toggle >}}
330
331 Create corresponding entries in the translation tables:
332
333 {{< code-toggle file=i18n/de >}}
334 products = 'Produkte'
335 services = 'Leistungen'
336 {{< / code-toggle >}}
337
338 ## Missing translations
339
340 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.
341
342 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.
343
344 > [!NOTE]
345 > Hugo will generate your website with these missing translation placeholders. It might not be suitable for production environments.
346
347 For merging of content from other languages (i.e. missing content translations), see [lang.Merge].
348
349 To track down missing translation strings, run Hugo with the `--printI18nWarnings` flag:
350
351 ```sh
352 hugo build --printI18nWarnings | grep i18n
353 i18n|MISSING_TRANSLATION|en|wordCount
354 ```
355
356 ## Multilingual themes support
357
358 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:
359
360 - Come from the built-in `.Permalink` or `.RelPermalink`
361 - Be constructed with the [`urls.RelLangURL`][] or [`urls.AbsLangURL`][] function, or be prefixed with `{{ .LanguagePrefix }}`
362
363 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).
364
365 ## Generate multilingual content with `hugo new content`
366
367 If you organize content with translations in the same directory:
368
369 ```sh
370 hugo new content post/test.en.md
371 hugo new content post/test.de.md
372 ```
373
374 If you organize content with translations in different directories:
375
376 ```sh
377 hugo new content content/en/post/test.md
378 hugo new content content/de/post/test.md
379 ```
380
381 [`enableMissingTranslationPlaceholders`]: /configuration/all/#enablemissingtranslationplaceholders
382 [`lang.Translate`]: /functions/lang/translate/
383 [`slug`]: /content-management/urls/#slug
384 [`time.Format`]: /functions/time/format/
385 [`url`]: /content-management/urls/#url
386 [`urls.AbsLangURL`]: /functions/urls/abslangurl/
387 [`urls.RelLangURL`]: /functions/urls/rellangurl/
388 [automatically]: /content-management/menus/#define-automatically
389 [configuration directory]: /configuration/introduction/#configuration-directory
390 [configure languages]: /configuration/languages/
391 [example menu template]: /templates/menu/#example
392 [front matter]: /content-management/menus/#define-in-front-matter
393 [lang.FormatAccounting]: /functions/lang/formataccounting/
394 [lang.FormatCurrency]: /functions/lang/formatcurrency/
395 [lang.FormatNumberCustom]: /functions/lang/formatnumbercustom/
396 [lang.FormatNumber]: /functions/lang/formatnumber/
397 [lang.FormatPercent]: /functions/lang/formatpercent/
398 [lang.Merge]: /functions/lang/merge/
399 [project configuration]: /content-management/menus/#define-in-project-configuration