2016-08-07 20:01:55 +00:00
|
|
|
// Copyright 2016-present The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-05-14 04:35:16 +00:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2016-07-24 11:58:27 +00:00
|
|
|
"sync"
|
|
|
|
|
2016-07-25 20:22:09 +00:00
|
|
|
"sort"
|
2016-07-24 11:58:27 +00:00
|
|
|
|
2016-08-06 12:51:50 +00:00
|
|
|
"errors"
|
2016-08-05 11:10:58 +00:00
|
|
|
"fmt"
|
|
|
|
|
2016-05-14 04:35:16 +00:00
|
|
|
"github.com/spf13/cast"
|
2016-08-07 20:01:55 +00:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2016-05-14 04:35:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Multilingual struct {
|
2016-08-07 20:01:55 +00:00
|
|
|
Languages helpers.Languages
|
2016-07-25 20:22:09 +00:00
|
|
|
|
2016-08-07 20:01:55 +00:00
|
|
|
DefaultLang *helpers.Language
|
2016-07-28 07:30:58 +00:00
|
|
|
|
2016-08-07 20:01:55 +00:00
|
|
|
langMap map[string]*helpers.Language
|
2016-07-25 20:22:09 +00:00
|
|
|
langMapInit sync.Once
|
|
|
|
}
|
|
|
|
|
2016-08-07 20:01:55 +00:00
|
|
|
func (ml *Multilingual) Language(lang string) *helpers.Language {
|
2016-07-25 20:22:09 +00:00
|
|
|
ml.langMapInit.Do(func() {
|
2016-08-07 20:01:55 +00:00
|
|
|
ml.langMap = make(map[string]*helpers.Language)
|
2016-07-25 20:22:09 +00:00
|
|
|
for _, l := range ml.Languages {
|
|
|
|
ml.langMap[l.Lang] = l
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return ml.langMap[lang]
|
|
|
|
}
|
|
|
|
|
2016-08-06 12:51:50 +00:00
|
|
|
func newMultiLingualFromSites(sites ...*Site) (*Multilingual, error) {
|
2016-08-07 20:01:55 +00:00
|
|
|
languages := make(helpers.Languages, len(sites))
|
2016-08-06 12:51:50 +00:00
|
|
|
|
|
|
|
for i, s := range sites {
|
|
|
|
if s.Language == nil {
|
|
|
|
return nil, errors.New("Missing language for site")
|
|
|
|
}
|
|
|
|
languages[i] = s.Language
|
|
|
|
}
|
|
|
|
|
2016-08-07 20:01:55 +00:00
|
|
|
return &Multilingual{Languages: languages, DefaultLang: helpers.NewDefaultLanguage()}, nil
|
2016-08-06 12:51:50 +00:00
|
|
|
|
2016-08-07 12:03:03 +00:00
|
|
|
}
|
2016-08-06 12:51:50 +00:00
|
|
|
|
2016-08-07 12:03:03 +00:00
|
|
|
func newMultiLingualDefaultLanguage() *Multilingual {
|
2016-08-07 20:01:55 +00:00
|
|
|
return newMultiLingualForLanguage(helpers.NewDefaultLanguage())
|
2016-08-06 12:51:50 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 20:01:55 +00:00
|
|
|
func newMultiLingualForLanguage(language *helpers.Language) *Multilingual {
|
|
|
|
languages := helpers.Languages{language}
|
2016-08-07 12:03:03 +00:00
|
|
|
return &Multilingual{Languages: languages, DefaultLang: language}
|
|
|
|
}
|
2016-07-25 20:22:09 +00:00
|
|
|
func (ml *Multilingual) enabled() bool {
|
2016-07-28 07:30:58 +00:00
|
|
|
return len(ml.Languages) > 1
|
2016-07-24 11:58:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 04:35:16 +00:00
|
|
|
func (s *Site) multilingualEnabled() bool {
|
2016-08-08 08:12:39 +00:00
|
|
|
if s.owner == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return s.owner.multilingual != nil && s.owner.multilingual.enabled()
|
2016-05-14 04:35:16 +00:00
|
|
|
}
|
2016-07-24 11:58:27 +00:00
|
|
|
|
2016-08-07 20:01:55 +00:00
|
|
|
func toSortedLanguages(l map[string]interface{}) (helpers.Languages, error) {
|
|
|
|
langs := make(helpers.Languages, len(l))
|
2016-08-05 11:10:58 +00:00
|
|
|
i := 0
|
|
|
|
|
|
|
|
for lang, langConf := range l {
|
2016-08-10 20:46:02 +00:00
|
|
|
langsMap, err := cast.ToStringMapE(langConf)
|
2016-08-05 11:10:58 +00:00
|
|
|
|
2016-08-10 20:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Language config is not a map: %T", langConf)
|
2016-08-05 11:10:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 20:01:55 +00:00
|
|
|
language := helpers.NewLanguage(lang)
|
2016-08-05 11:10:58 +00:00
|
|
|
|
2016-10-16 17:28:21 +00:00
|
|
|
for loki, v := range langsMap {
|
2016-08-05 11:10:58 +00:00
|
|
|
switch loki {
|
|
|
|
case "title":
|
|
|
|
language.Title = cast.ToString(v)
|
2016-09-12 18:57:52 +00:00
|
|
|
case "languagename":
|
|
|
|
language.LanguageName = cast.ToString(v)
|
2016-08-05 11:10:58 +00:00
|
|
|
case "weight":
|
|
|
|
language.Weight = cast.ToInt(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put all into the Params map
|
|
|
|
language.SetParam(loki, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
langs[i] = language
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(langs)
|
|
|
|
|
|
|
|
return langs, nil
|
|
|
|
}
|