2016-08-07 16:01:55 -04: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 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
|
|
|
|
2016-08-06 08:51:50 -04:00
|
|
|
"errors"
|
2016-08-05 07:10:58 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/cast"
|
2016-05-14 00:35:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Multilingual struct {
|
2016-08-07 16:01:55 -04:00
|
|
|
Languages helpers.Languages
|
2016-07-25 16:22:09 -04:00
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
DefaultLang *helpers.Language
|
2016-07-28 03:30:58 -04:00
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
langMap map[string]*helpers.Language
|
2016-07-25 16:22:09 -04:00
|
|
|
langMapInit sync.Once
|
|
|
|
}
|
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
func (ml *Multilingual) Language(lang string) *helpers.Language {
|
2016-07-25 16:22:09 -04:00
|
|
|
ml.langMapInit.Do(func() {
|
2016-08-07 16:01:55 -04:00
|
|
|
ml.langMap = make(map[string]*helpers.Language)
|
2016-07-25 16:22:09 -04:00
|
|
|
for _, l := range ml.Languages {
|
|
|
|
ml.langMap[l.Lang] = l
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return ml.langMap[lang]
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func newMultiLingualFromSites(cfg config.Provider, sites ...*Site) (*Multilingual, error) {
|
2016-08-07 16:01:55 -04:00
|
|
|
languages := make(helpers.Languages, len(sites))
|
2016-08-06 08:51:50 -04:00
|
|
|
|
|
|
|
for i, s := range sites {
|
|
|
|
if s.Language == nil {
|
|
|
|
return nil, errors.New("Missing language for site")
|
|
|
|
}
|
|
|
|
languages[i] = s.Language
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
defaultLang := cfg.GetString("defaultContentLanguage")
|
2016-08-06 08:51:50 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if defaultLang == "" {
|
|
|
|
defaultLang = "en"
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Multilingual{Languages: languages, DefaultLang: helpers.NewLanguage(defaultLang, cfg)}, nil
|
2016-08-06 08:51:50 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
func newMultiLingualForLanguage(language *helpers.Language) *Multilingual {
|
|
|
|
languages := helpers.Languages{language}
|
2016-08-07 08:03:03 -04:00
|
|
|
return &Multilingual{Languages: languages, DefaultLang: language}
|
|
|
|
}
|
2016-07-25 16:22:09 -04:00
|
|
|
func (ml *Multilingual) enabled() bool {
|
2016-07-28 03:30:58 -04:00
|
|
|
return len(ml.Languages) > 1
|
2016-07-24 07:58:27 -04:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
func (s *Site) multilingualEnabled() bool {
|
2016-08-08 04:12:39 -04:00
|
|
|
if s.owner == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return s.owner.multilingual != nil && s.owner.multilingual.enabled()
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
2016-07-24 07:58:27 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func toSortedLanguages(cfg config.Provider, l map[string]interface{}) (helpers.Languages, error) {
|
2016-08-07 16:01:55 -04:00
|
|
|
langs := make(helpers.Languages, len(l))
|
2016-08-05 07:10:58 -04:00
|
|
|
i := 0
|
|
|
|
|
|
|
|
for lang, langConf := range l {
|
2016-08-10 16:46:02 -04:00
|
|
|
langsMap, err := cast.ToStringMapE(langConf)
|
2016-08-05 07:10:58 -04:00
|
|
|
|
2016-08-10 16:46:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Language config is not a map: %T", langConf)
|
2016-08-05 07:10:58 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
language := helpers.NewLanguage(lang, cfg)
|
2016-08-05 07:10:58 -04:00
|
|
|
|
2016-10-16 13:28:21 -04:00
|
|
|
for loki, v := range langsMap {
|
2016-08-05 07:10:58 -04:00
|
|
|
switch loki {
|
|
|
|
case "title":
|
|
|
|
language.Title = cast.ToString(v)
|
2016-09-12 14:57:52 -04:00
|
|
|
case "languagename":
|
|
|
|
language.LanguageName = cast.ToString(v)
|
2016-08-05 07:10:58 -04: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
|
|
|
|
}
|