mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
Add support for a content dir set per language
A sample config: ```toml defaultContentLanguage = "en" defaultContentLanguageInSubdir = true [Languages] [Languages.en] weight = 10 title = "In English" languageName = "English" contentDir = "content/english" [Languages.nn] weight = 20 title = "På Norsk" languageName = "Norsk" contentDir = "content/norwegian" ``` The value of `contentDir` can be any valid path, even absolute path references. The only restriction is that the content dirs cannot overlap. The content files will be assigned a language by 1. The placement: `content/norwegian/post/my-post.md` will be read as Norwegian content. 2. The filename: `content/english/post/my-post.nn.md` will be read as Norwegian even if it lives in the English content folder. The content directories will be merged into a big virtual filesystem with one simple rule: The most specific language file will win. This means that if both `content/norwegian/post/my-post.md` and `content/english/post/my-post.nn.md` exists, they will be considered duplicates and the version inside `content/norwegian` will win. Note that translations will be automatically assigned by Hugo by the content file's relative placement, so `content/norwegian/post/my-post.md` will be a translation of `content/english/post/my-post.md`. If this does not work for you, you can connect the translations together by setting a `translationKey` in the content files' front matter. Fixes #4523 Fixes #4552 Fixes #4553
This commit is contained in:
@@ -3,6 +3,7 @@ package hugolib
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -433,7 +434,7 @@ func TestMultiSitesRebuild(t *testing.T) {
|
||||
// t.Parallel() not supported, see https://github.com/fortytw2/leaktest/issues/4
|
||||
// This leaktest seems to be a little bit shaky on Travis.
|
||||
if !isCI() {
|
||||
defer leaktest.CheckTimeout(t, 30*time.Second)()
|
||||
defer leaktest.CheckTimeout(t, 10*time.Second)()
|
||||
}
|
||||
|
||||
assert := require.New(t)
|
||||
@@ -459,6 +460,8 @@ func TestMultiSitesRebuild(t *testing.T) {
|
||||
b.AssertFileContent("public/fr/sect/doc1/index.html", "Single", "Shortcode: Bonjour")
|
||||
b.AssertFileContent("public/en/sect/doc1-slug/index.html", "Single", "Shortcode: Hello")
|
||||
|
||||
contentFs := b.H.BaseFs.ContentFs
|
||||
|
||||
for i, this := range []struct {
|
||||
preFunc func(t *testing.T)
|
||||
events []fsnotify.Event
|
||||
@@ -490,9 +493,9 @@ func TestMultiSitesRebuild(t *testing.T) {
|
||||
},
|
||||
{
|
||||
func(t *testing.T) {
|
||||
writeNewContentFile(t, fs, "new_en_1", "2016-07-31", "content/new1.en.md", -5)
|
||||
writeNewContentFile(t, fs, "new_en_2", "1989-07-30", "content/new2.en.md", -10)
|
||||
writeNewContentFile(t, fs, "new_fr_1", "2016-07-30", "content/new1.fr.md", 10)
|
||||
writeNewContentFile(t, contentFs, "new_en_1", "2016-07-31", "new1.en.md", -5)
|
||||
writeNewContentFile(t, contentFs, "new_en_2", "1989-07-30", "new2.en.md", -10)
|
||||
writeNewContentFile(t, contentFs, "new_fr_1", "2016-07-30", "new1.fr.md", 10)
|
||||
},
|
||||
[]fsnotify.Event{
|
||||
{Name: filepath.FromSlash("content/new1.en.md"), Op: fsnotify.Create},
|
||||
@@ -513,10 +516,10 @@ func TestMultiSitesRebuild(t *testing.T) {
|
||||
},
|
||||
{
|
||||
func(t *testing.T) {
|
||||
p := "content/sect/doc1.en.md"
|
||||
doc1 := readSource(t, fs, p)
|
||||
p := "sect/doc1.en.md"
|
||||
doc1 := readFileFromFs(t, contentFs, p)
|
||||
doc1 += "CHANGED"
|
||||
writeSource(t, fs, p, doc1)
|
||||
writeToFs(t, contentFs, p, doc1)
|
||||
},
|
||||
[]fsnotify.Event{{Name: filepath.FromSlash("content/sect/doc1.en.md"), Op: fsnotify.Write}},
|
||||
func(t *testing.T) {
|
||||
@@ -529,7 +532,7 @@ func TestMultiSitesRebuild(t *testing.T) {
|
||||
// Rename a file
|
||||
{
|
||||
func(t *testing.T) {
|
||||
if err := fs.Source.Rename("content/new1.en.md", "content/new1renamed.en.md"); err != nil {
|
||||
if err := contentFs.Rename("new1.en.md", "new1renamed.en.md"); err != nil {
|
||||
t.Fatalf("Rename failed: %s", err)
|
||||
}
|
||||
},
|
||||
@@ -650,7 +653,7 @@ weight = 15
|
||||
title = "Svenska"
|
||||
`
|
||||
|
||||
writeNewContentFile(t, fs, "Swedish Contentfile", "2016-01-01", "content/sect/doc1.sv.md", 10)
|
||||
writeNewContentFile(t, fs.Source, "Swedish Contentfile", "2016-01-01", "content/sect/doc1.sv.md", 10)
|
||||
// replace the config
|
||||
b.WithNewConfig(newConfig)
|
||||
|
||||
@@ -1038,18 +1041,31 @@ func readFileFromFs(t testing.TB, fs afero.Fs, filename string) string {
|
||||
if err != nil {
|
||||
// Print some debug info
|
||||
root := strings.Split(filename, helpers.FilePathSeparator)[0]
|
||||
afero.Walk(fs, root, func(path string, info os.FileInfo, err error) error {
|
||||
if info != nil && !info.IsDir() {
|
||||
fmt.Println(" ", path)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
printFs(fs, root, os.Stdout)
|
||||
Fatalf(t, "Failed to read file: %s", err)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func printFs(fs afero.Fs, path string, w io.Writer) {
|
||||
if fs == nil {
|
||||
return
|
||||
}
|
||||
afero.Walk(fs, path, func(path string, info os.FileInfo, err error) error {
|
||||
if info != nil && !info.IsDir() {
|
||||
s := path
|
||||
if lang, ok := info.(hugofs.LanguageAnnouncer); ok {
|
||||
s = s + "\tLANG: " + lang.Lang()
|
||||
}
|
||||
if fp, ok := info.(hugofs.FilePather); ok {
|
||||
s = s + "\tRF: " + fp.Filename() + "\tBP: " + fp.BaseDir()
|
||||
}
|
||||
fmt.Fprintln(w, " ", s)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
const testPageTemplate = `---
|
||||
title: "%s"
|
||||
publishdate: "%s"
|
||||
@@ -1062,9 +1078,9 @@ func newTestPage(title, date string, weight int) string {
|
||||
return fmt.Sprintf(testPageTemplate, title, date, weight, title)
|
||||
}
|
||||
|
||||
func writeNewContentFile(t *testing.T, fs *hugofs.Fs, title, date, filename string, weight int) {
|
||||
func writeNewContentFile(t *testing.T, fs afero.Fs, title, date, filename string, weight int) {
|
||||
content := newTestPage(title, date, weight)
|
||||
writeSource(t, fs, filename, content)
|
||||
writeToFs(t, fs, filename, content)
|
||||
}
|
||||
|
||||
type multiSiteTestBuilder struct {
|
||||
|
Reference in New Issue
Block a user