hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

multilingual.go (2170B)

    1 // Copyright 2019 The Hugo Authors. All rights reserved.
    2 //
    3 // Licensed under the Apache License, Version 2.0 (the "License");
    4 // you may not use this file except in compliance with the License.
    5 // You may obtain a copy of the License at
    6 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 package hugolib
   15 
   16 import (
   17 	"errors"
   18 	"sync"
   19 
   20 	"github.com/gohugoio/hugo/langs"
   21 
   22 	"github.com/gohugoio/hugo/config"
   23 )
   24 
   25 // Multilingual manages the all languages used in a multilingual site.
   26 type Multilingual struct {
   27 	Languages langs.Languages
   28 
   29 	DefaultLang *langs.Language
   30 
   31 	langMap     map[string]*langs.Language
   32 	langMapInit sync.Once
   33 }
   34 
   35 // Language returns the Language associated with the given string.
   36 func (ml *Multilingual) Language(lang string) *langs.Language {
   37 	ml.langMapInit.Do(func() {
   38 		ml.langMap = make(map[string]*langs.Language)
   39 		for _, l := range ml.Languages {
   40 			ml.langMap[l.Lang] = l
   41 		}
   42 	})
   43 	return ml.langMap[lang]
   44 }
   45 
   46 func getLanguages(cfg config.Provider) langs.Languages {
   47 	if cfg.IsSet("languagesSorted") {
   48 		return cfg.Get("languagesSorted").(langs.Languages)
   49 	}
   50 
   51 	return langs.Languages{langs.NewDefaultLanguage(cfg)}
   52 }
   53 
   54 func newMultiLingualFromSites(cfg config.Provider, sites ...*Site) (*Multilingual, error) {
   55 	languages := make(langs.Languages, len(sites))
   56 
   57 	for i, s := range sites {
   58 		if s.language == nil {
   59 			return nil, errors.New("missing language for site")
   60 		}
   61 		languages[i] = s.language
   62 	}
   63 
   64 	defaultLang := cfg.GetString("defaultContentLanguage")
   65 
   66 	if defaultLang == "" {
   67 		defaultLang = "en"
   68 	}
   69 
   70 	return &Multilingual{Languages: languages, DefaultLang: langs.NewLanguage(defaultLang, cfg)}, nil
   71 }
   72 
   73 func (ml *Multilingual) enabled() bool {
   74 	return len(ml.Languages) > 1
   75 }
   76 
   77 func (s *Site) multilingualEnabled() bool {
   78 	if s.h == nil {
   79 		return false
   80 	}
   81 	return s.h.multilingual != nil && s.h.multilingual.enabled()
   82 }