shithub: hugo

Download patch

ref: b3daa1f4bf1b84bcc5da028257ba609be74e3ecc
parent: df1677a6e8c0716527b7121d47c3c75058124090
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Fri Nov 17 11:28:35 EST 2017

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

--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -105,6 +105,10 @@
 	// if available.
 	translations Pages
 
+	// A key that maps to translation(s) of this page. This value is fetched
+	// from the page front matter.
+	translationKey string
+
 	// Params contains configuration defined in the params section of page frontmatter.
 	Params map[string]interface{}
 
@@ -880,6 +884,22 @@
 	return translations
 }
 
+// TranslationKey returns the key used to map language translations of this page.
+// It will use the translationKey set in front matter if set, or the content path and
+// filename (excluding any language code and extension), e.g. "about/index".
+// The Page Kind is always prepended.
+func (p *Page) TranslationKey() string {
+	if p.translationKey != "" {
+		return p.Kind + "/" + p.translationKey
+	}
+
+	if p.IsNode() {
+		return path.Join(p.Kind, path.Join(p.sections...), p.TranslationBaseName())
+	}
+
+	return path.Join(p.Kind, filepath.ToSlash(p.Dir()), p.TranslationBaseName())
+}
+
 func (p *Page) LinkTitle() string {
 	if len(p.linkTitle) > 0 {
 		return p.linkTitle
@@ -1094,6 +1114,9 @@
 		case "iscjklanguage":
 			isCJKLanguage = new(bool)
 			*isCJKLanguage = cast.ToBool(v)
+		case "translationkey":
+			p.translationKey = cast.ToString(v)
+			p.Params[loki] = p.translationKey
 		default:
 			// If not one of the explicit values, store in Params
 			switch vv := v.(type) {
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -1440,6 +1440,29 @@
 
 }
 
+func TestTranslationKey(t *testing.T) {
+	t.Parallel()
+	assert := require.New(t)
+	cfg, fs := newTestCfg()
+
+	writeSource(t, fs, filepath.Join("content", filepath.FromSlash("sect/simple.no.md")), "---\ntitle: \"A1\"\ntranslationKey: \"k1\"\n---\nContent\n")
+	writeSource(t, fs, filepath.Join("content", filepath.FromSlash("sect/simple.en.md")), "---\ntitle: \"A2\"\n---\nContent\n")
+
+	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
+
+	require.Len(t, s.RegularPages, 2)
+
+	home, _ := s.Info.Home()
+	assert.NotNil(home)
+	assert.Equal("home", home.TranslationKey())
+	assert.Equal("page/k1", s.RegularPages[0].TranslationKey())
+	p2 := s.RegularPages[1]
+
+	// This is a single language setup
+	assert.Equal("page/sect/simple.en", p2.TranslationKey())
+
+}
+
 func TestChompBOM(t *testing.T) {
 	t.Parallel()
 	const utf8BOM = "\xef\xbb\xbf"
--- a/hugolib/translations.go
+++ b/hugolib/translations.go
@@ -13,10 +13,6 @@
 
 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.
@@ -26,7 +22,7 @@
 	out := make(map[string]Translations)
 
 	for _, page := range pages {
-		base := createTranslationKey(page)
+		base := page.TranslationKey()
 
 		pageTranslation, present := out[base]
 		if !present {
@@ -45,22 +41,10 @@
 	return out
 }
 
-func createTranslationKey(p *Page) string {
-	base := p.TranslationBaseName()
-
-	if p.IsNode() {
-		// TODO(bep) see https://github.com/gohugoio/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 := createTranslationKey(page)
+		base := page.TranslationKey()
 		trans, exist := allTranslations[base]
 		if !exist {
 			continue
--