Set lang template globals for each site when render shortcodes

We should get rid of these globals, but that is another month.
This commit is contained in:
Bjørn Erik Pedersen 2016-08-12 18:17:00 +02:00
parent 28696b5dca
commit a823b1572b
3 changed files with 40 additions and 11 deletions

View file

@ -394,6 +394,9 @@ func (h *HugoSites) setupTranslations(master *Site) {
func (h *HugoSites) preRender() error {
for _, s := range h.Sites {
if err := s.setCurrentLanguageConfig(); err != nil {
return err
}
// Run "render prepare"
if err := s.renderHomePage(true); err != nil {
return err
@ -409,13 +412,20 @@ func (h *HugoSites) preRender() error {
}
}
for _, s := range h.Sites {
if err := s.setCurrentLanguageConfig(); err != nil {
return err
}
renderShortcodesForSite(s)
}
return nil
}
func renderShortcodesForSite(s *Site) {
pageChan := make(chan *Page)
wg := &sync.WaitGroup{}
// We want all the pages, so just pick one.
s := h.Sites[0]
for i := 0; i < getGoMaxProcs()*4; i++ {
wg.Add(1)
go func(pages <-chan *Page, wg *sync.WaitGroup) {
@ -456,7 +466,7 @@ func (h *HugoSites) preRender() error {
}(pageChan, wg)
}
for _, p := range s.AllPages {
for _, p := range s.Pages {
pageChan <- p
}
@ -464,7 +474,6 @@ func (h *HugoSites) preRender() error {
wg.Wait()
return nil
}
// Pages returns all pages for all sites.

View file

@ -258,6 +258,10 @@ func doTestMultiSitesBuild(t *testing.T, configContent, configSuffix string) {
assertFileContent(t, "public/en/index.html", true, "Home Page 1", "Hello", "Hugo Rocks!")
assertFileContent(t, "public/fr/index.html", true, "Home Page 1", "Bonjour", "Hugo Rocks!")
// check single page content
assertFileContent(t, "public/fr/sect/doc1/index.html", true, "Single", "Shortcode: Bonjour")
assertFileContent(t, "public/en/sect/doc1-slug/index.html", true, "Single", "Shortcode: Hello")
// Check node translations
homeEn := enSite.getNode("home-0")
require.NotNil(t, homeEn)
@ -566,8 +570,6 @@ title = "Svenska"
require.Len(t, svPage.Translations(), 2)
require.Len(t, svPage.AllTranslations(), 3)
require.Equal(t, "en", svPage.Translations()[0].Lang())
//noFile := readDestination(t, "/public/no/doc1/index.html")
//require.True(t, strings.Contains("foo", noFile), noFile)
}
@ -719,7 +721,7 @@ func createMultiTestSitesForConfig(t *testing.T, configContent, configSuffix str
// Add some layouts
if err := afero.WriteFile(hugofs.Source(),
filepath.Join("layouts", "_default/single.html"),
[]byte("Single: {{ .Title }}|{{ i18n \"hello\" }} {{ .Content }}"),
[]byte("Single: {{ .Title }}|{{ i18n \"hello\" }}|{{.Lang}}|{{ .Content }}"),
0755); err != nil {
t.Fatalf("Failed to write layout file: %s", err)
}
@ -738,6 +740,14 @@ func createMultiTestSitesForConfig(t *testing.T, configContent, configSuffix str
t.Fatalf("Failed to write layout file: %s", err)
}
// Add a shortcode
if err := afero.WriteFile(hugofs.Source(),
filepath.Join("layouts", "shortcodes", "shortcode.html"),
[]byte("Shortcode: {{ i18n \"hello\" }}"),
0755); err != nil {
t.Fatalf("Failed to write layout file: %s", err)
}
// Add some language files
if err := afero.WriteFile(hugofs.Source(),
filepath.Join("i18n", "en.yaml"),
@ -769,6 +779,9 @@ publishdate: "2000-01-01"
---
# doc1
*some "content"*
{{< shortcode >}}
NOTE: slug should be used as URL
`)},
{filepath.FromSlash("sect/doc1.fr.md"), []byte(`---
@ -780,6 +793,9 @@ publishdate: "2000-01-04"
---
# doc1
*quelque "contenu"*
{{< shortcode >}}
NOTE: should be in the 'en' Page's 'Translations' field.
NOTE: date is after "doc3"
`)},

View file

@ -776,11 +776,15 @@ func (s *Site) setupPrevNext() {
}
}
func (s *Site) render() (err error) {
func (s *Site) setCurrentLanguageConfig() error {
// There are sadly some global template funcs etc. that need the language information.
viper.Set("Multilingual", s.multilingualEnabled())
viper.Set("CurrentContentLanguage", s.Language)
if err = tpl.SetTranslateLang(s.Language.Lang); err != nil {
return tpl.SetTranslateLang(s.Language.Lang)
}
func (s *Site) render() (err error) {
if err = s.setCurrentLanguageConfig(); err != nil {
return
}