summaryrefslogtreecommitdiffstats
path: root/hugolib
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-07-02 10:46:28 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-07-04 09:12:44 +0200
commitaa6b1b9be7c9d7322333893b642aaf8c7a5f2c2e (patch)
tree14e3995039a65d7f8e98ff47c6c738b3a7e1684b /hugolib
parenta1d260b41a6673adef679ec4e262c5f390432cf5 (diff)
output: Support templates per site/language
This applies to both regular templates and shortcodes. So, if the site language is French and the output format is AMP, this is the (start) of the lookup order for the home page: 1. index.fr.amp.html 2. index.amp.html 3. index.fr.html 4. index.html 5. ... Fixes #3360
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/hugo_sites_build_test.go36
-rw-r--r--hugolib/page.go1
-rw-r--r--hugolib/shortcode.go21
-rw-r--r--hugolib/shortcode_test.go4
4 files changed, 50 insertions, 12 deletions
diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go
index c343f6087..96e2c66b2 100644
--- a/hugolib/hugo_sites_build_test.go
+++ b/hugolib/hugo_sites_build_test.go
@@ -305,12 +305,12 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
require.True(t, strings.Contains(languageRedirect, "0; url=http://example.com/blog/fr"), languageRedirect)
// check home page content (including data files rendering)
- th.assertFileContent("public/en/index.html", "Home Page 1", "Hello", "Hugo Rocks!")
- th.assertFileContent("public/fr/index.html", "Home Page 1", "Bonjour", "Hugo Rocks!")
+ th.assertFileContent("public/en/index.html", "Default Home Page 1", "Hello", "Hugo Rocks!")
+ th.assertFileContent("public/fr/index.html", "French Home Page 1", "Bonjour", "Hugo Rocks!")
// check single page content
- th.assertFileContent("public/fr/sect/doc1/index.html", "Single", "Shortcode: Bonjour")
- th.assertFileContent("public/en/sect/doc1-slug/index.html", "Single", "Shortcode: Hello")
+ th.assertFileContent("public/fr/sect/doc1/index.html", "Single", "Shortcode: Bonjour", "LingoFrench")
+ th.assertFileContent("public/en/sect/doc1-slug/index.html", "Single", "Shortcode: Hello", "LingoDefault")
// Check node translations
homeEn := enSite.getPage(KindHome)
@@ -1042,7 +1042,14 @@ func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, conf
if err := afero.WriteFile(mf,
filepath.Join("layouts", "index.html"),
- []byte("{{ $p := .Paginator }}Home Page {{ $p.PageNumber }}: {{ .Title }}|{{ .IsHome }}|{{ i18n \"hello\" }}|{{ .Permalink }}|{{ .Site.Data.hugo.slogan }}"),
+ []byte("{{ $p := .Paginator }}Default Home Page {{ $p.PageNumber }}: {{ .Title }}|{{ .IsHome }}|{{ i18n \"hello\" }}|{{ .Permalink }}|{{ .Site.Data.hugo.slogan }}"),
+ 0755); err != nil {
+ t.Fatalf("Failed to write layout file: %s", err)
+ }
+
+ if err := afero.WriteFile(mf,
+ filepath.Join("layouts", "index.fr.html"),
+ []byte("{{ $p := .Paginator }}French Home Page {{ $p.PageNumber }}: {{ .Title }}|{{ .IsHome }}|{{ i18n \"hello\" }}|{{ .Permalink }}|{{ .Site.Data.hugo.slogan }}"),
0755); err != nil {
t.Fatalf("Failed to write layout file: %s", err)
}
@@ -1055,6 +1062,21 @@ func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, conf
t.Fatalf("Failed to write layout file: %s", err)
}
+ // A shortcode in multiple languages
+ if err := afero.WriteFile(mf,
+ filepath.Join("layouts", "shortcodes", "lingo.html"),
+ []byte("LingoDefault"),
+ 0755); err != nil {
+ t.Fatalf("Failed to write layout file: %s", err)
+ }
+
+ if err := afero.WriteFile(mf,
+ filepath.Join("layouts", "shortcodes", "lingo.fr.html"),
+ []byte("LingoFrench"),
+ 0755); err != nil {
+ t.Fatalf("Failed to write layout file: %s", err)
+ }
+
// Add some language files
if err := afero.WriteFile(mf,
filepath.Join("i18n", "en.yaml"),
@@ -1098,6 +1120,8 @@ publishdate: "2000-01-01"
{{< shortcode >}}
+{{< lingo >}}
+
NOTE: slug should be used as URL
`)},
{Name: filepath.FromSlash("sect/doc1.fr.md"), Content: []byte(`---
@@ -1113,6 +1137,8 @@ publishdate: "2000-01-04"
{{< shortcode >}}
+{{< lingo >}}
+
NOTE: should be in the 'en' Page's 'Translations' field.
NOTE: date is after "doc3"
`)},
diff --git a/hugolib/page.go b/hugolib/page.go
index cf0a2144c..0f44b8b99 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -250,6 +250,7 @@ func (p *Page) createLayoutDescriptor() output.LayoutDescriptor {
return output.LayoutDescriptor{
Kind: p.Kind,
Type: p.Type(),
+ Lang: p.Lang(),
Layout: p.Layout,
Section: section,
}
diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go
index 150d82c44..3cf472f82 100644
--- a/hugolib/shortcode.go
+++ b/hugolib/shortcode.go
@@ -157,6 +157,7 @@ func (sc shortcode) String() string {
// Note that in the below, OutputFormat may be empty.
// We will try to look for the most specific shortcode template available.
type scKey struct {
+ Lang string
OutputFormat string
Suffix string
ShortcodePlaceholder string
@@ -166,8 +167,8 @@ func newScKey(m media.Type, shortcodeplaceholder string) scKey {
return scKey{Suffix: m.Suffix, ShortcodePlaceholder: shortcodeplaceholder}
}
-func newScKeyFromOutputFormat(o output.Format, shortcodeplaceholder string) scKey {
- return scKey{Suffix: o.MediaType.Suffix, OutputFormat: o.Name, ShortcodePlaceholder: shortcodeplaceholder}
+func newScKeyFromLangAndOutputFormat(lang string, o output.Format, shortcodeplaceholder string) scKey {
+ return scKey{Lang: lang, Suffix: o.MediaType.Suffix, OutputFormat: o.Name, ShortcodePlaceholder: shortcodeplaceholder}
}
func newDefaultScKey(shortcodeplaceholder string) scKey {
@@ -251,10 +252,11 @@ const innerCleanupExpand = "$1"
func prepareShortcodeForPage(placeholder string, sc shortcode, parent *ShortcodeWithPage, p *Page) map[scKey]func() (string, error) {
m := make(map[scKey]func() (string, error))
+ lang := p.Lang()
for _, f := range p.outputFormats {
// The most specific template will win.
- key := newScKeyFromOutputFormat(f, placeholder)
+ key := newScKeyFromLangAndOutputFormat(lang, f, placeholder)
m[key] = func() (string, error) {
return renderShortcode(key, sc, nil, p), nil
}
@@ -371,9 +373,11 @@ func (s *shortcodeHandler) updateDelta() bool {
func (s *shortcodeHandler) contentShortcodesForOutputFormat(f output.Format) map[scKey]func() (string, error) {
contentShortcodesForOuputFormat := make(map[scKey]func() (string, error))
+ lang := s.p.Lang()
+
for shortcodePlaceholder := range s.shortcodes {
- key := newScKeyFromOutputFormat(f, shortcodePlaceholder)
+ key := newScKeyFromLangAndOutputFormat(lang, f, shortcodePlaceholder)
renderFn, found := s.contentShortcodes[key]
if !found {
@@ -390,7 +394,7 @@ func (s *shortcodeHandler) contentShortcodesForOutputFormat(f output.Format) map
if !found {
panic(fmt.Sprintf("Shortcode %q could not be found", shortcodePlaceholder))
}
- contentShortcodesForOuputFormat[newScKeyFromOutputFormat(f, shortcodePlaceholder)] = renderFn
+ contentShortcodesForOuputFormat[newScKeyFromLangAndOutputFormat(lang, f, shortcodePlaceholder)] = renderFn
}
return contentShortcodesForOuputFormat
@@ -676,12 +680,19 @@ func getShortcodeTemplateForTemplateKey(key scKey, shortcodeName string, t tpl.T
suffix := strings.ToLower(key.Suffix)
outFormat := strings.ToLower(key.OutputFormat)
+ lang := strings.ToLower(key.Lang)
if outFormat != "" && suffix != "" {
+ if lang != "" {
+ names = append(names, fmt.Sprintf("%s.%s.%s.%s", shortcodeName, lang, outFormat, suffix))
+ }
names = append(names, fmt.Sprintf("%s.%s.%s", shortcodeName, outFormat, suffix))
}
if suffix != "" {
+ if lang != "" {
+ names = append(names, fmt.Sprintf("%s.%s.%s", shortcodeName, lang, suffix))
+ }
names = append(names, fmt.Sprintf("%s.%s", shortcodeName, suffix))
}
diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go
index 42da9e465..3d355f947 100644
--- a/hugolib/shortcode_test.go
+++ b/hugolib/shortcode_test.go
@@ -837,8 +837,8 @@ func TestReplaceShortcodeTokens(t *testing.T) {
func TestScKey(t *testing.T) {
require.Equal(t, scKey{Suffix: "xml", ShortcodePlaceholder: "ABCD"},
newScKey(media.XMLType, "ABCD"))
- require.Equal(t, scKey{Suffix: "html", OutputFormat: "AMP", ShortcodePlaceholder: "EFGH"},
- newScKeyFromOutputFormat(output.AMPFormat, "EFGH"))
+ require.Equal(t, scKey{Lang: "en", Suffix: "html", OutputFormat: "AMP", ShortcodePlaceholder: "EFGH"},
+ newScKeyFromLangAndOutputFormat("en", output.AMPFormat, "EFGH"))
require.Equal(t, scKey{Suffix: "html", ShortcodePlaceholder: "IJKL"},
newDefaultScKey("IJKL"))