hugolib: Fix .IsTranslated with identical filenames
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 17 Nov 2017 15:28:35 +0000 (16:28 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 17 Nov 2017 18:46:32 +0000 (19:46 +0100)
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

hugolib/page.go
hugolib/page_test.go
hugolib/translations.go

index 7c72fcb995f47741b55a7a86b36b86c7869a723c..d3f7c53df78d47e41601a05546ce1b61c6673b7e 100644 (file)
@@ -105,6 +105,10 @@ type Page struct {
        // 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 @@ func (p *Page) Translations() Pages {
        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 @@ func (p *Page) update(f interface{}) error {
                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) {
index 4e369e24114e3480e2003072e8ec967a15e0e48e..f3a917acc837397a0a8e454a4e5ed027469bf722 100644 (file)
@@ -1440,6 +1440,29 @@ func TestKind(t *testing.T) {
 
 }
 
+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"
index 53272ee14280f75a3955875145d13a6e9cac9672..2682363f0033a162d217e71e147134c81a1d9a5d 100644 (file)
 
 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 @@ func pagesToTranslationsMap(pages []*Page) map[string]Translations {
        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 @@ func pagesToTranslationsMap(pages []*Page) map[string]Translations {
        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