Unclear how to use lang.Merge - How to add language fallback for content


#1

After reading the documentation I found regarding translating content files, I find it unclear how to add a language fallback in case of missing content.

Assuming I have this file structure:

content
|__ _index.md <-- English content
|__ _index.de.md <-- German content

where content/_index.md has a param:

test: "This is a test"

and content/_index.de.md:

test: ""

how do I implement the fallback to English?

Documentation pages:



#2

I wrote this:

What was unclear in that page? The idea behind lang.Merge is to fill in the gaps in where you list your pages.


#3

Can you show me an example of how to use it in the particular case I mentioned? What exactly should I put in the template?


#4

Sorry, I cannot do that.


#5

Sorry, let me rephrase:

In the page you wrote, you included a practical example. Is that snippet supposed to be included at the top of every base template file?

I tried adding it in the layouts/_default/baseof.html.

My content/about/_index.md file contains (among others) the parameter:

---
test: "This is a test"
---

In the German version of the page (content/about/_index.de.md) I removed this parameter.

At this point, I’d expect that the content in the template should fallback to English when I do

<p>{{ .Params.test }}</p>

Instead, it prints nothing.

I also tried keeping the parameter test in content/about/_index.de.md, but instead giving it an empty string as content:

---
test: ""
---

but still I get the same result.

What am I missing?


#6

I saw that .Site.RegularPages was not returning any results so after some investigation I refactor the snippet you posted in the documentation page to this:

{{ $pages := (where .Site.Pages "Kind" "section") }}
{{ .Scratch.Set "pages" $pages }}
{{ $mainSite := .Sites.First }}
{{ if ne $mainSite .Site }}
   {{ .Scratch.Set "pages" ($pages | lang.Merge (where $mainSite.Pages "Kind" "section")) }}
{{ end }}
{{ $pages := .Scratch.Get "pages" }}

But the German translations are still missing…


#7

@bep, in the page you wrote you mentioned

Will “fill in the gaps”

How do you define the “gap” here?
If this is the content file in the default language (English), then how do I introduce a “gap” in another language?

# content/about/_index.md
---
title: "About us"
foo: "bar"
---
foobar

I tried the following cases:
1.

# content/about/_index.de.md
---
title: "Über uns"
foo: ""
---
# content/about/_index.de.md
---
title: "Über uns"
foo
---
# content/about/_index.de.md
---
title: "Über uns"
---

None of the above “gaps” was filled in after running the following snippet:

{{ $pages := (where .Site.Pages "Kind" "section") }}
{{ .Scratch.Set "pages" $pages }}
{{ $mainSite := .Sites.First }}
{{ if ne $mainSite .Site }}
   {{ .Scratch.Set "pages" ($pages | lang.Merge (where $mainSite.Pages "Kind" "section")) }}
{{ end }}
{{ $pages := .Scratch.Get "pages" }}

{{ range $pages }}
  <p>{{ .Params.foo }}</p>
{{ end }}

#8

A gap is a “missing translation”, e.g. you have content/about/_index.md – but not content/about/_index.de.md


Could need some help creating a multilanguage website
#9

I just figured that out at the same time… OK, I see now.
So, if I understood correctly, there is no way out of the box to fill-in partially missing translations, it’s either all or nothing.


#10

What do you mean by “partly missing”? If you mean partially translated content files (so you have half the about.fr.md in French and somehow add English for the missing paragraphs …?) , the answer is no. That would not be practical.


#11

Yes, that’s exactly what I mean. Thanks for your feedback.


#12

I will re-use this topic since I don´t get lang.Merge to work.

I have content/work/people.md but not content/work/people.de.md

Can someone give me a hint how I have to use lang.Merge?


#13

You need to show what you’ve gotten so far, and then folks can clone your work and assist. Please see Requesting Help for more information on getting help.

Also, gonna close this and have you open a new #support issue, as this person wasn’t failing to get lang.Merge working, they were looking for different functions. If you have an issue, provide code and detailed questions in a new post. :slight_smile:


#14