Fix .RenderString issue in .Translations
authorPaul Gottschling <paul.gottschling@gmail.com>
Fri, 14 Jan 2022 21:08:11 +0000 (16:08 -0500)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 27 Jan 2022 10:51:13 +0000 (11:51 +0100)
Fixes #9383

hugolib/page.go
hugolib/page_test.go
resources/page/page_lazy_contentprovider.go

index 286d210752c55fd805ff1ac07d1f30648d3d08be..d5055e7c11ed49903d027ee16b5faea742e54954 100644 (file)
@@ -635,7 +635,19 @@ func (p *pageState) RenderString(args ...interface{}) (template.HTML, error) {
                }
        }
 
-       c, err := p.pageOutput.cp.renderContentWithConverter(conv, []byte(s), false)
+       var cp *pageContentOutput
+
+       // If the current content provider is not yet initialized, do so now.
+       if lcp, ok := p.pageOutput.ContentProvider.(*page.LazyContentProvider); ok {
+               c := lcp.Init()
+               if pco, ok := c.(*pageContentOutput); ok {
+                       cp = pco
+               }
+       } else {
+               cp = p.pageOutput.cp
+       }
+
+       c, err := cp.renderContentWithConverter(conv, []byte(s), false)
        if err != nil {
                return "", p.wrapError(err)
        }
index fc01bbf25ebb7b00d0d711680369f65ccd2fb652..48a81ee4aa060511b59241525455560768c6d80a 100644 (file)
@@ -768,6 +768,69 @@ Here is the last report for commits in the year 2016. It covers hrev50718-hrev50
 `)
 }
 
+// Issue 9383
+func TestRenderStringForRegularPageTranslations(t *testing.T) {
+       c := qt.New(t)
+       b := newTestSitesBuilder(t)
+       b.WithLogger(loggers.NewBasicLoggerForWriter(jwalterweatherman.LevelDebug, os.Stderr))
+
+       b.WithConfigFile("toml",
+               `baseurl = "https://example.org/"
+title = "My Site"
+
+defaultContentLanguage = "ru"
+defaultContentLanguageInSubdir = true
+
+[languages.ru]
+contentDir = 'content/ru'
+weight = 1
+
+[languages.en]
+weight = 2
+contentDir = 'content/en'
+
+[outputs]
+home = ["HTML", "JSON"]`)
+
+       b.WithTemplates("index.html", `
+{{- range .Site.Home.Translations -}}
+       <p>{{- .RenderString "foo" -}}</p>
+{{- end -}}
+{{- range .Site.Home.AllTranslations -}}
+       <p>{{- .RenderString "bar" -}}</p>
+{{- end -}}
+`, "_default/single.html",
+               `{{ .Content }}`,
+               "index.json",
+               `{"Title": "My Site"}`,
+       )
+
+       b.WithContent(
+               "ru/a.md",
+               "",
+               "en/a.md",
+               "",
+       )
+
+       err := b.BuildE(BuildCfg{})
+       c.Assert(err, qt.Equals, nil)
+
+       b.AssertFileContent("public/ru/index.html", `
+<p>foo</p>
+<p>foo</p>
+<p>bar</p>
+<p>bar</p>
+`)
+
+       b.AssertFileContent("public/en/index.html", `
+<p>foo</p>
+<p>foo</p>
+<p>bar</p>
+<p>bar</p>
+`)
+
+}
+
 // Issue 8919
 func TestContentProviderWithCustomOutputFormat(t *testing.T) {
        b := newTestSitesBuilder(t)
index a513a063a16f2850ee0c037339cf565585827b8a..9979856f8c50599c0895947c6f62264e89362723 100644 (file)
@@ -49,6 +49,11 @@ func NewLazyContentProvider(f func() (ContentProvider, error)) *LazyContentProvi
        return &lcp
 }
 
+func (lcp *LazyContentProvider) Init() ContentProvider {
+       lcp.init.Do()
+       return lcp.cp
+}
+
 func (lcp *LazyContentProvider) Reset() {
        lcp.init.Reset()
 }