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
This commit is contained in:
Bjørn Erik Pedersen 2017-11-17 16:28:35 +01:00
parent df1677a6e8
commit b3daa1f4bf
3 changed files with 48 additions and 18 deletions

View 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) {

View 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"

View File

@ -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