diff options
author | Bjørn Erik Pedersen <[email protected]> | 2016-07-25 22:22:09 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2016-09-06 18:32:15 +0300 |
commit | c4e7c37055a029a26d87ebeb21614efb3f0b0040 (patch) | |
tree | 55c05751d0734ace44783bc0b196873fd889d590 /hugolib/multilingual.go | |
parent | 06d12ab895a83fc8a9f94b23e533b25511bbb6d1 (diff) | |
download | hugo-c4e7c37055a029a26d87ebeb21614efb3f0b0040.tar.gz hugo-c4e7c37055a029a26d87ebeb21614efb3f0b0040.zip |
Add Translations and AllTranslations methods to Page
Will revisit Node later.
Diffstat (limited to 'hugolib/multilingual.go')
-rw-r--r-- | hugolib/multilingual.go | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/hugolib/multilingual.go b/hugolib/multilingual.go index eebd43e3d..becdd5ba1 100644 --- a/hugolib/multilingual.go +++ b/hugolib/multilingual.go @@ -3,6 +3,7 @@ package hugolib import ( "sync" + "sort" "strings" "github.com/spf13/cast" @@ -23,14 +24,38 @@ func NewLanguage(lang string) *Language { type Languages []*Language +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 +} + 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] } type Multilingual struct { - enabled bool - Languages Languages + + 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 } func (l *Language) Params() map[string]interface{} { @@ -73,15 +98,14 @@ func (s *Site) SetMultilingualConfig(currentLang *Language, languages Languages) // TODO(bep) multilingo evaluate viper.Set("CurrentLanguage", currentLang) ml := &Multilingual{ - enabled: len(languages) > 0, Languages: languages, } - viper.Set("Multilingual", ml.enabled) + viper.Set("Multilingual", ml.enabled()) s.Multilingual = ml } func (s *Site) multilingualEnabled() bool { - return s.Multilingual != nil && s.Multilingual.enabled + return s.Multilingual != nil && s.Multilingual.enabled() } func currentLanguageString() string { |