mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
hugolib: Fix IsTranslated for "old" node types
The new logic for creating Page objects from old node types didn't include itself in the translation logic, so `IsTranslated` returned falsely false for sites with only two languages. The `AllTranslations` method also returned too few pages in that case. This commit fixes that. Fixes #2812
This commit is contained in:
parent
d1b89f5c7c
commit
88b5bbd532
3 changed files with 48 additions and 8 deletions
|
@ -180,7 +180,7 @@ func (h *HugoSites) assignMissingTranslations() error {
|
|||
// Assign translations
|
||||
for _, t1 := range nodes {
|
||||
for _, t2 := range nodes {
|
||||
if t2.isNewTranslation(t1) {
|
||||
if t1.isNewTranslation(t2) {
|
||||
t1.translations = append(t1.translations, t2)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -186,6 +186,49 @@ func assertFileContentRegexp(t *testing.T, filename string, defaultInSubDir bool
|
|||
}
|
||||
}
|
||||
|
||||
func TestMultiSitesWithTwoLanguages(t *testing.T) {
|
||||
testCommonResetState()
|
||||
|
||||
viper.Set("defaultContentLanguage", "nn")
|
||||
|
||||
writeSource(t, "config.toml", `
|
||||
[languages]
|
||||
[languages.nn]
|
||||
languageName = "Nynorsk"
|
||||
weight = 1
|
||||
title = "Tittel på Nynorsk"
|
||||
|
||||
[languages.en]
|
||||
title = "Title in English"
|
||||
languageName = "English"
|
||||
weight = 2
|
||||
`,
|
||||
)
|
||||
|
||||
if err := LoadGlobalConfig("", "config.toml"); err != nil {
|
||||
t.Fatalf("Failed to load config: %s", err)
|
||||
}
|
||||
|
||||
// Add some data
|
||||
writeSource(t, "data/hugo.toml", "slogan = \"Hugo Rocks!\"")
|
||||
|
||||
sites, err := NewHugoSitesFromConfiguration()
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create sites: %s", err)
|
||||
}
|
||||
|
||||
require.NoError(t, sites.Build(BuildCfg{}))
|
||||
require.Len(t, sites.Sites, 2)
|
||||
|
||||
nnSite := sites.Sites[0]
|
||||
nnSiteHome := nnSite.getPage(KindHome)
|
||||
require.Len(t, nnSiteHome.AllTranslations(), 2)
|
||||
require.Len(t, nnSiteHome.Translations(), 1)
|
||||
require.True(t, nnSiteHome.IsTranslated())
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
func TestMultiSitesBuild(t *testing.T) {
|
||||
for _, config := range []struct {
|
||||
|
|
|
@ -1626,7 +1626,8 @@ func (p *Page) Lang() string {
|
|||
}
|
||||
|
||||
func (p *Page) isNewTranslation(candidate *Page) bool {
|
||||
if p == candidate || p.Kind != candidate.Kind {
|
||||
|
||||
if p.Kind != candidate.Kind {
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -1634,10 +1635,6 @@ func (p *Page) isNewTranslation(candidate *Page) bool {
|
|||
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) {
|
||||
|
@ -1651,8 +1648,8 @@ func (p *Page) isNewTranslation(candidate *Page) bool {
|
|||
}
|
||||
|
||||
// Finally check that it is not already added.
|
||||
for _, translation := range candidate.translations {
|
||||
if p == translation {
|
||||
for _, translation := range p.translations {
|
||||
if candidate == translation {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue