Add localization examples (#1563)

Closes #1556
This commit is contained in:
Joe Mooring authored and GitHub committed 2021-11-04 07:10:39 -07:00
1 parent cf8627c2e6
commit 7cf058bfdd
1 file changed
+133 -65
+133 -65
View File
@@ -19,7 +19,7 @@ toc: true
You should define the available languages in a `languages` section in your site configuration. You should define the available languages in a `languages` section in your site configuration.
> Also See [Hugo Multilingual Part 1: Content translation](https://regisphilibert.com/blog/2018/08/hugo-multilingual-part-1-managing-content-translation/) > Also See [Hugo Multilingual Part 1: Content translation]
## Configure Languages ## Configure Languages
@@ -63,7 +63,7 @@ Anything not defined in a `languages` block will fall back to the global value f
With the configuration above, all content, sitemap, RSS feeds, paginations, With the configuration above, all content, sitemap, RSS feeds, paginations,
and taxonomy pages will be rendered below `/` in English (your default content language) and then below `/fr` in French. and taxonomy pages will be rendered below `/` in English (your default content language) and then below `/fr` in French.
When working with front matter `Params` in [single page templates][singles], omit the `params` in the key for the translation. When working with front matter `Params` in [single page templates], omit the `params` in the key for the translation.
`defaultContentLanguage` sets the project's default language. If not set, the default language will be `en`. `defaultContentLanguage` sets the project's default language. If not set, the default language will be `en`.
@@ -83,25 +83,24 @@ disableLanguages = ["fr", "ja"]
Note that you cannot disable the default content language. Note that you cannot disable the default content language.
We kept this as a standalone setting to make it easier to set via [OS environment](/getting-started/configuration/#configure-with-environment-variables): We kept this as a standalone setting to make it easier to set via [OS environment]:
```bash ```bash
HUGO_DISABLELANGUAGES="fr ja" hugo HUGO_DISABLELANGUAGES="fr ja" hugo
``` ```
If you have already a list of disabled languages in `config.toml`, you can enable them in development like this: If you have already a list of disabled languages in `config.toml`, you can enable them in development like this:
```bash ```bash
HUGO_DISABLELANGUAGES=" " hugo server HUGO_DISABLELANGUAGES=" " hugo server
``` ```
### Configure Multilingual Multihost ### Configure Multilingual Multihost
From **Hugo 0.31** we support multiple languages in a multihost configuration. See [this issue](https://github.com/gohugoio/hugo/issues/4027) for details. From **Hugo 0.31** we support multiple languages in a multihost configuration. See [this issue](https://github.com/gohugoio/hugo/issues/4027) for details.
This means that you can now configure a `baseURL` per `language`: This means that you can now configure a `baseURL` per `language`:
> If a `baseURL` is set on the `language` level, then all languages must have one and they must all be different. > If a `baseURL` is set on the `language` level, then all languages must have one and they must all be different.
Example: Example:
@@ -123,7 +122,7 @@ title = "In English"
With the above, the two sites will be generated into `public` with their own root: With the above, the two sites will be generated into `public` with their own root:
```bash ```text
public public
├── en ├── en
└── fr └── fr
@@ -133,7 +132,7 @@ public
When you run `hugo server` we will start multiple HTTP servers. You will typically see something like this in the console: When you run `hugo server` we will start multiple HTTP servers. You will typically see something like this in the console:
```bash ```text
Web Server is available at 127.0.0.1:1313 (bind address 127.0.0.1) Web Server is available at 127.0.0.1:1313 (bind address 127.0.0.1)
Web Server is available at 127.0.0.1:1314 (bind address 127.0.0.1) Web Server is available at 127.0.0.1:1314 (bind address 127.0.0.1)
Press Ctrl+C to stop Press Ctrl+C to stop
@@ -145,7 +144,6 @@ Live reload and `--navigateToChanged` between the servers work as expected.
Taxonomies and [Blackfriday configuration][config] can also be set per language: Taxonomies and [Blackfriday configuration][config] can also be set per language:
{{< code-toggle file="config" >}} {{< code-toggle file="config" >}}
[Taxonomies] [Taxonomies]
tag = "tags" tag = "tags"
@@ -195,7 +193,6 @@ If a file has no language code, it will be assigned the default language.
This system uses different content directories for each of the languages. Each language's content directory is set using the `contentDir` param. This system uses different content directories for each of the languages. Each language's content directory is set using the `contentDir` param.
{{< code-toggle file="config" >}} {{< code-toggle file="config" >}}
languages: languages:
en: en:
weight: 10 weight: 10
@@ -205,7 +202,6 @@ languages:
weight: 20 weight: 20
languageName: "Français" languageName: "Français"
contentDir: "content/french" contentDir: "content/french"
{{< /code-toggle >}} {{< /code-toggle >}}
The value of `contentDir` can be any valid path -- even absolute path references. The only restriction is that the content directories cannot overlap. The value of `contentDir` can be any valid path -- even absolute path references. The only restriction is that the content directories cannot overlap.
@@ -222,7 +218,7 @@ Their language is __assigned__ according to the content directory they are __pla
By having the same **path and basename** (relative to their language content directory), the content pieces are __linked__ together as translated pages. By having the same **path and basename** (relative to their language content directory), the content pieces are __linked__ together as translated pages.
### Bypassing default linking. ### Bypassing default linking
Any pages sharing the same `translationKey` set in front matter will be linked as translated pages regardless of basename or location. Any pages sharing the same `translationKey` set in front matter will be linked as translated pages regardless of basename or location.
@@ -232,14 +228,12 @@ Considering the following example:
2. `/content/om.nn.md` 2. `/content/om.nn.md`
3. `/content/presentation/a-propos.fr.md` 3. `/content/presentation/a-propos.fr.md`
```yaml {{< code-toggle >}}
# set in all three pages
translationKey: "about" translationKey: "about"
``` {{< /code-toggle >}}
By setting the `translationKey` front matter param to `about` in all three pages, they will be __linked__ as translated pages. By setting the `translationKey` front matter param to `about` in all three pages, they will be __linked__ as translated pages.
### Localizing permalinks ### Localizing permalinks
Because paths and filenames are used to handle linking, all translated pages will share the same URL (apart from the language subdirectory). Because paths and filenames are used to handle linking, all translated pages will share the same URL (apart from the language subdirectory).
@@ -253,7 +247,6 @@ Title: A Propos
slug: "a-propos" slug: "a-propos"
{{< /code-toggle >}} {{< /code-toggle >}}
At render, Hugo will build both `/about/` and `/fr/a-propos/` while maintaining their translation linking. At render, Hugo will build both `/about/` and `/fr/a-propos/` while maintaining their translation linking.
{{% note %}} {{% note %}}
@@ -283,16 +276,16 @@ To create a list of links to translated content, use a template similar to the f
{{ if .IsTranslated }} {{ if .IsTranslated }}
<h4>{{ i18n "translations" }}</h4> <h4>{{ i18n "translations" }}</h4>
<ul> <ul>
{{ range .Translations }} {{ range .Translations }}
<li> <li>
<a href="{{ .Permalink }}">{{ .Lang }}: {{ .Title }}{{ if .IsPage }} ({{ i18n "wordCount" . }}){{ end }}</a> <a href="{{ .Permalink }}">{{ .Lang }}: {{ .Title }}{{ if .IsPage }} ({{ i18n "wordCount" . }}){{ end }}</a>
</li> </li>
{{ end }} {{ end }}
</ul> </ul>
{{ end }} {{ end }}
{{< /code >}} {{< /code >}}
The above can be put in a `partial` (i.e., inside `layouts/partials/`) and included in any template, whether a [single content page][contenttemplate] or the [homepage][]. It will not print anything if there are no translations for a given page. The above can be put in a `partial` (i.e., inside `layouts/partials/`) and included in any template, whether a [single content page][contenttemplate] or the [homepage]. It will not print anything if there are no translations for a given page.
The above also uses the [`i18n` function][i18func] described in the next section. The above also uses the [`i18n` function][i18func] described in the next section.
@@ -300,7 +293,6 @@ The above also uses the [`i18n` function][i18func] described in the next section
`.AllTranslations` on a `Page` can be used to list all translations, including the page itself. On the home page it can be used to build a language navigator: `.AllTranslations` on a `Page` can be used to list all translations, including the page itself. On the home page it can be used to build a language navigator:
{{< code file="layouts/partials/allLanguages.html" >}} {{< code file="layouts/partials/allLanguages.html" >}}
<ul> <ul>
{{ range $.Site.Home.AllTranslations }} {{ range $.Site.Home.AllTranslations }}
@@ -311,22 +303,21 @@ The above also uses the [`i18n` function][i18func] described in the next section
## Translation of Strings ## Translation of Strings
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. 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.
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. 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.
{{% note %}} {{% note %}}
From **Hugo 0.31** you no longer need to use a valid language code. It can be anything. From **Hugo 0.31** you no longer need to use a valid language code. It can be anything.
See https://github.com/gohugoio/hugo/issues/3564 See [this issue](https://github.com/gohugoio/hugo/issues/3564)
{{% /note %}} {{% /note %}}
### Query basic translation ### Query basic translation
From within your templates, use the `i18n` function like this: From within your templates, use the `i18n` function like this:
``` ```go-html-template
{{ i18n "home" }} {{ i18n "home" }}
``` ```
@@ -339,7 +330,7 @@ other = "Home"
The result will be The result will be
``` ```text
Home Home
``` ```
@@ -347,7 +338,7 @@ Home
Often you will want to use the page variables in the translation strings. To do so, pass the `.` context when calling `i18n`: Often you will want to use the page variables in the translation strings. To do so, pass the `.` context when calling `i18n`:
``` ```go-html-template
{{ i18n "wordCount" . }} {{ i18n "wordCount" . }}
``` ```
@@ -360,7 +351,7 @@ other = "This article has {{ .WordCount }} words."
Assume `.WordCount` in the context has value is 101. The result will be: Assume `.WordCount` in the context has value is 101. The result will be:
``` ```text
This article has 101 words. This article has 101 words.
``` ```
@@ -368,7 +359,7 @@ This article has 101 words.
In order to meet singular/plural requirement, you must pass a dictionary (map) with a numeric `.Count` property to the `i18n` function. The below example uses `.ReadingTime` variable which has a built-in `.Count` property. In order to meet singular/plural requirement, you must pass a dictionary (map) with a numeric `.Count` property to the `i18n` function. The below example uses `.ReadingTime` variable which has a built-in `.Count` property.
``` ```go-html-template
{{ i18n "readingTime" .ReadingTime }} {{ i18n "readingTime" .ReadingTime }}
``` ```
@@ -382,52 +373,122 @@ other = "{{.Count}} minutes to read"
Assume `.ReadingTime.Count` in the context has value of 525600. The result will be: Assume `.ReadingTime.Count` in the context has value of 525600. The result will be:
``` ```text
525600 minutes to read 525600 minutes to read
``` ```
If `.ReadingTime.Count` in the context has value is 1. The result is: If `.ReadingTime.Count` in the context has value is 1. The result is:
``` ```text
One minute to read One minute to read
``` ```
In case you need to pass custom data: (`(dict "Count" 25)` is minimum requirement) In case you need to pass custom data: (`(dict "Count" 25)` is minimum requirement)
``` ```go-html-template
{{ i18n "readingTime" (dict "Count" 25 "FirstArgument" true "SecondArgument" false "Etc" "so on, so far") }} {{ i18n "readingTime" (dict "Count" 25 "FirstArgument" true "SecondArgument" false "Etc" "so on, so far") }}
``` ```
## Localization
## Customize Dates The following localization examples assume your site's primary language is English, with translations to French and German.
At the time of this writing, Go does not yet have support for internationalized locales for dates, 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: {{< code-toggle file="config" >}}
defaultContentLang = 'en'
~~~yaml [languages]
1: "janvier" [languages.en]
2: "février" contentDir = 'content/en'
3: "mars" languageName = 'English'
4: "avril" weight = 1
5: "mai" [languages.fr]
6: "juin" contentDir = 'content/fr'
7: "juillet" languageName = 'Français'
8: "août" weight = 2
9: "septembre" [languages.de]
10: "octobre" contentDir = 'content/de'
11: "novembre" languageName = 'Deutsch'
12: "décembre" weight = 3
~~~
...then index the non-English date names in your templates like so: {{< /code-toggle >}}
~~~html ### Dates
<time class="post-date" datetime="{{ .Date.Format `2006-01-02T15:04:05Z07:00` | safeHTML }}">
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 }})
</time>
~~~
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. With this front matter:
{{< code-toggle >}}
date = 2021-11-03T12:34:56+01:00
{{< /code-toggle >}}
And this template code:
```go-html-template
{{ .Date | time.Format ":date_full" }}
```
The rendered page displays:
Language|Value
:--|:--
English|Wednesday, November 3, 2021
Français|mercredi 3 novembre 2021
Deutsch|Mittwoch, 3. November 2021
See [time.Format] for details.
### Currency
With this template code:
```go-html-template
{{ 512.5032 | lang.FormatCurrency 2 "USD" }}
```
The rendered page displays:
Language|Value
:--|:--
English|$512.50
Français|512,50 $US
Deutsch|512,50 $
See [lang.FormatCurrency] and [lang.FormatAccounting] for details.
### Numbers
With this template code:
```go-html-template
{{ 512.5032 | lang.FormatNumber 2 }}
```
The rendered page displays:
Language|Value
:--|:--
English|512.50
Français|512,50
Deutsch|512,50
See [lang.FormatNumber] and [lang.FormatNumberCustom] for details.
### Percentages
With this template code:
```go-html-template
{{ 512.5032 | lang.FormatPercent 2 }} ---> 512.50%
```
The rendered page displays:
Language|Value
:--|:--
English|512.50%
Français|512,50 %
Deutsch|512,50 %
See [lang.FormatPercent] for details.
## Menus ## Menus
@@ -445,7 +506,6 @@ url = "/"
name = "Home" name = "Home"
weight = 0 weight = 0
[languages.de] [languages.de]
weight = 10 weight = 10
languageName = "Deutsch" languageName = "Deutsch"
@@ -458,7 +518,7 @@ weight = 0
The rendering of the main navigation works as usual. `.Site.Menus` will just contain the menu in the current language. Note that `absLangURL` below will link to the correct locale of your website. Without it, menu entries in all languages would link to the English version, since it's the default content language that resides in the root directory. The rendering of the main navigation works as usual. `.Site.Menus` will just contain the menu in the current language. Note that `absLangURL` below will link to the correct locale of your website. Without it, menu entries in all languages would link to the English version, since it's the default content language that resides in the root directory.
``` ```go-html-template
<ul> <ul>
{{- $currentPage := . -}} {{- $currentPage := . -}}
{{ range .Site.Menus.main -}} {{ range .Site.Menus.main -}}
@@ -467,7 +527,6 @@ The rendering of the main navigation works as usual. `.Site.Menus` will just con
</li> </li>
{{- end }} {{- end }}
</ul> </ul>
``` ```
## Missing Translations ## Missing Translations
@@ -480,12 +539,12 @@ While translating a Hugo website, it can be handy to have a visual indicator of
Hugo will generate your website with these missing translation placeholders. It might not be suitable for production environments. Hugo will generate your website with these missing translation placeholders. It might not be suitable for production environments.
{{% /note %}} {{% /note %}}
For merging of content from other languages (i.e. missing content translations), see [lang.Merge](/functions/lang.merge/). For merging of content from other languages (i.e. missing content translations), see [lang.Merge].
To track down missing translation strings, run Hugo with the `--i18n-warnings` flag: To track down missing translation strings, run Hugo with the `--i18n-warnings` flag:
``` ```bash
hugo --i18n-warnings | grep i18n hugo --i18n-warnings | grep i18n
i18n|MISSING_TRANSLATION|en|wordCount i18n|MISSING_TRANSLATION|en|wordCount
``` ```
@@ -504,8 +563,17 @@ If there is more than one language defined, the `LanguagePrefix` variable will e
[go-i18n-source]: https://github.com/nicksnyder/go-i18n [go-i18n-source]: https://github.com/nicksnyder/go-i18n
[go-i18n]: https://github.com/nicksnyder/go-i18n [go-i18n]: https://github.com/nicksnyder/go-i18n
[homepage]: /templates/homepage/ [homepage]: /templates/homepage/
[Hugo Multilingual Part 1: Content translation]: https://regisphilibert.com/blog/2018/08/hugo-multilingual-part-1-managing-content-translation/
[i18func]: /functions/i18n/ [i18func]: /functions/i18n/
[lang.FormatAccounting]: /functions/lang/#langformataccounting
[lang.FormatCurrency]: /functions/lang/#langformatcurrency
[lang.FormatNumber]: /functions/lang/#langformatnumber
[lang.FormatNumberCustom]: functions/lang/#langformatnumbercustom
[lang.FormatPercent]: /functions/lang/#langformatpercent
[lang.Merge]: /functions/lang.merge/
[menus]: /content-management/menus/ [menus]: /content-management/menus/
[OS environment]: /getting-started/configuration/#configure-with-environment-variables
[rellangurl]: /functions/rellangurl [rellangurl]: /functions/rellangurl
[RFC 5646]: https://tools.ietf.org/html/rfc5646 [RFC 5646]: https://tools.ietf.org/html/rfc5646
[singles]: /templates/single-page-templates/ [single page templates]: /templates/single-page-templates/
[time.Format]: /functions/dateformat