shithub: hugo

Download patch

ref: 976f8f84bfe36f2c9e2ec6992b15dbb70c812af5
parent: aafbd3b4bfeef2db8edd50870affae164c172d92
author: Bjørn Erik Pedersen <[email protected]>
date: Mon Nov 21 05:11:34 EST 2016

node to page: Fixe index page translation issues

Updates #2297

--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -195,10 +195,17 @@
 		// Assign translations
 		for _, t1 := range nodes {
 			for _, t2 := range nodes {
-				if t2.isTranslation(t1) {
+				if t2.isNewTranslation(t1) {
 					t1.translations = append(t1.translations, t2)
 				}
 			}
+		}
+	}
+
+	// Now we can sort the translations.
+	for _, p := range allPages {
+		if len(p.translations) > 0 {
+			pageBy(languagePageSort).Sort(p.translations)
 		}
 	}
 	return nil
--- a/hugolib/node_as_page_test.go
+++ b/hugolib/node_as_page_test.go
@@ -239,18 +239,25 @@
 paginage = 1
 title = "Hugo Multilingual Rocks!"
 rssURI = "customrss.xml"
+defaultContentLanguage = "nn"
+defaultContentLanguageInSubdir = true
 
+
 [languages]
 [languages.nn]
 languageName = "Nynorsk"
 weight = 1
 title = "Hugo på norsk"
-defaultContentLanguage = "nn"
 
 [languages.en]
 languageName = "English"
 weight = 2
 title = "Hugo in English"
+
+[languages.de]
+languageName = "Deutsch"
+weight = 3
+title = "Deutsche Hugo"
 `)
 
 	for _, lang := range []string{"nn", "en"} {
@@ -257,8 +264,9 @@
 		writeRegularPagesForNodeAsPageTestsWithLang(t, lang)
 	}
 
-	// Only write node pages for the English side of the fence
+	// Only write node pages for the English and Deutsch
 	writeNodePagesForNodeAsPageTests("en", t)
+	writeNodePagesForNodeAsPageTests("de", t)
 
 	if err := LoadGlobalConfig("", "config.toml"); err != nil {
 		t.Fatalf("Failed to load config: %s", err)
@@ -270,7 +278,7 @@
 		t.Fatalf("Failed to create sites: %s", err)
 	}
 
-	if len(sites.Sites) != 2 {
+	if len(sites.Sites) != 3 {
 		t.Fatalf("Got %d sites", len(sites.Sites))
 	}
 
@@ -280,12 +288,34 @@
 		t.Fatalf("Failed to build sites: %s", err)
 	}
 
-	// The en language has content pages
+	// The en and de language have content pages
+	enHome := sites.Sites[1].getPage("home")
+	require.NotNil(t, enHome)
+	require.Equal(t, "en", enHome.Language().Lang)
+	require.Contains(t, enHome.Content, "l-en")
 
+	deHome := sites.Sites[2].getPage("home")
+	require.NotNil(t, deHome)
+	require.Equal(t, "de", deHome.Language().Lang)
+	require.Contains(t, deHome.Content, "l-de")
+
+	require.Len(t, deHome.Translations(), 2, deHome.Translations()[0].Language().Lang)
+	require.Equal(t, "en", deHome.Translations()[1].Language().Lang)
+	require.Equal(t, "nn", deHome.Translations()[0].Language().Lang)
+
+	enSect := sites.Sites[1].getPage("section", "sect1")
+	require.NotNil(t, enSect)
+	require.Equal(t, "en", enSect.Language().Lang)
+	require.Len(t, enSect.Translations(), 2, enSect.Translations()[0].Language().Lang)
+	require.Equal(t, "de", enSect.Translations()[1].Language().Lang)
+	require.Equal(t, "nn", enSect.Translations()[0].Language().Lang)
+
 	assertFileContent(t, filepath.Join("public", "nn", "index.html"), true,
 		"Index Title: Hugo på norsk")
 	assertFileContent(t, filepath.Join("public", "en", "index.html"), true,
 		"Index Title: Home Sweet Home!", "<strong>Content!</strong>")
+	assertFileContent(t, filepath.Join("public", "de", "index.html"), true,
+		"Index Title: Home Sweet Home!", "<strong>Content!</strong>")
 
 	// Taxonomy list
 	assertFileContent(t, filepath.Join("public", "nn", "categories", "hugo", "index.html"), true,
@@ -528,8 +558,8 @@
 date : %q
 lastMod : %q
 ---
-Home **Content!**
-`, date.Add(1*24*time.Hour).Format(time.RFC822), date.Add(2*24*time.Hour).Format(time.RFC822)))
+l-%s Home **Content!**
+`, date.Add(1*24*time.Hour).Format(time.RFC822), date.Add(2*24*time.Hour).Format(time.RFC822), lang))
 
 	writeSource(t, filepath.Join("content", "sect1", filename), fmt.Sprintf(`---
 title: Section1
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -1608,19 +1608,19 @@
 	return p.lang
 }
 
