Skip to content

Commit 9409bc0

Browse files
committed
Improve .Site.GetPage for regular translated pages
You can still use the full path with extensions, but to get the current language version: * If the content file lives in `/content/blog/mypost.en.md` * Use `.Site.GetPage "page" "blog/mypost"` Fixes #4285
1 parent 4eb1650 commit 9409bc0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

‎hugolib/hugo_sites_build_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,20 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
299299
require.Equal(t, "fr", frenchPage.Lang())
300300
}
301301

302+
// See https://github.com/gohugoio/hugo/issues/4285
303+
// Before Hugo 0.33 you had to be explicit with the content path to get the correct Page, which
304+
// isn't ideal in a multilingual setup. You want a way to get the current language version if available.
305+
// Now you can do lookups with translation base name to get that behaviour.
306+
// Let us test all the regular page variants:
307+
getPageDoc1En := enSite.getPage(KindPage, filepath.ToSlash(doc1en.Path()))
308+
getPageDoc1EnBase := enSite.getPage(KindPage, "sect/doc1")
309+
getPageDoc1Fr := frSite.getPage(KindPage, filepath.ToSlash(doc1fr.Path()))
310+
getPageDoc1FrBase := frSite.getPage(KindPage, "sect/doc1")
311+
require.Equal(t, doc1en, getPageDoc1En)
312+
require.Equal(t, doc1fr, getPageDoc1Fr)
313+
require.Equal(t, doc1en, getPageDoc1EnBase)
314+
require.Equal(t, doc1fr, getPageDoc1FrBase)
315+
302316
// Check redirect to main language, French
303317
languageRedirect := readDestination(t, fs, "public/index.html")
304318
require.True(t, strings.Contains(languageRedirect, "0; url=http://example.com/blog/fr"), languageRedirect)
@@ -683,6 +697,7 @@ title = "Svenska"
683697
// Veriy Swedish site
684698
require.Len(t, svSite.RegularPages, 1)
685699
svPage := svSite.RegularPages[0]
700+
686701
require.Equal(t, "Swedish Contentfile", svPage.title)
687702
require.Equal(t, "sv", svPage.Lang())
688703
require.Len(t, svPage.Translations(), 2)

‎hugolib/page_collections.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ func (c *PageCollections) refreshPageCaches() {
5151
c.RegularPages = c.findPagesByKindIn(KindPage, c.Pages)
5252
c.AllRegularPages = c.findPagesByKindIn(KindPage, c.AllPages)
5353

54+
var s *Site
55+
56+
if len(c.Pages) > 0 {
57+
s = c.Pages[0].s
58+
}
59+
5460
cacheLoader := func(kind string) func() (map[string]interface{}, error) {
5561
return func() (map[string]interface{}, error) {
5662
cache := make(map[string]interface{})
@@ -64,6 +70,13 @@ func (c *PageCollections) refreshPageCaches() {
6470
cache[filepath.ToSlash(p.Source.Path())] = p
6571
// Ref/Relref supports this potentially ambiguous lookup.
6672
cache[p.Source.LogicalName()] = p
73+
74+
if s != nil && p.s == s {
75+
// We need a way to get to the current language version.
76+
pathWithNoExtensions := path.Join(filepath.ToSlash(p.Source.Dir()), p.Source.TranslationBaseName())
77+
cache[pathWithNoExtensions] = p
78+
}
79+
6780
}
6881
default:
6982
for _, p := range c.indexPages {

0 commit comments

Comments
 (0)