summaryrefslogtreecommitdiffstats
path: root/commands/multilingual.go
blob: 4d0f6e1076920e7755ba783866aeced6afa6c91f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package commands

import (
	"fmt"
	"sort"

	"strings"

	"github.com/spf13/cast"
	"github.com/spf13/hugo/hugolib"
	"github.com/spf13/viper"
)

func readMultilingualConfiguration() (*hugolib.HugoSites, error) {
	sites := make([]*hugolib.Site, 0)
	multilingual := viper.GetStringMap("Multilingual")
	if len(multilingual) == 0 {
		// TODO(bep) multilingo langConfigsList = append(langConfigsList, hugolib.NewLanguage("en"))
		sites = append(sites, hugolib.NewSite(hugolib.NewLanguage("en")))
	}

	if len(multilingual) > 0 {
		var err error

		languages, err := toSortedLanguages(multilingual)

		if err != nil {
			return nil, fmt.Errorf("Failed to parse multilingual config: %s", err)
		}

		for _, lang := range languages {
			sites = append(sites, hugolib.NewSite(lang))
		}

	}

	return hugolib.NewHugoSites(sites...)

}

func toSortedLanguages(l map[string]interface{}) (hugolib.Languages, error) {
	langs := make(hugolib.Languages, len(l))
	i := 0

	for lang, langConf := range l {
		langsMap, ok := langConf.(map[string]interface{})

		if !ok {
			return nil, fmt.Errorf("Language config is not a map: %v", langsMap)
		}

		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[i] = language
		i++
	}

	sort.Sort(langs)

	return langs, nil
}