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-24 07:58:27 -04:00
|
|
|
var langConfigsList hugolib.Languages
|
2016-05-14 00:35:16 -04:00
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
func readMultilingualConfiguration() error {
|
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"))
|
|
|
|
return nil
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
langConfigsList, err = toSortedLanguages(multilingual)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse multilingual config: %s", err)
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
2016-07-24 07:58:27 -04:00
|
|
|
|
|
|
|
return 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))
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
langs = append(langs, language)
|
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
|
|
|
}
|