From b3daa1f4bf1b84bcc5da028257ba609be74e3ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 17 Nov 2017 16:28:35 +0100 Subject: [PATCH] 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 --- hugolib/page.go | 23 +++++++++++++++++++++++ hugolib/page_test.go | 23 +++++++++++++++++++++++ hugolib/translations.go | 20 ++------------------ 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/hugolib/page.go b/hugolib/page.go index 7c72fcb99..d3f7c53df 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -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) { diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 4e369e241..f3a917acc 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -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" diff --git a/hugolib/translations.go b/hugolib/translations.go index 53272ee14..2682363f0 100644 --- 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 @@ 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