2016-05-14 00:35:16 -04:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2016-07-24 07:58:27 -04:00
|
|
|
"fmt"
|
2016-05-14 00:35:16 -04:00
|
|
|
"sort"
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
"strings"
|
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
"github.com/spf13/cast"
|
2016-07-24 07:58:27 -04:00
|
|
|
"github.com/spf13/hugo/hugolib"
|
2016-05-14 00:35:16 -04:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2016-07-27 04:03:45 -04:00
|
|
|
func readMultilingualConfiguration() (hugolib.HugoSites, error) {
|
|
|
|
h := make(hugolib.HugoSites, 0)
|
2016-05-14 00:35:16 -04:00
|
|
|
multilingual := viper.GetStringMap("Multilingual")
|
|
|
|
if len(multilingual) == 0 {
|
2016-07-24 07:58:27 -04:00
|
|
|
// TODO(bep) multilingo langConfigsList = append(langConfigsList, hugolib.NewLanguage("en"))
|
2016-07-26 04:24:27 -04:00
|
|
|
h = append(h, hugolib.NewSite(hugolib.NewLanguage("en")))
|
|
|
|
return h, nil
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
var err error
|
|
|
|
|
2016-07-26 04:24:27 -04:00
|
|
|
langConfigsList, err := toSortedLanguages(multilingual)
|
2016-07-24 07:58:27 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2016-07-26 04:24:27 -04:00
|
|
|
return nil, fmt.Errorf("Failed to parse multilingual config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, lang := range langConfigsList {
|
|
|
|
s := hugolib.NewSite(lang)
|
|
|
|
s.SetMultilingualConfig(lang, langConfigsList)
|
|
|
|
h = append(h, s)
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
2016-07-24 07:58:27 -04:00
|
|
|
|
2016-07-26 04:24:27 -04:00
|
|
|
return h, nil
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
func toSortedLanguages(l map[string]interface{}) (hugolib.Languages, error) {
|
|
|
|
langs := make(hugolib.Languages, len(l))
|
2016-07-25 16:22:09 -04:00
|
|
|
i := 0
|
2016-07-24 07:58:27 -04:00
|
|
|
|
|
|
|
for lang, langConf := range l {
|
|
|
|
langsMap, ok := langConf.(map[string]interface{})
|
2016-05-14 00:35:16 -04:00
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Language config is not a map: %v", langsMap)
|
|
|
|
}
|
2016-05-14 00:35:16 -04:00
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
language := hugolib.NewLanguage(lang)
|
|
|
|
|
|
|
|
for k, v := range langsMap {
|
|
|
|
loki := strings.ToLower(k)
|
|
|
|
switch loki {
|
|
|
|
case "title":
|
|
|
|
language.Title = cast.ToString(v)
|
|
|
|
case "weight":
|
|
|
|
language.Weight = cast.ToInt(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put all into the Params map
|
|
|
|
// TODO(bep) reconsile with the type handling etc. from other params handlers.
|
|
|
|
language.SetParam(loki, v)
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:22:09 -04:00
|
|
|
langs[i] = language
|
|
|
|
i++
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
2016-07-24 07:58:27 -04:00
|
|
|
|
|
|
|
sort.Sort(langs)
|
|
|
|
|
|
|
|
return langs, nil
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|