docs: Add docs for lang.Merge

See https://github.com/gohugoio/hugo/issues/4463
This commit is contained in:
Bjørn Erik Pedersen 2018-03-16 10:08:01 +01:00
parent ffaec4ca8c
commit 70005364a2
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 51 additions and 6 deletions

View file

@ -178,11 +178,6 @@ You can also set the key used to link the translations explicitly in front matte
translationKey: "my-story" translationKey: "my-story"
``` ```
{{% note %}}
**Before Hugo 0.31**, the file's directory was not considered when looking for translations. This did not work when you named all of your content files, say, `index.md`. Now we use the full content path.
{{% /note %}}
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: 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:
```yaml ```yaml
@ -192,6 +187,7 @@ slug: "a-propos"
At render, Hugo will build both `/about/` and `/a-propos/` as properly linked translated pages. At render, Hugo will build both `/about/` and `/a-propos/` as properly linked translated pages.
For merging of content from other languages (i.e. missing content translations), see [lang.Merge](/functions/lang.merge/).
## Link to Translated Content ## Link to Translated Content
@ -354,7 +350,7 @@ The rendering of the main navigation works as usual. `.Site.Menus` will just con
``` ```
## Missing translations ## 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. 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.
@ -364,6 +360,8 @@ 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 suited for production environments. Hugo will generate your website with these missing translation placeholders. It might not be suited for production environments.
{{% /note %}} {{% /note %}}
For merging of content from other languages (i.e. missing content translations), see [lang.Merge](/functions/lang.merge/).
## Multilingual Themes support ## Multilingual Themes support
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: 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:

View file

@ -0,0 +1,47 @@
---
title: lang.Merge
description: "Merge missing translations from other languages."
godocref: ""
workson: []
date: 2018-03-16
categories: [functions]
keywords: [multilingual]
menu:
docs:
parent: "functions"
toc: false
signature: ["lang.Merge FROM TO"]
workson: []
hugoversion:
relatedfuncs: []
deprecated: false
draft: false
aliases: []
comments:
---
As an example:
```bash
{{ $pages := .Site.RegularPages | lang.Merge $frSite.RegularPages | lang.Merge $enSite.RegularPages }}
```
Will "fill in the gaps" in the current site with, from left to right, content from the French site, and lastly the English.
A more practical example is to fill in the missing translations for the "minority languages" with content from the main language:
```bash
{{ $pages := .Site.RegularPages }}
{{ .Scratch.Set "pages" $pages }}
{{ $mainSite := .Sites.First }}
{{ if ne $mainSite .Site }}
{{ .Scratch.Set "pages" ($pages | lang.Merge $mainSite.RegularPages) }}
{{ end }}
{{ $pages := .Scratch.Get "pages" }}
```
{{% note %}}
Note that the slightly ugly `.Scratch` construct will not be needed once this is fixed: https://github.com/golang/go/issues/10608
{{% /note %}}