// 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{}
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
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) {
}
+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"
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.
out := make(map[string]Translations)
for _, page := range pages {
- base := createTranslationKey(page)
+ base := page.TranslationKey()
pageTranslation, present := out[base]
if !present {
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