Skip to content

Commit b3daa1f

Browse files
committed
hugolib: Fix .IsTranslated with identical filenames
This commit refines the key used to map translations: * Use `translationKey` set in front matter * Fall back to path + base filename (i.e. the filename without extension and language code) Note that the Page Kinde will be prepended to both cases above. It does not make sense to have a section as translation for the home page. Fixes #2699
1 parent df1677a commit b3daa1f

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

‎hugolib/page.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ type Page struct {
105105
// if available.
106106
translations Pages
107107

108+
// A key that maps to translation(s) of this page. This value is fetched
109+
// from the page front matter.
110+
translationKey string
111+
108112
// Params contains configuration defined in the params section of page frontmatter.
109113
Params map[string]interface{}
110114

@@ -880,6 +884,22 @@ func (p *Page) Translations() Pages {
880884
return translations
881885
}
882886

887+
// TranslationKey returns the key used to map language translations of this page.
888+
// It will use the translationKey set in front matter if set, or the content path and
889+
// filename (excluding any language code and extension), e.g. "about/index".
890+
// The Page Kind is always prepended.
891+
func (p *Page) TranslationKey() string {
892+
if p.translationKey != "" {
893+
return p.Kind + "/" + p.translationKey
894+
}
895+
896+
if p.IsNode() {
897+
return path.Join(p.Kind, path.Join(p.sections...), p.TranslationBaseName())
898+
}
899+
900+
return path.Join(p.Kind, filepath.ToSlash(p.Dir()), p.TranslationBaseName())
901+
}
902+
883903
func (p *Page) LinkTitle() string {
884904
if len(p.linkTitle) > 0 {
885905
return p.linkTitle
@@ -1094,6 +1114,9 @@ func (p *Page) update(f interface{}) error {
10941114
case "iscjklanguage":
10951115
isCJKLanguage = new(bool)
10961116
*isCJKLanguage = cast.ToBool(v)
1117+
case "translationkey":
1118+
p.translationKey = cast.ToString(v)
1119+
p.Params[loki] = p.translationKey
10971120
default:
10981121
// If not one of the explicit values, store in Params
10991122
switch vv := v.(type) {

‎hugolib/page_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,29 @@ func TestKind(t *testing.T) {
14401440

14411441
}
14421442

1443+
func TestTranslationKey(t *testing.T) {
1444+
t.Parallel()
1445+
assert := require.New(t)
1446+
cfg, fs := newTestCfg()
1447+
1448+
writeSource(t, fs, filepath.Join("content", filepath.FromSlash("sect/simple.no.md")), "---\ntitle: \"A1\"\ntranslationKey: \"k1\"\n---\nContent\n")
1449+
writeSource(t, fs, filepath.Join("content", filepath.FromSlash("sect/simple.en.md")), "---\ntitle: \"A2\"\n---\nContent\n")
1450+
1451+
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
1452+
1453+
require.Len(t, s.RegularPages, 2)
1454+
1455+
home, _ := s.Info.Home()
1456+
assert.NotNil(home)
1457+
assert.Equal("home", home.TranslationKey())
1458+
assert.Equal("page/k1", s.RegularPages[0].TranslationKey())
1459+
p2 := s.RegularPages[1]
1460+
1461+
// This is a single language setup
1462+
assert.Equal("page/sect/simple.en", p2.TranslationKey())
1463+
1464+
}
1465+
14431466
func TestChompBOM(t *testing.T) {
14441467
t.Parallel()
14451468
const utf8BOM = "\xef\xbb\xbf"

‎hugolib/translations.go‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313

1414
package hugolib
1515

16-
import (
17-
"fmt"
18-
)
19-
2016
// Translations represent the other translations for a given page. The
2117
// string here is the language code, as affected by the `post.LANG.md`
2218
// filename.
@@ -26,7 +22,7 @@ func pagesToTranslationsMap(pages []*Page) map[string]Translations {
2622
out := make(map[string]Translations)
2723

2824
for _, page := range pages {
29-
base := createTranslationKey(page)
25+
base := page.TranslationKey()
3026

3127
pageTranslation, present := out[base]
3228
if !present {
@@ -45,22 +41,10 @@ func pagesToTranslationsMap(pages []*Page) map[string]Translations {
4541
return out
4642
}
4743

48-
func createTranslationKey(p *Page) string {
49-
base := p.TranslationBaseName()
50-
51-
if p.IsNode() {
52-
// TODO(bep) see https://github.com/gohugoio/hugo/issues/2699
53-
// Must prepend the section and kind to the key to make it unique
54-
base = fmt.Sprintf("%s/%s/%s", p.Kind, p.sections, base)
55-
}
56-
57-
return base
58-
}
59-
6044
func assignTranslationsToPages(allTranslations map[string]Translations, pages []*Page) {
6145
for _, page := range pages {
6246
page.translations = page.translations[:0]
63-
base := createTranslationKey(page)
47+
base := page.TranslationKey()
6448
trans, exist := allTranslations[base]
6549
if !exist {
6650
continue

0 commit comments

Comments
 (0)