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"
|
|
|
|
|
2017-06-13 16:42:45 +00:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-06-13 17:07:35 +00:00
|
|
|
"github.com/spf13/cast"
|
2016-05-14 04:35:16 +00:00
|
|
|
)
|
|
|
|
|
2017-08-02 12:25:05 +00:00
|
|
|
// Multilingual manages the all languages used in a multilingual site.
|
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
|
|
|
|
}
|
|
|
|
|
2017-08-08 08:54:35 +00:00
|
|
|
// Language returns the Language associated with the given string.
|
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]
|
|
|
|
}
|
|
|
|
|
2017-11-02 07:25:20 +00:00
|
|
|
func getLanguages(cfg config.Provider) helpers.Languages {
|
|
|
|
if cfg.IsSet("languagesSorted") {
|
|
|
|
return cfg.Get("languagesSorted").(helpers.Languages)
|
|
|
|
}
|
|
|
|
|
|
|
|
return helpers.Languages{helpers.NewDefaultLanguage(cfg)}
|
|
|
|
}
|
|
|
|
|
2017-02-05 03:20:06 +00:00
|
|
|
func newMultiLingualFromSites(cfg config.Provider, 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
|
|
|
|
}
|
|
|
|
|
2017-02-05 03:20:06 +00:00
|
|
|
defaultLang := cfg.GetString("defaultContentLanguage")
|
2016-08-06 12:51:50 +00:00
|
|
|
|
2017-02-05 03:20:06 +00:00
|
|
|
if defaultLang == "" {
|
|
|
|
defaultLang = "en"
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Multilingual{Languages: languages, DefaultLang: helpers.NewLanguage(defaultLang, cfg)}, nil
|
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
|
|
|
|
2017-02-05 03:20:06 +00:00
|
|
|
func toSortedLanguages(cfg config.Provider, l map[string]interface{}) (helpers.Languages, error) {
|
2016-08-07 20:01:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-05 03:20:06 +00:00
|
|
|
language := helpers.NewLanguage(lang, cfg)
|
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
|
|
|
|
}
|