node to page: Fixe index page translation issues

Updates #2297
This commit is contained in:
Bjørn Erik Pedersen 2016-11-21 10:11:34 +01:00
parent aafbd3b4bf
commit 976f8f84bf
4 changed files with 79 additions and 17 deletions

View file

@ -195,12 +195,19 @@ func (h *HugoSites) assignMissingTranslations() error {
// Assign translations // Assign translations
for _, t1 := range nodes { for _, t1 := range nodes {
for _, t2 := range nodes { for _, t2 := range nodes {
if t2.isTranslation(t1) { if t2.isNewTranslation(t1) {
t1.translations = append(t1.translations, t2) 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 return nil
} }

View file

@ -239,26 +239,34 @@ func TestNodesAsPageMultilingual(t *testing.T) {
paginage = 1 paginage = 1
title = "Hugo Multilingual Rocks!" title = "Hugo Multilingual Rocks!"
rssURI = "customrss.xml" rssURI = "customrss.xml"
defaultContentLanguage = "nn"
defaultContentLanguageInSubdir = true
[languages] [languages]
[languages.nn] [languages.nn]
languageName = "Nynorsk" languageName = "Nynorsk"
weight = 1 weight = 1
title = "Hugo på norsk" title = "Hugo på norsk"
defaultContentLanguage = "nn"
[languages.en] [languages.en]
languageName = "English" languageName = "English"
weight = 2 weight = 2
title = "Hugo in English" title = "Hugo in English"
[languages.de]
languageName = "Deutsch"
weight = 3
title = "Deutsche Hugo"
`) `)
for _, lang := range []string{"nn", "en"} { for _, lang := range []string{"nn", "en"} {
writeRegularPagesForNodeAsPageTestsWithLang(t, lang) 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("en", t)
writeNodePagesForNodeAsPageTests("de", t)
if err := LoadGlobalConfig("", "config.toml"); err != nil { if err := LoadGlobalConfig("", "config.toml"); err != nil {
t.Fatalf("Failed to load config: %s", err) t.Fatalf("Failed to load config: %s", err)
@ -270,7 +278,7 @@ title = "Hugo in English"
t.Fatalf("Failed to create sites: %s", err) 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)) t.Fatalf("Got %d sites", len(sites.Sites))
} }
@ -280,12 +288,34 @@ title = "Hugo in English"
t.Fatalf("Failed to build sites: %s", err) 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, assertFileContent(t, filepath.Join("public", "nn", "index.html"), true,
"Index Title: Hugo på norsk") "Index Title: Hugo på norsk")
assertFileContent(t, filepath.Join("public", "en", "index.html"), true, assertFileContent(t, filepath.Join("public", "en", "index.html"), true,
"Index Title: Home Sweet Home!", "<strong>Content!</strong>") "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 // Taxonomy list
assertFileContent(t, filepath.Join("public", "nn", "categories", "hugo", "index.html"), true, assertFileContent(t, filepath.Join("public", "nn", "categories", "hugo", "index.html"), true,
@ -528,8 +558,8 @@ title: Home Sweet Home!
date : %q date : %q
lastMod : %q lastMod : %q
--- ---
Home **Content!** l-%s Home **Content!**
`, date.Add(1*24*time.Hour).Format(time.RFC822), date.Add(2*24*time.Hour).Format(time.RFC822))) `, 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(`--- writeSource(t, filepath.Join("content", "sect1", filename), fmt.Sprintf(`---
title: Section1 title: Section1

View file

@ -1608,19 +1608,19 @@ func (p *Page) Lang() string {
return p.lang return p.lang
} }
func (p *Page) isTranslation(candidate *Page) bool { func (p *Page) isNewTranslation(candidate *Page) bool {
if p == candidate || p.Kind != candidate.Kind { if p == candidate || p.Kind != candidate.Kind {
return false return false
} }
if p.lang != candidate.lang || p.language != p.language {
return false
}
if p.Kind == KindPage || p.Kind == kindUnknown { if p.Kind == KindPage || p.Kind == kindUnknown {
panic("Node type not currently supported for this op") 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) // 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. // It represents the same node, but different language, if the sections is the same.
if len(p.sections) != len(candidate.sections) { if len(p.sections) != len(candidate.sections) {
@ -1633,6 +1633,13 @@ func (p *Page) isTranslation(candidate *Page) bool {
} }
} }
// Finally check that it is not already added.
for _, translation := range candidate.translations {
if p == translation {
return false
}
}
return true return true
} }
@ -1717,9 +1724,13 @@ func (p *Page) addLangFilepathPrefix(outfile string) string {
} }
func sectionsFromFilename(filename string) []string { func sectionsFromFilename(filename string) []string {
var sections []string
dir, _ := filepath.Split(filename) dir, _ := filepath.Split(filename)
dir = strings.TrimSuffix(dir, helpers.FilePathSeparator) dir = strings.TrimSuffix(dir, helpers.FilePathSeparator)
sections := strings.Split(dir, helpers.FilePathSeparator) if dir == "" {
return sections
}
sections = strings.Split(dir, helpers.FilePathSeparator)
return sections return sections
} }

View file

@ -13,6 +13,10 @@
package hugolib package hugolib
import (
"fmt"
)
// Translations represent the other translations for a given page. The // Translations represent the other translations for a given page. The
// string here is the language code, as affected by the `post.LANG.md` // string here is the language code, as affected by the `post.LANG.md`
// filename. // filename.
@ -22,7 +26,7 @@ func pagesToTranslationsMap(ml *Multilingual, pages []*Page) map[string]Translat
out := make(map[string]Translations) out := make(map[string]Translations)
for _, page := range pages { for _, page := range pages {
base := page.TranslationBaseName() base := createTranslationKey(page)
pageTranslation, present := out[base] pageTranslation, present := out[base]
if !present { if !present {
@ -41,10 +45,22 @@ func pagesToTranslationsMap(ml *Multilingual, pages []*Page) map[string]Translat
return out 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) { func assignTranslationsToPages(allTranslations map[string]Translations, pages []*Page) {
for _, page := range pages { for _, page := range pages {
page.translations = page.translations[:0] page.translations = page.translations[:0]
base := page.TranslationBaseName() base := createTranslationKey(page)
trans, exist := allTranslations[base] trans, exist := allTranslations[base]
if !exist { if !exist {
continue continue
@ -53,7 +69,5 @@ func assignTranslationsToPages(allTranslations map[string]Translations, pages []
for _, translatedPage := range trans { for _, translatedPage := range trans {
page.translations = append(page.translations, translatedPage) page.translations = append(page.translations, translatedPage)
} }
pageBy(languagePageSort).Sort(page.translations)
} }
} }