Fix language params handling

This fixes some issues with language params handling by separating params from configuration values per language.

This means that you can now do this:

```toml
[languages]
[languages.en]
languageName = "English"
weight = 1
title = "My Cool Site"
[languages.en.params]
myParam = "Hi!"
```

This is not a breaking change, but the above is a less suprising way of configuring custom params.

It also fixes some hard-to-debug corner-cases in multilingual sites.

Fixes #4356
Fixes #4352
This commit is contained in:
Bjørn Erik Pedersen
2018-01-30 17:51:18 +01:00
parent feeed073c3
commit ae742cb1bd
4 changed files with 67 additions and 23 deletions

View File

@@ -16,7 +16,6 @@ package helpers
import (
"sort"
"strings"
"sync"
"github.com/gohugoio/hugo/config"
"github.com/spf13/cast"
@@ -42,9 +41,16 @@ type Language struct {
Title string
Weight int
Cfg config.Provider
params map[string]interface{}
paramsInit sync.Once
Cfg config.Provider
// These are params declared in the [params] section of the language merged with the
// site's params, the most specific (language) wins on duplicate keys.
params map[string]interface{}
// These are config values, i.e. the settings declared outside of the [params] section of the language.
// This is the map Hugo looks in when looking for configuration values (baseURL etc.).
// Values in this map can also be fetched from the params map above.
settings map[string]interface{}
}
func (l *Language) String() string {
@@ -53,15 +59,14 @@ func (l *Language) String() string {
// NewLanguage creates a new language.
func NewLanguage(lang string, cfg config.Provider) *Language {
// Note that language specific params will be overridden later.
// We should improve that, but we need to make a copy:
params := make(map[string]interface{})
// Merge with global config.
globalParams := cfg.GetStringMap("params")
for k, v := range globalParams {
if _, ok := params[k]; !ok {
params[k] = v
}
for k, v := range cfg.GetStringMap("params") {
params[k] = v
}
l := &Language{Lang: lang, Cfg: cfg, params: params}
ToLowerMap(params)
l := &Language{Lang: lang, Cfg: cfg, params: params, settings: make(map[string]interface{})}
return l
}
@@ -115,7 +120,7 @@ func (l Languages) IsMultihost() bool {
return false
}
// SetParam sets param with the given key and value.
// SetParam sets a param with the given key and value.
// SetParam is case-insensitive.
func (l *Language) SetParam(k string, v interface{}) {
l.params[strings.ToLower(k)] = v
@@ -166,7 +171,7 @@ func (l *Language) GetLocal(key string) interface{} {
}
key = strings.ToLower(key)
if !globalOnlySettings[key] {
if v, ok := l.params[key]; ok {
if v, ok := l.settings[key]; ok {
return v
}
}
@@ -179,7 +184,7 @@ func (l *Language) Set(key string, value interface{}) {
panic("language not set")
}
key = strings.ToLower(key)
l.params[key] = value
l.settings[key] = value
}
// IsSet checks whether the key is set in the language or the related config store.
@@ -188,7 +193,7 @@ func (l *Language) IsSet(key string) bool {
key = strings.ToLower(key)
if !globalOnlySettings[key] {
if _, ok := l.params[key]; ok {
if _, ok := l.settings[key]; ok {
return true
}
}

View File

@@ -23,11 +23,24 @@ import (
func TestGetGlobalOnlySetting(t *testing.T) {
v := viper.New()
lang := NewDefaultLanguage(v)
lang.SetParam("defaultContentLanguageInSubdir", false)
lang.SetParam("paginatePath", "side")
lang.Set("defaultContentLanguageInSubdir", false)
lang.Set("paginatePath", "side")
v.Set("defaultContentLanguageInSubdir", true)
v.Set("paginatePath", "page")
require.True(t, lang.GetBool("defaultContentLanguageInSubdir"))
require.Equal(t, "side", lang.GetString("paginatePath"))
}
func TestLanguageParams(t *testing.T) {
assert := require.New(t)
v := viper.New()
v.Set("p1", "p1cfg")
lang := NewDefaultLanguage(v)
lang.SetParam("p1", "p1p")
assert.Equal("p1p", lang.Params()["p1"])
assert.Equal("p1cfg", lang.Get("p1"))
}