diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-11-02 08:25:20 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-11-17 11:01:46 +0100 |
commit | 2e0465764b5dacc511b977b1c9aa07324ad0ee9c (patch) | |
tree | ae5a32eb9340e4c0e78e063228821edc29037631 /hugolib | |
parent | 6233ddf9d19b51f69c0c4a796d88732d1700e585 (diff) |
Add multilingual multihost support
This commit adds multihost support when more than one language is configured and `baseURL` is set per language.
Updates #4027
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/config.go | 28 | ||||
-rw-r--r-- | hugolib/hugo_sites.go | 74 | ||||
-rw-r--r-- | hugolib/hugo_sites_build_test.go | 2 | ||||
-rw-r--r-- | hugolib/hugo_sites_multihost_test.go | 72 | ||||
-rw-r--r-- | hugolib/multilingual.go | 8 | ||||
-rw-r--r-- | hugolib/page.go | 5 | ||||
-rw-r--r-- | hugolib/page_paths.go | 4 | ||||
-rw-r--r-- | hugolib/site_render.go | 2 |
8 files changed, 162 insertions, 33 deletions
diff --git a/hugolib/config.go b/hugolib/config.go index acfa0704d..db59253cd 100644 --- a/hugolib/config.go +++ b/hugolib/config.go @@ -19,6 +19,7 @@ import ( "io" "strings" + "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/helpers" "github.com/spf13/afero" "github.com/spf13/viper" @@ -80,11 +81,34 @@ func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper. helpers.Deprecated("site config", "disableRobotsTXT", "Use disableKinds= [\"robotsTXT\"]", false) } - loadDefaultSettingsFor(v) + if err := loadDefaultSettingsFor(v); err != nil { + return v, err + } return v, nil } +func loadLanguageSettings(cfg config.Provider) error { + multilingual := cfg.GetStringMap("languages") + var ( + langs helpers.Languages + err error + ) + + if len(multilingual) == 0 { + langs = append(langs, helpers.NewDefaultLanguage(cfg)) + } else { + langs, err = toSortedLanguages(cfg, multilingual) + if err != nil { + return fmt.Errorf("Failed to parse multilingual config: %s", err) + } + } + + cfg.Set("languagesSorted", langs) + + return nil +} + func loadDefaultSettingsFor(v *viper.Viper) error { c, err := helpers.NewContentSpec(v) @@ -154,5 +178,5 @@ func loadDefaultSettingsFor(v *viper.Viper) error { v.SetDefault("debug", false) v.SetDefault("disableFastRender", false) - return nil + return loadLanguageSettings(v) } diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 6e2340903..e0697507b 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -15,7 +15,6 @@ package hugolib import ( "errors" - "fmt" "strings" "sync" @@ -37,9 +36,16 @@ type HugoSites struct { multilingual *Multilingual + // Multihost is set if multilingual and baseURL set on the language level. + multihost bool + *deps.Deps } +func (h *HugoSites) IsMultihost() bool { + return h != nil && h.multihost +} + // GetContentPage finds a Page with content given the absolute filename. // Returns nil if none found. func (h *HugoSites) GetContentPage(filename string) *Page { @@ -92,6 +98,31 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) { h.Deps = sites[0].Deps + // The baseURL may be provided at the language level. If that is true, + // then every language must have a baseURL. In this case we always render + // to a language sub folder, which is then stripped from all the Permalink URLs etc. + var baseURLFromLang bool + + for _, s := range sites { + burl := s.Language.GetLocal("baseURL") + if baseURLFromLang && burl == nil { + return h, errors.New("baseURL must be set on all or none of the languages") + } + + if burl != nil { + baseURLFromLang = true + } + } + + if baseURLFromLang { + for _, s := range sites { + // TODO(bep) multihost check + s.Info.defaultContentLanguageInSubdir = true + s.Cfg.Set("defaultContentLanguageInSubdir", true) + } + h.multihost = true + } + return h, nil } @@ -180,39 +211,19 @@ func createSitesFromConfig(cfg deps.DepsCfg) ([]*Site, error) { sites []*Site ) - multilingual := cfg.Cfg.GetStringMap("languages") - - if len(multilingual) == 0 { - l := helpers.NewDefaultLanguage(cfg.Cfg) - cfg.Language = l - s, err := newSite(cfg) - if err != nil { - return nil, err - } - sites = append(sites, s) - } + languages := getLanguages(cfg.Cfg) - if len(multilingual) > 0 { + for _, lang := range languages { + var s *Site var err error - - languages, err := toSortedLanguages(cfg.Cfg, multilingual) + cfg.Language = lang + s, err = newSite(cfg) if err != nil { - return nil, fmt.Errorf("Failed to parse multilingual config: %s", err) + return nil, err } - for _, lang := range languages { - var s *Site - var err error - cfg.Language = lang - s, err = newSite(cfg) - - if err != nil { - return nil, err - } - - sites = append(sites, s) - } + sites = append(sites, s) } return sites, nil @@ -227,7 +238,12 @@ func (h *HugoSites) reset() { func (h *HugoSites) createSitesFromConfig() error { + if err := loadLanguageSettings(h.Cfg); err != nil { + return err + } + depsCfg := deps.DepsCfg{Fs: h.Fs, Cfg: h.Cfg} + sites, err := createSitesFromConfig(depsCfg) if err != nil { @@ -286,7 +302,7 @@ type BuildCfg struct { func (h *HugoSites) renderCrossSitesArtifacts() error { - if !h.multilingual.enabled() { + if !h.multilingual.enabled() || h.IsMultihost() { return nil } diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go index 96e2c66b2..079f0fcfa 100644 --- a/hugolib/hugo_sites_build_test.go +++ b/hugolib/hugo_sites_build_test.go @@ -1269,7 +1269,7 @@ lag: t.Fatalf("Failed to create sites: %s", err) } - if len(sites.Sites) != 4 { + if len(sites.Sites) == 0 { t.Fatalf("Got %d sites", len(sites.Sites)) } diff --git a/hugolib/hugo_sites_multihost_test.go b/hugolib/hugo_sites_multihost_test.go new file mode 100644 index 000000000..864d52c71 --- /dev/null +++ b/hugolib/hugo_sites_multihost_test.go @@ -0,0 +1,72 @@ +package hugolib + +import ( + "testing" + + "github.com/spf13/afero" + "github.com/stretchr/testify/require" +) + +func TestMultihosts(t *testing.T) { + t.Parallel() + + var multiSiteTOMLConfigTemplate = ` +paginate = 1 +disablePathToLower = true +defaultContentLanguage = "{{ .DefaultContentLanguage }}" +defaultContentLanguageInSubdir = {{ .DefaultContentLanguageInSubdir }} + +[permalinks] +other = "/somewhere/else/:filename" + +[Taxonomies] +tag = "tags" + +[Languages] +[Languages.en] +baseURL = "https://example.com" +weight = 10 +title = "In English" +languageName = "English" + +[Languages.fr] +baseURL = "https://example.fr" +weight = 20 +title = "Le Français" +languageName = "Français" + +[Languages.nn] +baseURL = "https://example.no" +weight = 30 +title = "På nynorsk" +languageName = "Nynorsk" + +` + + siteConfig := testSiteConfig{Fs: afero.NewMemMapFs(), DefaultContentLanguage: "fr", DefaultContentLanguageInSubdir: false} + sites := createMultiTestSites(t, siteConfig, multiSiteTOMLConfigTemplate) + fs := sites.Fs + cfg := BuildCfg{Watching: true} + th := testHelper{sites.Cfg, fs, t} + assert := require.New(t) + + err := sites.Build(cfg) + assert.NoError(err) + + th.assertFileContent("public/en/sect/doc1-slug/index.html", "Hello") + + s1 := sites.Sites[0] + + s1h := s1.getPage(KindHome) + assert.True(s1h.IsTranslated()) + assert.Len(s1h.Translations(), 2) + assert.Equal("https://example.com/", s1h.Permalink()) + + s2 := sites.Sites[1] + s2h := s2.getPage(KindHome) + assert.Equal("https://example.fr/", s2h.Permalink()) + + th.assertFileContentStraight("public/fr/index.html", "French Home Page") + th.assertFileContentStraight("public/en/index.html", "Default Home Page") + +} diff --git a/hugolib/multilingual.go b/hugolib/multilingual.go index e0245fa2b..589df66e0 100644 --- a/hugolib/multilingual.go +++ b/hugolib/multilingual.go @@ -47,6 +47,14 @@ func (ml *Multilingual) Language(lang string) *helpers.Language { return ml.langMap[lang] } +func getLanguages(cfg config.Provider) helpers.Languages { + if cfg.IsSet("languagesSorted") { + return cfg.Get("languagesSorted").(helpers.Languages) + } + + return helpers.Languages{helpers.NewDefaultLanguage(cfg)} +} + func newMultiLingualFromSites(cfg config.Provider, sites ...*Site) (*Multilingual, error) { languages := make(helpers.Languages, len(sites)) diff --git a/hugolib/page.go b/hugolib/page.go index d928b92f9..7da77f192 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -1754,6 +1754,11 @@ func (p *Page) shouldAddLanguagePrefix() bool { return false } + if p.s.owner.IsMultihost() { + // TODO(bep) multihost check vs lang below + return true + } + if p.Lang() == "" { return false } diff --git a/hugolib/page_paths.go b/hugolib/page_paths.go index e18e0f10e..993ad0780 100644 --- a/hugolib/page_paths.go +++ b/hugolib/page_paths.go @@ -257,6 +257,10 @@ func (p *Page) createRelativePermalinkForOutputFormat(f output.Format) string { tp = strings.TrimSuffix(tp, f.BaseFilename()) } + if p.s.owner.IsMultihost() { + tp = strings.TrimPrefix(tp, helpers.FilePathSeparator+p.s.Info.Language.Lang) + } + return p.s.PathSpec.URLizeFilename(tp) } diff --git a/hugolib/site_render.go b/hugolib/site_render.go index 4118f3eef..b4d688bda 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -387,7 +387,7 @@ func (s *Site) renderAliases() error { } } - if s.owner.multilingual.enabled() { + if s.owner.multilingual.enabled() && !s.owner.IsMultihost() { mainLang := s.owner.multilingual.DefaultLang if s.Info.defaultContentLanguageInSubdir { mainLangURL := s.PathSpec.AbsURL(mainLang.Lang, false) |