Single page multi languages?

I have a “Downloads” page on my site that is translated with variables loaded from i18n.

How can I get hugo to render /content/download/_index.md for every language without creating a /content/download/_index.de.md file?

  1. The current way of doing this is to mount the same content folder (or file) multiple times with different lang. See Configure modules
  2. For more complex setups (with many languages), the above can be a little cumbersome, but we’re working on a full rehaul of this in the next version of Hugo (with some added dimensions), which will allow you to e.g. assing multiple languages via Gob patterns to one or more pages (also via cascading so you can sett fallback languages.
1 Like

I don’t know if that is a valid approach, may lead to strange behavior later or is complex working nonsense — but a multilanguage content adapter looks promising…

removed that - has been solved by @bep and @jmooring and adjusted the code

detected possible problems:

  • download is not a section but a page: not listed in site.Sections
  • the pages show up in site.RegularPages, so a where filter is needed (see layout below)

@bep if this all is practical maybe adding support for section pages with an adapter might be an option. Did I just miss how to do that?

and here is a working example.

  • The adapter content/_content.gotmpl

    if the download section also contains pages, you will need to adjust template lookup.

    {{ .EnableAllLanguages }}
    {{ with resources.Get "download.md" }}
       {{ $pageVars := site.Data.download }}
       {{ $content := dict "mediaType" "text/markdown" "value" .Content }}
       {{ $page := dict
          "content" $content
          "kind" "section"
          "path" "download"
          "title" $pageVars.title
          "params" $pageVars.params
       }}
       {{ $.AddPage $page }}
    {{ end }}
    
  • create a markdown file /assets/download.md

    ## My download page
    
    Is this translated used a shortcode? **{{< T true >}}**
    
  • I tried to extract the frontmatter from markdown resource and create a param dict but failed (findREsubmatch, remarshal, resources.FromString …) so a externalised frontmatter to a data file

    create page params in data/download.toml

    title: download     <-- passed as title to the adapter (and translated in template)
    params:             <-- passed as frontmatter to the page
      - key1: one        
      - key2: two
    
  • create i18n files

    # de.toml
    download = 'runterladen (de)'
    true     = 'Ja'
    # en.toml
    download = 'download (en)'
    true     = 'Yes'
    # fr.toml
    download = 'tèlècharger (fr)'
    true     = 'Qui'
    
  • a layout download/page.html

    {{ define "main" }}
     <h1>Translated title: {{ T .Title }}</h1>
     {{ .Content }}
     {{ site.Sections }}
     <h2>{{ .Params.key1 }}</h2>
     <h3>{{ .Params.key2 }}</h2>
     <ul>
        {{ range where site.RegularPages "File.IsContentAdapter" "ne" true }}
           <li>{{ highlight (. | jsonify (dict "indent" "  ")) "JSON" }}</li>
        {{ end }}
     </ul>
    {{ end }}
    

p.s. the shortcode uses is as simple as {{ i18n (.Get 0) }}

1 Like

I haven’t read your entire post, but you can create all kinds of pages with a content adapter (including sections), and you’re right about this being a way to create multiple language versions easily.

Edit in: You’re right, currently it’s not possible to create the home page from a content adapter, but I’m not sure why that is, sounds like a restriction we could/should remove.

so it looks like I`m missing something:

changing the kind to section, or using type did not create any pages

1 Like

This works great (content/_content.gotmpl):

{{ .EnableAllLanguages }}
{{ $content := dict
  "mediaType" "text/markdown"
  "value" "This is the downloads page."
}}
{{ $page := dict
  "content" $content
  "kind" "section"
  "path" "download"
  "title" (printf "Downloads (%s)" site.Language.Lang)
  "weight" 1
}}
{{ .AddPage $page }}

Try it:

git clone --single-branch -b hugo-forum-topic-55022 https://github.com/jmooring/hugo-testing hugo-forum-topic-55022
cd hugo-forum-topic-55022
hugo server
2 Likes

indeed, works like a charm…

I used your repo and applied the resource, data file, i18n and the shortcode and all is fine…
played that back to mine and - boom -

Oh wow - One of those days

So in this case my conclusion is the problem is in front of my computer… there must be something terrible broken with my stuff…
I won’t tell how log it took to find it, but :shushing_face:

here's the problematic line

disableKinds = ['rss', 'section', 'taxonomy', 'term'] :woozy_face:

Wow, you guys are awesome… looks like it was fun to figure out.

I’ll try this tomorrow in the repo and see if I can get it to work in my context…

Been there, done that.

1 Like