diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index f9a9745c8..0da7efb88 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -195,12 +195,19 @@ func (h *HugoSites) assignMissingTranslations() error { // 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 } diff --git a/hugolib/node_as_page_test.go b/hugolib/node_as_page_test.go index 9b0eb88a1..a152ba303 100644 --- a/hugolib/node_as_page_test.go +++ b/hugolib/node_as_page_test.go @@ -239,26 +239,34 @@ func TestNodesAsPageMultilingual(t *testing.T) { 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"} { 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 @@ title = "Hugo in English" 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 @@ title = "Hugo in English" 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!", "Content!") + assertFileContent(t, filepath.Join("public", "de", "index.html"), true, + "Index Title: Home Sweet Home!", "Content!") // Taxonomy list assertFileContent(t, filepath.Join("public", "nn", "categories", "hugo", "index.html"), true, @@ -528,8 +558,8 @@ title: Home Sweet Home! 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 diff --git a/hugolib/page.go b/hugolib/page.go index 55b36af8d..2ca2270d1 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -1608,19 +1608,19 @@ func (p *Page) Lang() string { 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 @@ 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 } @@ -1717,9 +1724,13 @@ func (p *Page) addLangFilepathPrefix(outfile string) string { } 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 } diff --git a/hugolib/translations.go b/hugolib/translations.go index e3df3acd1..36e79125d 100644 --- 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 @@ func pagesToTranslationsMap(ml *Multilingual, pages []*Page) map[string]Translat 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 @@ func pagesToTranslationsMap(ml *Multilingual, pages []*Page) map[string]Translat 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 @@ func assignTranslationsToPages(allTranslations map[string]Translations, pages [] for _, translatedPage := range trans { page.translations = append(page.translations, translatedPage) } - - pageBy(languagePageSort).Sort(page.translations) } }