-func (p *Page) isTranslation(candidate *Page) bool {
+func (p *Page) isNewTranslation(candidate *Page) bool {
 	if p == candidate || p.Kind != candidate.Kind {
 		return false
 	}
 
-	if p.lang != candidate.lang || p.language != p.language {
-		return false
-	}
-
 	if p.Kind == KindPage || p.Kind == kindUnknown {
 		panic("Node type not currently supported for this op")
 	}
 
+	if p.language.Lang == candidate.language.Lang {
+		return false
+	}
+
 	// At this point, we know that this is a traditional Node (home page, section, taxonomy)
 	// It represents the same node, but different language, if the sections is the same.
 	if len(p.sections) != len(candidate.sections) {
@@ -1633,6 +1633,13 @@
 		}
 	}
 
+	// Finally check that it is not already added.
+	for _, translation := range candidate.translations {
+		if p == translation {
+			return false
+		}
+	}
+
 	return true
 
 }
@@ -1717,9 +1724,13 @@
 }
 
 func sectionsFromFilename(filename string) []string {
+	var sections []string
 	dir, _ := filepath.Split(filename)
 	dir = strings.TrimSuffix(dir, helpers.FilePathSeparator)
-	sections := strings.Split(dir, helpers.FilePathSeparator)
+	if dir == "" {
+		return sections
+	}
+	sections = strings.Split(dir, helpers.FilePathSeparator)
 	return sections
 }
 
--- a/hugolib/translations.go
+++ b/hugolib/translations.go
@@ -13,6 +13,10 @@
 
 package hugolib
 
+import (
+	"fmt"
+)
+
 // Translations represent the other translations for a given page. The
 // string here is the language code, as affected by the `post.LANG.md`
 // filename.
@@ -22,7 +26,7 @@
 	out := make(map[string]Translations)
 
 	for _, page := range pages {
-		base := page.TranslationBaseName()
+		base := createTranslationKey(page)
 
 		pageTranslation, present := out[base]
 		if !present {
@@ -41,10 +45,22 @@
 	return out
 }
 
+func createTranslationKey(p *Page) string {
+	base := p.TranslationBaseName()
+
+	if p.IsNode() {
+		// TODO(bep) see https://github.com/spf13/hugo/issues/2699
+		// Must prepend the section and kind to the key to make it unique
+		base = fmt.Sprintf("%s/%s/%s", p.Kind, p.sections, base)
+	}
+
+	return base
+}
+
 func assignTranslationsToPages(allTranslations map[string]Translations, pages []*Page) {
 	for _, page := range pages {
 		page.translations = page.translations[:0]
-		base := page.TranslationBaseName()
+		base := createTranslationKey(page)
 		trans, exist := allTranslations[base]
 		if !exist {
 			continue
@@ -53,7 +69,5 @@
 		for _, translatedPage := range trans {
 			page.translations = append(page.translations, translatedPage)
 		}
-
-		pageBy(languagePageSort).Sort(page.translations)
 	}
 }