2016-05-14 00:35:16 -04:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2016-07-24 07:58:27 -04:00
|
|
|
"sync"
|
|
|
|
|
2016-07-25 16:22:09 -04:00
|
|
|
"sort"
|
2016-07-24 07:58:27 -04:00
|
|
|
"strings"
|
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
type Language struct {
|
|
|
|
Lang string
|
|
|
|
Title string
|
|
|
|
Weight int
|
|
|
|
params map[string]interface{}
|
|
|
|
paramsInit sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLanguage(lang string) *Language {
|
|
|
|
return &Language{Lang: lang, params: make(map[string]interface{})}
|
|
|
|
}
|
|
|
|
|
2016-07-26 04:24:27 -04:00
|
|
|
// TODO(bep) multilingo
|
|
|
|
func newDefaultLanguage() *Language {
|
|
|
|
return NewLanguage("en")
|
|
|
|
}
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
type Languages []*Language
|
|
|
|
|
2016-07-25 16:22:09 -04:00
|
|
|
func NewLanguages(l ...*Language) Languages {
|
|
|
|
languages := make(Languages, len(l))
|
|
|
|
for i := 0; i < len(l); i++ {
|
|
|
|
languages[i] = l[i]
|
|
|
|
}
|
|
|
|
sort.Sort(languages)
|
|
|
|
return languages
|
|
|
|
}
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
func (l Languages) Len() int { return len(l) }
|
|
|
|
func (l Languages) Less(i, j int) bool { return l[i].Weight < l[j].Weight }
|
|
|
|
func (l Languages) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
type Multilingual struct {
|
2016-07-24 07:58:27 -04:00
|
|
|
Languages Languages
|
2016-07-25 16:22:09 -04:00
|
|
|
|
|
|
|
langMap map[string]*Language
|
|
|
|
langMapInit sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *Multilingual) Language(lang string) *Language {
|
|
|
|
ml.langMapInit.Do(func() {
|
|
|
|
ml.langMap = make(map[string]*Language)
|
|
|
|
for _, l := range ml.Languages {
|
|
|
|
ml.langMap[l.Lang] = l
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return ml.langMap[lang]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *Multilingual) enabled() bool {
|
|
|
|
return len(ml.Languages) > 0
|
2016-07-24 07:58:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Language) Params() map[string]interface{} {
|
|
|
|
l.paramsInit.Do(func() {
|
|
|
|
// Merge with global config.
|
|
|
|
// TODO(bep) consider making this part of a constructor func.
|
|
|
|
globalParams := viper.GetStringMap("Params")
|
|
|
|
for k, v := range globalParams {
|
|
|
|
if _, ok := l.params[k]; !ok {
|
|
|
|
l.params[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return l.params
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
func (l *Language) SetParam(k string, v interface{}) {
|
|
|
|
l.params[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Language) GetString(key string) string { return cast.ToString(l.Get(key)) }
|
|
|
|
func (ml *Language) GetStringMap(key string) map[string]interface{} {
|
2016-05-14 00:35:16 -04:00
|
|
|
return cast.ToStringMap(ml.Get(key))
|
|
|
|
}
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
func (l *Language) GetStringMapString(key string) map[string]string {
|
|
|
|
return cast.ToStringMapString(l.Get(key))
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
|
|
|
|
2016-07-24 07:58:27 -04:00
|
|
|
func (l *Language) Get(key string) interface{} {
|
|
|
|
key = strings.ToLower(key)
|
|
|
|
if v, ok := l.params[key]; ok {
|
|
|
|
return v
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
|
|
|
return viper.Get(key)
|
|
|
|
}
|
|
|
|
|
2016-07-26 04:24:27 -04:00
|
|
|
// TODO(bep) multilingo move this to a constructor.
|
2016-07-24 07:58:27 -04:00
|
|
|
func (s *Site) SetMultilingualConfig(currentLang *Language, languages Languages) {
|
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
ml := &Multilingual{
|
2016-07-24 07:58:27 -04:00
|
|
|
Languages: languages,
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
2016-07-25 16:22:09 -04:00
|
|
|
viper.Set("Multilingual", ml.enabled())
|
2016-05-14 00:35:16 -04:00
|
|
|
s.Multilingual = ml
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) multilingualEnabled() bool {
|
2016-07-25 16:22:09 -04:00
|
|
|
return s.Multilingual != nil && s.Multilingual.enabled()
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
2016-07-24 07:58:27 -04:00
|
|
|
|
2016-07-26 04:24:27 -04:00
|
|
|
// TODO(bep) multilingo remove these
|
|
|
|
func (s *Site) currentLanguageString() string {
|
|
|
|
return s.currentLanguage().Lang
|
2016-07-24 07:58:27 -04:00
|
|
|
}
|
|
|
|
|
2016-07-26 04:24:27 -04:00
|
|
|
func (s *Site) currentLanguage() *Language {
|
|
|
|
return s.Lang
|
2016-07-24 07:58:27 -04:00
|
|
|
}
|