diff --git a/config/config.go b/config/config.go index 8a5ad9f..d1d41ff 100644 --- a/config/config.go +++ b/config/config.go @@ -138,3 +138,9 @@ func (c *Config) SetDefaultValue(fieldDefaultValue func(fieldName string) string setDefaultValue(c.Elements, c.Languages, fieldDefaultValue) } } + +func (c *Config) SetValue(fieldValue func(fieldName string) string) { + if fieldValue != nil { + setValue(c.Elements, c.Languages, fieldValue) + } +} diff --git a/config/utils.go b/config/utils.go index f18fa00..d0ca573 100644 --- a/config/utils.go +++ b/config/utils.go @@ -54,3 +54,25 @@ func setDefaultValue(elements []*Element, languages []*Language, fieldDefaultVal } } } + +func setValue(elements []*Element, languages []*Language, fieldValue func(string) string) { + for _, elem := range elements { + if elem.Type == `langset` { + setValue(elem.Elements, elem.Languages, fieldValue) + continue + } + if elem.Type == `fieldset` { + setValue(elem.Elements, languages, fieldValue) + continue + } + if len(elem.Name) > 0 { + if len(languages) == 0 { + elem.Value = fieldValue(elem.Name) + continue + } + for _, lang := range languages { + elem.Value = fieldValue(lang.Name(elem.Name)) + } + } + } +}