mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Fix YAML loading of multilingual config
And some other minor fixes from code review. Updates #2309
This commit is contained in:
parent
446e606a09
commit
d8a256c155
5 changed files with 74 additions and 12 deletions
|
@ -183,12 +183,13 @@ func AbsURL(in string, addLanguage bool) string {
|
|||
}
|
||||
|
||||
func getLanguagePrefix() string {
|
||||
defaultLang := viper.GetString("DefaultContentLanguage")
|
||||
defaultInSubDir := viper.GetBool("DefaultContentLanguageInSubdir")
|
||||
|
||||
if !viper.GetBool("Multilingual") {
|
||||
return ""
|
||||
}
|
||||
|
||||
defaultLang := viper.GetString("DefaultContentLanguage")
|
||||
defaultInSubDir := viper.GetBool("DefaultContentLanguageInSubdir")
|
||||
|
||||
currentLang := viper.Get("CurrentContentLanguage").(*Language).Lang
|
||||
if currentLang == "" || (currentLang == defaultLang && !defaultInSubDir) {
|
||||
return ""
|
||||
|
|
|
@ -385,9 +385,9 @@ func (h *HugoSites) setupTranslations(master *Site) {
|
|||
}
|
||||
}
|
||||
|
||||
// preRender performs build tasks that needs to be done as late as possible.
|
||||
// preRender performs build tasks that need to be done as late as possible.
|
||||
// Shortcode handling is the main task in here.
|
||||
// TODO(bep) We need to look at the whole handler-chain construct witht he below in mind.
|
||||
// TODO(bep) We need to look at the whole handler-chain construct with he below in mind.
|
||||
func (h *HugoSites) preRender() error {
|
||||
|
||||
for _, s := range h.Sites {
|
||||
|
|
|
@ -160,9 +160,22 @@ func assertFileContent(t *testing.T, filename string, defaultInSubDir bool, matc
|
|||
}
|
||||
}
|
||||
|
||||
//
|
||||
func TestMultiSitesBuild(t *testing.T) {
|
||||
for _, config := range []struct {
|
||||
content string
|
||||
suffix string
|
||||
}{
|
||||
{multiSiteTomlConfig, "toml"},
|
||||
{multiSiteYAMLConfig, "yml"},
|
||||
} {
|
||||
doTestMultiSitesBuild(t, config.content, config.suffix)
|
||||
}
|
||||
}
|
||||
|
||||
func doTestMultiSitesBuild(t *testing.T, configContent, configSuffix string) {
|
||||
testCommonResetState()
|
||||
sites := createMultiTestSites(t, multiSiteTomlConfig)
|
||||
sites := createMultiTestSitesForConfig(t, configContent, configSuffix)
|
||||
|
||||
err := sites.Build(BuildCfg{})
|
||||
|
||||
|
@ -598,7 +611,54 @@ title = "Bokmål"
|
|||
lag = "lag"
|
||||
`
|
||||
|
||||
var multiSiteYAMLConfig = `
|
||||
DefaultExtension: "html"
|
||||
baseurl: "http://example.com/blog"
|
||||
DisableSitemap: false
|
||||
DisableRSS: false
|
||||
RSSUri: "index.xml"
|
||||
|
||||
paginate: 1
|
||||
DefaultContentLanguage: "fr"
|
||||
|
||||
permalinks:
|
||||
other: "/somewhere/else/:filename"
|
||||
|
||||
blackfriday:
|
||||
angledQuotes: true
|
||||
|
||||
Taxonomies:
|
||||
tag: "tags"
|
||||
|
||||
Languages:
|
||||
en:
|
||||
weight: 10
|
||||
title: "English"
|
||||
blackfriday:
|
||||
angledQuotes: false
|
||||
fr:
|
||||
weight: 20
|
||||
title: "Français"
|
||||
Taxonomies:
|
||||
plaque: "plaques"
|
||||
nn:
|
||||
weight: 30
|
||||
title: "Nynorsk"
|
||||
Taxonomies:
|
||||
lag: "lag"
|
||||
nb:
|
||||
weight: 40
|
||||
title: "Bokmål"
|
||||
Taxonomies:
|
||||
lag: "lag"
|
||||
|
||||
`
|
||||
|
||||
func createMultiTestSites(t *testing.T, tomlConfig string) *HugoSites {
|
||||
return createMultiTestSitesForConfig(t, tomlConfig, "toml")
|
||||
}
|
||||
|
||||
func createMultiTestSitesForConfig(t *testing.T, configContent, configSuffix string) *HugoSites {
|
||||
|
||||
// Add some layouts
|
||||
if err := afero.WriteFile(hugofs.Source(),
|
||||
|
@ -760,8 +820,9 @@ lag:
|
|||
`)},
|
||||
}
|
||||
|
||||
writeSource(t, "multilangconfig.toml", tomlConfig)
|
||||
if err := LoadGlobalConfig("", "multilangconfig.toml"); err != nil {
|
||||
configFile := "multilangconfig." + configSuffix
|
||||
writeSource(t, configFile, configContent)
|
||||
if err := LoadGlobalConfig("", configFile); err != nil {
|
||||
t.Fatalf("Failed to load config: %s", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -83,10 +83,10 @@ func toSortedLanguages(l map[string]interface{}) (helpers.Languages, error) {
|
|||
i := 0
|
||||
|
||||
for lang, langConf := range l {
|
||||
langsMap, ok := langConf.(map[string]interface{})
|
||||
langsMap, err := cast.ToStringMapE(langConf)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Language config is not a map: %v", langsMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Language config is not a map: %T", langConf)
|
||||
}
|
||||
|
||||
language := helpers.NewLanguage(lang)
|
||||
|
|
|
@ -777,7 +777,7 @@ func (s *Site) setupPrevNext() {
|
|||
}
|
||||
|
||||
func (s *Site) render() (err error) {
|
||||
// There are sadly some global template funcs etc. that needs the language information.
|
||||
// There are sadly some global template funcs etc. that need the language information.
|
||||
viper.Set("Multilingual", s.multilingualEnabled())
|
||||
viper.Set("CurrentContentLanguage", s.Language)
|
||||
if err = tpl.SetTranslateLang(s.Language.Lang); err != nil {
|
||||
|
|
Loading…
Reference in a new issue