github.com/jbramsden/hugo@v0.47.1/docs/content/en/content-management/multilingual.md (about) 1 --- 2 title: Multilingual Mode 3 linktitle: Multilingual and i18n 4 description: Hugo supports the creation of websites with multiple languages side by side. 5 date: 2017-01-10 6 publishdate: 2017-01-10 7 lastmod: 2017-01-10 8 categories: [content management] 9 keywords: [multilingual,i18n, internationalization] 10 menu: 11 docs: 12 parent: "content-management" 13 weight: 150 14 weight: 150 #rem 15 draft: false 16 aliases: [/content/multilingual/,/tutorials/create-a-multilingual-site/] 17 toc: true 18 --- 19 20 You should define the available languages in a `languages` section in your site configuration. 21 22 > Also See [Hugo Multilingual Part 1: Content translation](https://regisphilibert.com/blog/2018/08/hugo-multilingual-part-1-managing-content-translation/) 23 24 ## Configure Languages 25 26 The following is an example of a site configuration for a multilingual Hugo project: 27 28 {{< code-toggle file="config" >}} 29 DefaultContentLanguage = "en" 30 copyright = "Everything is mine" 31 32 [params.navigation] 33 help = "Help" 34 35 [languages] 36 [languages.en] 37 title = "My blog" 38 weight = 1 39 [languages.en.params] 40 linkedin = "https://linkedin.com/whoever" 41 42 [languages.fr] 43 title = "Mon blogue" 44 weight = 2 45 [languages.fr.params] 46 linkedin = "https://linkedin.com/fr/whoever" 47 [languages.fr.params.navigation] 48 help = "Aide" 49 {{< /code-toggle >}} 50 51 Anything not defined in a `[languages]` block will fall back to the global 52 value for that key (e.g., `copyright` for the English [`en`] language). 53 54 With the configuration above, all content, sitemap, RSS feeds, paginations, 55 and taxonomy pages will be rendered below `/` in English (your default content language) and then below `/fr` in French. 56 57 When working with front matter `Params` in [single page templates][singles], omit the `params` in the key for the translation. 58 59 `defaultContentLanguage` sets the project's default language. If not set, the default language will be `en`. 60 61 If the default language needs to be rendererd below its own language code (`/en`) like the others, set `defaultContentLanguageInSubdir: true`. 62 63 Only the obvious non-global options can be overridden per language. Examples of global options are `baseURL`, `buildDrafts`, etc. 64 65 ### Disable a Language 66 67 You can disable one or more languages. This can be useful when working on a new translation. 68 69 ```toml 70 disableLanguages = ["fr", "jp"] 71 ``` 72 73 Note that you cannot disable the default content language. 74 75 We kept this as a standalone setting to make it easier to set via [OS environment](/getting-started/configuration/#configure-with-environment-variables): 76 77 ```bash 78 HUGO_DISABLELANGUAGES="fr jp" hugo 79 ``` 80 If you have already a list of disabled languages in `config.toml`, you can enable them in development like this: 81 82 ```bash 83 HUGO_DISABLELANGUAGES=" " hugo server 84 ``` 85 86 87 ### Configure Multilingual Multihost 88 89 From **Hugo 0.31** we support multiple languages in a multihost configuration. See [this issue](https://github.com/gohugoio/hugo/issues/4027) for details. 90 91 This means that you can now configure a `baseURL` per `language`: 92 93 94 > If a `baseURL` is set on the `language` level, then all languages must have one and they must all be different. 95 96 Example: 97 98 {{< code-toggle file="config" >}} 99 [languages] 100 [languages.fr] 101 baseURL = "https://example.fr" 102 languageName = "Français" 103 weight = 1 104 title = "En Français" 105 106 [languages.en] 107 baseURL = "https://example.com" 108 languageName = "English" 109 weight = 2 110 title = "In English" 111 {{</ code-toggle >}} 112 113 With the above, the two sites will be generated into `public` with their own root: 114 115 ```bash 116 public 117 ├── en 118 └── no 119 ``` 120 121 **All URLs (i.e `.Permalink` etc.) will be generated from that root. So the English home page above will have its `.Permalink` set to `https://example.com/`.** 122 123 When you run `hugo server` we will start multiple HTTP servers. You will typlically see something like this in the console: 124 125 ```bash 126 Web Server is available at 127.0.0.1:1313 (bind address 127.0.0.1) 127 Web Server is available at 127.0.0.1:1314 (bind address 127.0.0.1) 128 Press Ctrl+C to stop 129 ``` 130 131 Live reload and `--navigateToChanged` between the servers work as expected. 132 133 ### Taxonomies and Blackfriday 134 135 Taxonomies and [Blackfriday configuration][config] can also be set per language: 136 137 138 {{< code-toggle file="config" >}} 139 [Taxonomies] 140 tag = "tags" 141 142 [blackfriday] 143 angledQuotes = true 144 hrefTargetBlank = true 145 146 [languages] 147 [languages.en] 148 weight = 1 149 title = "English" 150 [languages.en.blackfriday] 151 angledQuotes = false 152 153 [languages.fr] 154 weight = 2 155 title = "Français" 156 [languages.fr.Taxonomies] 157 plaque = "plaques" 158 {{</ code-toggle >}} 159 160 ## Translate Your Content 161 162 There are two ways to manage your content translation, both ensures each page is assigned a language and linked to its translations. 163 164 ### Translation by filename 165 166 Considering the following example: 167 168 1. `/content/about.en.md` 169 2. `/content/about.fr.md` 170 171 The first file is assigned the english language and linked to the second. 172 The second file is assigned the french language and linked to the first. 173 174 Their language is __assigned__ according to the language code added as __suffix to the filename__. 175 176 By having the same **path and base filename**, the content pieces are __linked__ together as translated pages. 177 {{< note >}} 178 179 If a file is missing any language code, it will be assigned the default language. 180 181 {{</ note >}} 182 ### Translation by content directory 183 184 This system uses different content directories for each of the languages. Each language's content directory is set using the `contentDir` param. 185 186 {{< code-toggle file="config" >}} 187 188 languages: 189 en: 190 weight: 10 191 languageName: "English" 192 contentDir: "content/english" 193 nn: 194 weight: 20 195 languageName: "Français" 196 contentDir: "content/french" 197 198 {{< /code-toggle >}} 199 200 The value of `contentDir` can be any valid path, even absolute path references. The only restriction is that the content directories cannot overlap. 201 202 Considering the following example in conjunction with the configuration above: 203 204 1. `/content/english/about.md` 205 2. `/content/french/about.md` 206 207 The first file is assigned the english language and is linked to the second. 208 <br>The second file is assigned the french language and is linked to the first. 209 210 Their language is __assigned__ according to the content directory they are __placed__ in. 211 212 By having the same **path and basename** (relative to their language content directory), the content pieces are __linked__ together as translated pages. 213 214 ### Bypassing default linking. 215 216 Any pages sharing the same `translationKey` set in front matter will be linked as translated pages regardless of basename or location. 217 218 Considering the following example: 219 220 1. `/content/about-us.en.md` 221 2. `/content/om.nn.md` 222 3. `/content/presentation/a-propos.fr.md` 223 224 ```yaml 225 # set in all three pages 226 translationKey: "about" 227 ``` 228 229 By setting the `translationKey` front matter param to `about` in all three pages, they will be __linked__ as translated pages. 230 231 232 ### Localizing permalinks 233 234 Because paths and filenames are used to handle linking, all translated pages, except for the language part, will be sharing the same url. 235 236 To localize the URLs, the [`slug`]({{< ref "/content-management/organization/index.md#slug" >}}) or [`url`]({{< ref "/content-management/organization/index.md#url" >}}) front matter param can be set in any of the non-default language file. 237 238 For example, a french translation (`content/about.fr.md`) can have its own localized slug. 239 240 {{< code-toggle >}} 241 Title: A Propos 242 slug: "a-propos" 243 {{< /code-toggle >}} 244 245 246 At render, Hugo will build both `/about/` and `fr/a-propos/` while maintaning their translation linking. 247 {{% note %}} 248 If using `url`, remember to include the language part as well: `fr/compagnie/a-propos/`. 249 {{%/ note %}} 250 251 ### Page Bundles 252 253 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...). 254 255 Therefore, from within a template, the page will have access to the files from all linked pages' bundles. 256 257 If, across the linked bundles, two or more files share the same basenname, only one will be included and chosen as follows: 258 259 * File from current language Bundle, if present. 260 * First file found across bundles by order of language `Weight`. 261 262 {{% note %}} 263 264 Page Bundle's resources follow the same language assignement logic as content files, be it by filename (`image.jpg`, `image.fr.jpg`) or by directory (`english/about/header.jpg`, `french/about/header.jpg`). 265 266 {{%/ note %}} 267 268 ## Reference the Translated Content 269 270 To create a list of links to translated content, use a template similar to the following: 271 272 {{< code file="layouts/partials/i18nlist.html" >}} 273 {{ if .IsTranslated }} 274 <h4>{{ i18n "translations" }}</h4> 275 <ul> 276 {{ range .Translations }} 277 <li> 278 <a href="{{ .Permalink }}">{{ .Lang }}: {{ .Title }}{{ if .IsPage }} ({{ i18n "wordCount" . }}){{ end }}</a> 279 </li> 280 {{ end}} 281 </ul> 282 {{ end }} 283 {{< /code >}} 284 285 The above can be put in a `partial` (i.e., inside `layouts/partials/`) and included in any template, be it for a [single content page][contenttemplate] or the [homepage][]. It will not print anything if there are no translations for a given page. 286 287 The above also uses the [`i18n` function][i18func] described in the next section. 288 289 ### List All Available Languages 290 291 `.AllTranslations` on a `Page` can be used to list all translations, including itself. Called on the home page it can be used to build a language navigator: 292 293 294 {{< code file="layouts/partials/allLanguages.html" >}} 295 <ul> 296 {{ range $.Site.Home.AllTranslations }} 297 <li><a href="{{ .Permalink }}">{{ .Language.LanguageName }}</a></li> 298 {{ end }} 299 </ul> 300 {{< /code >}} 301 302 ## Translation of Strings 303 304 Hugo uses [go-i18n][] to support string translations. [See the project's source repository][go-i18n-source] to find tools that will help you manage your translation workflows. 305 306 Translations are collected from the `themes/<THEME>/i18n/` folder (built into the theme), as well as translations present in `i18n/` at the root of your project. In the `i18n`, the translations will be merged and take precedence over what is in the theme folder. Language files should be named according to [RFC 5646][] with names such as `en-US.toml`, `fr.toml`, etc. 307 308 {{% note %}} 309 From **Hugo 0.31** you no longer need to use a valid language code. It _can be_ anything. 310 311 See https://github.com/gohugoio/hugo/issues/3564 312 313 {{% /note %}} 314 315 From within your templates, use the `i18n` function like this: 316 317 ``` 318 {{ i18n "home" }} 319 ``` 320 321 This uses a definition like this one in `i18n/en-US.toml`: 322 323 ``` 324 [home] 325 other = "Home" 326 ``` 327 328 Often you will want to use to the page variables in the translations strings. To do that, pass on the "." context when calling `i18n`: 329 330 ``` 331 {{ i18n "wordCount" . }} 332 ``` 333 334 This uses a definition like this one in `i18n/en-US.toml`: 335 336 ``` 337 [wordCount] 338 other = "This article has {{ .WordCount }} words." 339 ``` 340 An example of singular and plural form: 341 342 ``` 343 [readingTime] 344 one = "One minute read" 345 other = "{{.Count}} minutes read" 346 ``` 347 And then in the template: 348 349 ``` 350 {{ i18n "readingTime" .ReadingTime }} 351 ``` 352 To track down missing translation strings, run Hugo with the `--i18n-warnings` flag: 353 354 ``` 355 hugo --i18n-warnings | grep i18n 356 i18n|MISSING_TRANSLATION|en|wordCount 357 ``` 358 359 ## Customize Dates 360 361 At the time of this writing, Go does not yet have support for internationalized locales, but if you do some work, you can simulate it. For example, if you want to use French month names, you can add a data file like ``data/mois.yaml`` with this content: 362 363 ~~~yaml 364 1: "janvier" 365 2: "février" 366 3: "mars" 367 4: "avril" 368 5: "mai" 369 6: "juin" 370 7: "juillet" 371 8: "août" 372 9: "septembre" 373 10: "octobre" 374 11: "novembre" 375 12: "décembre" 376 ~~~ 377 378 ... then index the non-English date names in your templates like so: 379 380 ~~~html 381 <time class="post-date" datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" | safeHTML }}"> 382 Article publié le {{ .Date.Day }} {{ index $.Site.Data.mois (printf "%d" .Date.Month) }} {{ .Date.Year }} (dernière modification le {{ .Lastmod.Day }} {{ index $.Site.Data.mois (printf "%d" .Lastmod.Month) }} {{ .Lastmod.Year }}) 383 </time> 384 ~~~ 385 386 This technique extracts the day, month and year by specifying ``.Date.Day``, ``.Date.Month``, and ``.Date.Year``, and uses the month number as a key, when indexing the month name data file. 387 388 ## Menus 389 390 You can define your menus for each language independently. The [creation of a menu][menus] works analogous to earlier versions of Hugo, except that they have to be defined in their language-specific block in the configuration file: 391 392 ``` 393 defaultContentLanguage = "en" 394 395 [languages.en] 396 weight = 0 397 languageName = "English" 398 399 [[languages.en.menu.main]] 400 url = "/" 401 name = "Home" 402 weight = 0 403 404 405 [languages.de] 406 weight = 10 407 languageName = "Deutsch" 408 409 [[languages.de.menu.main]] 410 url = "/" 411 name = "Startseite" 412 weight = 0 413 ``` 414 415 The rendering of the main navigation works as usual. `.Site.Menus` will just contain the menu of the current language. Pay attention to the generation of the menu links. `absLangURL` takes care that you link to the correct locale of your website. Otherwise, both menu entries would link to the English version as the default content language that resides in the root directory. 416 417 ``` 418 <ul> 419 {{- $currentPage := . -}} 420 {{ range .Site.Menus.main -}} 421 <li class="{{ if $currentPage.IsMenuCurrent "main" . }}active{{ end }}"> 422 <a href="{{ .URL | absLangURL }}">{{ .Name }}</a> 423 </li> 424 {{- end }} 425 </ul> 426 427 ``` 428 429 ## Missing Translations 430 431 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. 432 433 While translating a Hugo website, it can be handy to have a visual indicator of missing translations. The [`enableMissingTranslationPlaceholders` configuration option][config] will flag all untranslated strings with the placeholder `[i18n] identifier`, where `identifier` is the id of the missing translation. 434 435 {{% note %}} 436 Hugo will generate your website with these missing translation placeholders. It might not be suited for production environments. 437 {{% /note %}} 438 439 For merging of content from other languages (i.e. missing content translations), see [lang.Merge](/functions/lang.merge/). 440 441 ## Multilingual Themes support 442 443 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: 444 445 * Come from the built-in `.Permalink` or `.RelPermalink` 446 * Be constructed with 447 * The [`relLangURL` template function][rellangurl] or the [`absLangURL` template function][abslangurl] **OR** 448 * Prefixed with `{{ .LanguagePrefix }}` 449 450 If there is more than one language defined, the `LanguagePrefix` variable will equal `/en` (or whatever your `CurrentLanguage` is). If not enabled, it will be an empty string and is therefore harmless for single-language Hugo websites. 451 452 [abslangurl]: /functions/abslangurl 453 [config]: /getting-started/configuration/ 454 [contenttemplate]: /templates/single-page-templates/ 455 [go-i18n-source]: https://github.com/nicksnyder/go-i18n 456 [go-i18n]: https://github.com/nicksnyder/go-i18n 457 [homepage]: /templates/homepage/ 458 [i18func]: /functions/i18n/ 459 [menus]: /content-management/menus/ 460 [rellangurl]: /functions/rellangurl 461 [RFC 5646]: https://tools.ietf.org/html/rfc5646 462 [singles]: /templates/single-page-templates/