github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/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/,/content-management/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  ## Configure Languages
    23  
    24  The following is an example of a site configuration for a multilingual Hugo project:
    25  
    26  {{< code-toggle file="config" >}}
    27  DefaultContentLanguage = "en"
    28  copyright = "Everything is mine"
    29  
    30  [params.navigation]
    31  help  = "Help"
    32  
    33  [languages]
    34  [languages.en]
    35  title = "My blog"
    36  weight = 1
    37  [languages.en.params]
    38  linkedin = "english-link"
    39  
    40  [languages.fr]
    41  copyright = "Tout est à moi"
    42  title = "Mon blog"
    43  weight = 2
    44  [languages.fr.params]
    45  linkedin = "lien-francais"
    46  [languages.fr.params.navigation]
    47  help  = "Aide"
    48  {{< /code-toggle >}}
    49  
    50  Anything not defined in a `[languages]` block will fall back to the global
    51  value for that key (e.g., `copyright` for the English [`en`] language).
    52  
    53  With the configuration above, all content, sitemap, RSS feeds, paginations,
    54  and taxonomy pages will be rendered below `/` in English (your default content language) and then below `/fr` in French.
    55  
    56  When working with front matter `Params` in [single page templates][singles], omit the `params` in the key for the translation.
    57  
    58  If you want all of the languages to be put below their respective language code, enable `defaultContentLanguageInSubdir: true`.
    59  
    60  Only the obvious non-global options can be overridden per language. Examples of global options are `baseURL`, `buildDrafts`, etc.
    61  
    62  ## Disable a Language
    63  
    64  You can disable one or more languages. This can be useful when working on a new translation.
    65  
    66  ```toml
    67  disableLanguages = ["fr", "jp"]
    68  ```
    69  
    70  Note that you cannot disable the default content language.
    71  
    72  We kept this as a standalone setting to make it easier to set via [OS environment](/getting-started/configuration/#configure-with-environment-variables):
    73  
    74  ```bash
    75  HUGO_DISABLELANGUAGES="fr jp" hugo
    76  ```
    77  If you have already a list of disabled languages in `config.toml`, you can enable them in development like this:
    78  
    79  ```bash
    80  HUGO_DISABLELANGUAGES=" " hugo server
    81  ```
    82  
    83  
    84  ## Configure Multilingual Multihost
    85  
    86  From **Hugo 0.31** we support multiple languages in a multihost configuration. See [this issue](https://github.com/gohugoio/hugo/issues/4027) for details.
    87  
    88  This means that you can now configure a `baseURL` per `language`:
    89  
    90  
    91  > If a `baseURL` is set on the `language` level, then all languages must have one and they must all be different.
    92  
    93  Example:
    94  
    95  {{< code-toggle file="config" >}}
    96  [languages]
    97  [languages.no]
    98  baseURL = "https://example.no"
    99  languageName = "Norsk"
   100  weight = 1
   101  title = "På norsk"
   102  
   103  [languages.en]
   104  baseURL = "https://example.com"
   105  languageName = "English"
   106  weight = 2
   107  title = "In English"
   108  {{</ code-toggle >}}
   109  
   110  With the above, the two sites will be generated into `public` with their own root:
   111  
   112  ```bash
   113  public
   114  ├── en
   115  └── no
   116  ```
   117  
   118  **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/`.**
   119  
   120  When you run `hugo server` we will start multiple HTTP servers. You will typlically see something like this in the console:
   121  
   122  ```bash
   123  Web Server is available at 127.0.0.1:1313 (bind address 127.0.0.1)
   124  Web Server is available at 127.0.0.1:1314 (bind address 127.0.0.1)
   125  Press Ctrl+C to stop
   126  ```
   127  
   128  Live reload and `--navigateToChanged` between the servers work as expected.
   129  
   130  ## Taxonomies and Blackfriday
   131  
   132  Taxonomies and [Blackfriday configuration][config] can also be set per language:
   133  
   134  
   135  {{< code-toggle file="config" >}}
   136  [Taxonomies]
   137  tag = "tags"
   138  
   139  [blackfriday]
   140  angledQuotes = true
   141  hrefTargetBlank = true
   142  
   143  [languages]
   144  [languages.en]
   145  weight = 1
   146  title = "English"
   147  [languages.en.blackfriday]
   148  angledQuotes = false
   149  
   150  [languages.fr]
   151  weight = 2
   152  title = "Français"
   153  [languages.fr.Taxonomies]
   154  plaque = "plaques"
   155  {{</ code-toggle >}}
   156  
   157  ## Translate Your Content
   158  
   159  Translated articles are identified by the name of the content file.
   160  
   161  ### Examples of Translated Articles
   162  
   163  1. `/content/about.en.md`
   164  2. `/content/about.fr.md`
   165  
   166  In this example, the `about.md` will be assigned the configured `defaultContentLanguage`. 
   167  
   168  1. `/content/about.md`
   169  2. `/content/about.fr.md`
   170  
   171  This way, you can slowly start to translate your current content without having to rename everything. If left unspecified, the default value for `defaultContentLanguage` is `en`.
   172  
   173  By having the same **directory and base filename**, the content pieces are linked together as translated pieces.
   174  
   175  You can also set the key used to link the translations explicitly in front matter:
   176  
   177  ```yaml
   178  translationKey: "my-story"
   179  ```
   180  
   181  If you need distinct URLs per language, you can set the slug in the non-default language file. For example, you can define a custom slug for a French translation in the front matter of `content/about.fr.md` as follows:
   182  
   183  ```yaml
   184  slug: "a-propos"
   185  
   186  ```
   187  
   188  At render, Hugo will build both `/about/` and `/a-propos/` as properly linked translated pages.
   189  
   190  For merging of content from other languages (i.e. missing content translations), see [lang.Merge](/functions/lang.merge/).
   191  
   192  ## Link to Translated Content
   193  
   194  To create a list of links to translated content, use a template similar to the following:
   195  
   196  {{< code file="layouts/partials/i18nlist.html" >}}
   197  {{ if .IsTranslated }}
   198  <h4>{{ i18n "translations" }}</h4>
   199  <ul>
   200      {{ range .Translations }}
   201      <li>
   202          <a href="{{ .Permalink }}">{{ .Lang }}: {{ .Title }}{{ if .IsPage }} ({{ i18n "wordCount" . }}){{ end }}</a>
   203      </li>
   204      {{ end}}
   205  </ul>
   206  {{ end }}
   207  {{< /code >}}
   208  
   209  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.
   210  
   211  The above also uses the [`i18n` function][i18func] described in the next section.
   212  
   213  ## List All Available Languages
   214  
   215  `.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:
   216  
   217  
   218  {{< code file="layouts/partials/allLanguages.html" >}}
   219  <ul>
   220  {{ range $.Site.Home.AllTranslations }}
   221  <li><a href="{{ .Permalink }}">{{ .Language.LanguageName }}</a></li>
   222  {{ end }}
   223  </ul>
   224  {{< /code >}}
   225  
   226  ## Translation of Strings
   227  
   228  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.
   229  
   230  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.
   231  
   232  {{% note %}}
   233  From **Hugo 0.31** you no longer need to use a valid language code. It _can be_ anything.
   234  
   235  See https://github.com/gohugoio/hugo/issues/3564
   236  
   237  {{% /note %}}
   238  
   239  From within your templates, use the `i18n` function like this:
   240  
   241  ```
   242  {{ i18n "home" }}
   243  ```
   244  
   245  This uses a definition like this one in `i18n/en-US.toml`:
   246  
   247  ```
   248  [home]
   249  other = "Home"
   250  ```
   251  
   252  Often you will want to use to the page variables in the translations strings. To do that, pass on the "." context when calling `i18n`:
   253  
   254  ```
   255  {{ i18n "wordCount" . }}
   256  ```
   257  
   258  This uses a definition like this one in `i18n/en-US.toml`:
   259  
   260  ```
   261  [wordCount]
   262  other = "This article has {{ .WordCount }} words."
   263  ```
   264  An example of singular and plural form:
   265  
   266  ```
   267  [readingTime]
   268  one = "One minute read"
   269  other = "{{.Count}} minutes read"
   270  ```
   271  And then in the template:
   272  
   273  ```
   274  {{ i18n "readingTime" .ReadingTime }}
   275  ```
   276  To track down missing translation strings, run Hugo with the `--i18n-warnings` flag:
   277  
   278  ```
   279   hugo --i18n-warnings | grep i18n
   280  i18n|MISSING_TRANSLATION|en|wordCount
   281  ```
   282  
   283  ## Customize Dates
   284  
   285  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:
   286  
   287  ~~~yaml
   288  1: "janvier"
   289  2: "février"
   290  3: "mars"
   291  4: "avril"
   292  5: "mai"
   293  6: "juin"
   294  7: "juillet"
   295  8: "août"
   296  9: "septembre"
   297  10: "octobre"
   298  11: "novembre"
   299  12: "décembre"
   300  ~~~
   301  
   302  ... then index the non-English date names in your templates like so:
   303  
   304  ~~~html
   305  <time class="post-date" datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" | safeHTML }}">
   306    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 }})
   307  </time>
   308  ~~~
   309  
   310  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.
   311  
   312  ## Menus
   313  
   314  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:
   315  
   316  ```
   317  defaultContentLanguage = "en"
   318  
   319  [languages.en]
   320  weight = 0
   321  languageName = "English"
   322  
   323  [[languages.en.menu.main]]
   324  url    = "/"
   325  name   = "Home"
   326  weight = 0
   327  
   328  
   329  [languages.de]
   330  weight = 10
   331  languageName = "Deutsch"
   332  
   333  [[languages.de.menu.main]]
   334  url    = "/"
   335  name   = "Startseite"
   336  weight = 0
   337  ```
   338  
   339  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.
   340  
   341  ```
   342  <ul>
   343      {{- $currentPage := . -}}
   344      {{ range .Site.Menus.main -}}
   345      <li class="{{ if $currentPage.IsMenuCurrent "main" . }}active{{ end }}">
   346          <a href="{{ .URL | absLangURL }}">{{ .Name }}</a>
   347      </li>
   348      {{- end }}
   349  </ul>
   350  
   351  ```
   352  
   353  ## Missing Translations
   354  
   355  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.
   356  
   357  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.
   358  
   359  {{% note %}}
   360  Hugo will generate your website with these missing translation placeholders. It might not be suited for production environments.
   361  {{% /note %}}
   362  
   363  For merging of content from other languages (i.e. missing content translations), see [lang.Merge](/functions/lang.merge/).
   364  
   365  ## Multilingual Themes support
   366  
   367  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:
   368  
   369  * Come from the built-in `.Permalink` or `.RelPermalink`
   370  * Be constructed with
   371      * The [`relLangURL` template function][rellangurl] or the [`absLangURL` template function][abslangurl] **OR**
   372      * Prefixed with `{{ .LanguagePrefix }}`
   373  
   374  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.
   375  
   376  [abslangurl]: /functions/abslangurl
   377  [config]: /getting-started/configuration/
   378  [contenttemplate]: /templates/single-page-templates/
   379  [go-i18n-source]: https://github.com/nicksnyder/go-i18n
   380  [go-i18n]: https://github.com/nicksnyder/go-i18n
   381  [homepage]: /templates/homepage/
   382  [i18func]: /functions/i18n/
   383  [menus]: /content-management/menus/
   384  [rellangurl]: /functions/rellangurl
   385  [RFC 5646]: https://tools.ietf.org/html/rfc5646
   386  [singles]: /templates/single-page-templates/