[Solved] How to list latest posts in each language of a site

Hi,

Sorry in advance if this questions was already answered, I used the search but did not find anything about my question.

I have a multilanguage website, the default language is english, and I also have some pages in french.

In my index page I want to show the 5 latest blog articles in english & the 5 latest articles in french.

For now my index template is like that :

        {{ if isset .Site.Params "latestpostcount" }}
            <div class="posts">
            {{ $nbPosts := len (where .Data.Pages "Section" "blog") }}
            {{ if gt $nbPosts 0 }}
                <div class="page-heading">Latest posts</div>
                <ul>
                {{ range (first .Site.Params.latestpostcount (where .Pages "Section" "blog")).GroupByDate "Jan, 2006" "desc" }}
                    <li class="groupby">{{ .Key }}</li>
                    {{ range sort .Pages "Date" "desc" }}
                        {{ partial "li.html" . }}
                    {{ end }}
                {{ end }}
                </ul>

                {{ if gt $nbPosts .Site.Params.latestpostcount }}
                    <a href="./blog/" class="see-more">See more ...</a>
                {{ end }}
            {{ end }}
            </div>
        {{ end }}

when I access this url : http://<mysite>/ I see the latest english posts, if I go to http://<mysite>/fr/, I see the latest french posts. I want to show in each index pages the latest posts in english & french.

As I understand .Pages "Section" "blog" will automatically give me the list of posts in the current language, is it possible to force another language ?

Thanks in advance.

I’m writing this quick and untested:

  • .AllTranslations will give you a an ordered list of all translations of a Page, including the current one.
  • .Site.Home.AllTranslations will give you all “home pages”

So, on an index page:

{{ range .AllTranslations }}
{{ $pages := where .Pages “Section” “blog”) }}
{{ end }}

Or

{{ range .Site.Home.AllTranslations }}
{{ $pages := where .Pages “Section” “blog”) }}
{{ end }}

And similar.

For completenes, the .Translations variable will get you the other languages.

Thanks a lot I’ll test that tomorrow morning :wink:

1 Like

As promised, I tested it this morning and the result is strange. First my code now looks like that :

        {{ if isset .Site.Params "latestpostcount" }}
            {{ range .Site.Home.AllTranslations }}
            <div class="posts">
            {{ .Lang }}
            {{ $nbPosts := len (where .Data.Pages "Section" "blog") }}
            {{ if gt $nbPosts 0 }}
                <div class="page-heading">Latest posts</div>
                <ul>
                {{ range (first .Site.Params.latestpostcount (where .Pages "Section" "blog")).GroupByDate "Jan, 2006" "desc" }}
                    <li class="groupby">{{ .Key }}</li>
                    {{ range sort .Pages "Date" "desc" }}
                        {{ partial "li.html" . }}
                    {{ end }}
                {{ end }}
                </ul>

                {{ if gt $nbPosts .Site.Params.latestpostcount }}
                    <a href="./blog/" class="see-more">See more ...</a>
                {{ end }}
            {{ end }}
            </div>
            {{ end }}
        {{ end }} 

I just added the range @bep specified and {{ .Lang }} to debug the language shown.

Unfortunately the result is not conclusive :

  • http://<mysite>/ shows en and the 10 latest english posts and just the string fr with not french blog posts. So Not OK
  • http://<mysite>/fr/ correctly show en and the 10 latest english posts and fr and the 10 latest french posts. So OK

So that does not work correctly for my default language page (english). For reference the relevant part my toml file is below

DefaultContentLanguage = "en"
[languages]
[languages.en]
weight = 1
[languages.en.params]

[[languages.en.menu.main]]
url    = "/blog"
name   = "Blog"
weight = -120

[[languages.en.menu.main]]
url    = "/projects"
name   = "Projects"
weight = -110

[[languages.en.menu.main]]
url    = "/about"
name   = "About"
weight = -10

[languages.fr]
weight = 2
[languages.fr.params]

[[languages.fr.menu.main]]
url    = "/blog"
name   = "Blog"
weight = -120

[[languages.fr.menu.main]]
url    = "/projects"
name   = "Projets"
weight = -110

[[languages.fr.menu.main]]
url    = "/about"
name   = "A Propos"
weight = -10

Do you have another idea ?

I forgot to report about the other expression you proposed :

  • .AllTranslations : same results as .Site.Home.AllTranslations for both pages.
  • .Translations :
    • http://<mysite>/ shows no english and just the string fr with not french blog posts. So Not OK
    • http://<mysite>/fr/ correctly show en and the 10 latest english posts and fr and the 10 latest french posts. So better but it should have shown only other translations so only english ?

Edit :

Replacing DefaultContentLanguage by defaultContentLanguage in the toml changed the result for .Translations :

  • .Translations :
    • http://<mysite>/ shows no english at all and just the string fr with not french blog posts. So Not OK
    • http://<mysite>/fr/ correctly show en and the 10 latest english posts and no fr at all. So all good it seems.

It is impossible to judge what is causing this behaviour without seeing the full site source.

Sorry I should have started with that : https://github.com/seblucas/www-blog/tree/hugo

Make sure to use the hugo branch. For now in that branch there is not any blog item that is marked as french but you can rename anything under content/blog to .fr.md as everything is written in french.

Hope this will help

I don’t see any recent commits in that branch that looks anything like what is discussed above.

As it’s published automatically to Netlify, no I haven’t pushed it. I’ll create a new branch this evening and push it

Sorry for the delay, I had no time yesterday to finish the branch (too much family work). Now it’s done you can check https://github.com/seblucas/www-blog/tree/hugo-multilanguage

You just have to start hugo serve. The index template is https://github.com/seblucas/www-blog/blob/hugo-multilanguage/themes/cocoa-eh/layouts/index.html

Thanks in advance for your help.

@bep

I tried with many versions of Hugo :

  • 0.34
  • 0.33
  • 0.32.4
  • 0.31.1
  • 0.30.2

And I always have the same result, do you want me to test other older versions ?

I’m still on this but I ran out of options :frowning:

I also tested 0.35 but the result it still the same.

I noticed something maybe interesting when I changed my config.toml to set fr as my preferred language. If I do that :

  • http://<mysite>/ show posts from both languages
  • http://<mysite>/en/ show only english posts and no french

So it seems to be really allergic to French when it show the english page :wink:

@bep Will it help if I try to reproduce it with another template ? if yes which template should I choose ?

Hi all,

I finally took a well know theme (Ananke) and changed the config to add multiple language and tweaked index.html template to add latests posts for all language and it works.

So it’s definitely a theme bug. I have not found for now but I’ll find it :wink:

Sorry for the noise

It seems I’m the only one interested in this but for completeness I’ll share what I found :

In my theme I was using :

.Data.Pages

That produce the bugs reported in this thread. The Ananke theme was using

 .Site.RegularPages

And no more problems with multilanguage. I don’t know if that’s a bug / a feature (I’ve not read the doc about those two variables) at least it now works.

1 Like