mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
Merge commit '90ad8045056167004d27857a95542936657b8a16'
This commit is contained in:
@@ -4,7 +4,6 @@ linktitle: Multilingual
|
||||
description: Hugo supports the creation of websites with multiple languages side by side.
|
||||
date: 2017-01-10
|
||||
publishdate: 2017-01-10
|
||||
lastmod: 2017-01-10
|
||||
categories: [content management]
|
||||
keywords: [multilingual,i18n, internationalization]
|
||||
menu:
|
||||
@@ -335,13 +334,13 @@ This article has 101 words.
|
||||
|
||||
### Query a singular/plural translation
|
||||
|
||||
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 other 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 }}
|
||||
```
|
||||
|
||||
The function will read `.Count` from `.ReadingTime` and evaluate where the number is singular (`one`) or plural (`other`). After that, it will pass to `readingTime` id:
|
||||
The function will read `.Count` from `.ReadingTime` and evaluate whether the number is singular (`one`) or plural (`other`). After that, it will pass to `readingTime` id in `i18n/en-US.toml` file:
|
||||
|
||||
{{< code-toggle file="i18n/en-US" >}}
|
||||
[readingTime]
|
||||
@@ -349,7 +348,7 @@ one = "One minute to read"
|
||||
other = "{{.Count}} minutes to read"
|
||||
{{< /code-toggle >}}
|
||||
|
||||
Assume `.ReadingTime.Count` in the context has value of 525600. The result will be:
|
||||
Assuming `.ReadingTime.Count` in the context has value is 525600. The result will be:
|
||||
|
||||
```text
|
||||
525600 minutes to read
|
||||
@@ -361,7 +360,7 @@ If `.ReadingTime.Count` in the context has value is 1. The result is:
|
||||
One minute to read
|
||||
```
|
||||
|
||||
In case you need to pass custom data: (`(dict "Count" 25)` is minimum requirement)
|
||||
In case you need to pass a custom data: (`(dict "Count" numeric_value_only)` is minimum requirement)
|
||||
|
||||
```go-html-template
|
||||
{{ i18n "readingTime" (dict "Count" 25 "FirstArgument" true "SecondArgument" false "Etc" "so on, so far") }}
|
||||
@@ -507,6 +506,40 @@ The rendering of the main navigation works as usual. `.Site.Menus` will just con
|
||||
</ul>
|
||||
```
|
||||
|
||||
### Dynamically localizing menus with i18n
|
||||
While customizing menus per language is useful, your config file can become hard to maintain if you have a lot of languages
|
||||
|
||||
If your menus are the same in all languages (ie. if the only thing that changes is the translated name) you can use the `.Identifier` as a translation key for the menu name:
|
||||
|
||||
{{< code-toggle file="config" >}}
|
||||
[[menu.main]]
|
||||
name = "About me"
|
||||
url = "about"
|
||||
weight = 1
|
||||
identifier = "about"
|
||||
{{< /code-toggle >}}
|
||||
|
||||
You now need to specify the translations for the menu keys in the i18n files:
|
||||
|
||||
{{< code file="i18n/pt.toml" >}}
|
||||
[about]
|
||||
other="Sobre mim"
|
||||
{{< /code >}}
|
||||
|
||||
And do the appropriate changes in the menu code to use the `i18n` tag with the `.Identifier` as a key. You will also note that here we are using a `default` to fall back to `.Name`, in case the `.Identifier` key is also not present in the language specified in the `defaultContentLanguage` configuration.
|
||||
|
||||
{{< code file="layouts/partials/menu.html" >}}
|
||||
<ul>
|
||||
{{- $currentPage := . -}}
|
||||
{{ range .Site.Menus.main -}}
|
||||
<li class="{{ if $currentPage.IsMenuCurrent "main" . }}active{{ end }}">
|
||||
<a href="{{ .URL | absLangURL }}">{{ i18n .Identifier | default .Name}}</a>
|
||||
</li>
|
||||
{{- end }}
|
||||
</ul>
|
||||
{{< /code >}}
|
||||
|
||||
|
||||
## Missing Translations
|
||||
|
||||
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.
|
||||
@@ -535,6 +568,12 @@ To support Multilingual mode in your themes, some considerations must be taken f
|
||||
|
||||
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).
|
||||
|
||||
|
||||
## Generate multilingual content with `hugo new`
|
||||
Currently, `hugo new` is not ready to support generating multilingual content. But there is a [proposal topic](https://github.com/gohugoio/hugo/issues/7732) about this in Github issue to discuss how it should work.
|
||||
|
||||
|
||||
|
||||
[abslangurl]: /functions/abslangurl
|
||||
[config]: /getting-started/configuration/
|
||||
[contenttemplate]: /templates/single-page-templates/
|
||||
|
Reference in New Issue
Block a user