| 1 |
// Copyright 2024 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 langs contains the language related types and function. |
| 15 |
package langs |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"sync" |
| 20 |
"time" |
| 21 |
|
| 22 |
"golang.org/x/text/collate" |
| 23 |
"golang.org/x/text/language" |
| 24 |
|
| 25 |
"github.com/bep/golocales" |
| 26 |
"github.com/bep/logg" |
| 27 |
"github.com/gohugoio/hugo/common/hmaps" |
| 28 |
"github.com/gohugoio/hugo/common/htime" |
| 29 |
"github.com/gohugoio/hugo/common/hugo" |
| 30 |
"github.com/gohugoio/hugo/common/loggers" |
| 31 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 32 |
) |
| 33 |
|
| 34 |
var _ sitesmatrix.DimensionInfo = (*Language)(nil) |
| 35 |
|
| 36 |
type Language struct { |
| 37 |
// The language code, e.g. "en" or "no". |
| 38 |
// This is the key used in the languages map in the configuration, |
| 39 |
// and currently only settable as the key in the language map in the config. |
| 40 |
Lang string |
| 41 |
|
| 42 |
// Fields from the language config. |
| 43 |
LanguageConfig |
| 44 |
|
| 45 |
// Used for date formatting etc. We don't want these exported to the |
| 46 |
// templates. |
| 47 |
translator golocales.Translator |
| 48 |
timeFormatter htime.TimeFormatter |
| 49 |
tag language.Tag |
| 50 |
|
| 51 |
// collator1 and collator2 are the same, we have 2 to prevent deadlocks. |
| 52 |
collator1 *Collator |
| 53 |
collator2 *Collator |
| 54 |
|
| 55 |
location *time.Location |
| 56 |
isDefault bool |
| 57 |
|
| 58 |
// This is just an alias of Site.Params. |
| 59 |
params hmaps.Params |
| 60 |
|
| 61 |
logger loggers.Logger |
| 62 |
} |
| 63 |
|
| 64 |
// Name is an alias for Lang. |
| 65 |
func (l *Language) Name() string { |
| 66 |
return l.Lang |
| 67 |
} |
| 68 |
|
| 69 |
func (l *Language) Logger() logg.Logger { |
| 70 |
if l.logger == nil { |
| 71 |
return loggers.Log().Logger() |
| 72 |
} |
| 73 |
return l.logger.Logger() |
| 74 |
} |
| 75 |
|
| 76 |
func (l *Language) IsDefault() bool { |
| 77 |
return l.isDefault |
| 78 |
} |
| 79 |
|
| 80 |
// NewLanguage creates a new language. |
| 81 |
func NewLanguage(lang, defaultContentLanguage, timeZone string, languageConfig LanguageConfig, logger loggers.Logger) (*Language, error) { |
| 82 |
var translator golocales.Translator |
| 83 |
for _, key := range []string{languageConfig.Locale, lang, defaultContentLanguage, "en"} { |
| 84 |
if key == "" { |
| 85 |
continue |
| 86 |
} |
| 87 |
if translator = golocales.New(key); translator != nil { |
| 88 |
break |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
var coll1, coll2 *Collator |
| 93 |
tag, err := language.Parse(lang) |
| 94 |
if err == nil { |
| 95 |
coll1 = &Collator{ |
| 96 |
c: collate.New(tag), |
| 97 |
} |
| 98 |
coll2 = &Collator{ |
| 99 |
c: collate.New(tag), |
| 100 |
} |
| 101 |
} else { |
| 102 |
coll1 = &Collator{ |
| 103 |
c: collate.New(language.English), |
| 104 |
} |
| 105 |
coll2 = &Collator{ |
| 106 |
c: collate.New(language.English), |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
l := &Language{ |
| 111 |
Lang: lang, |
| 112 |
LanguageConfig: languageConfig, |
| 113 |
translator: translator, |
| 114 |
timeFormatter: htime.NewTimeFormatter(translator), |
| 115 |
tag: tag, |
| 116 |
collator1: coll1, |
| 117 |
collator2: coll2, |
| 118 |
isDefault: lang == defaultContentLanguage, |
| 119 |
logger: logger, |
| 120 |
} |
| 121 |
|
| 122 |
return l, l.loadLocation(timeZone) |
| 123 |
} |
| 124 |
|
| 125 |
// This is injected from hugolib to avoid circular dependencies. |
| 126 |
var DeprecationFunc = func(item, alternative string, err bool) {} |
| 127 |
|
| 128 |
// Params returns the language params. |
| 129 |
// Note that this is the same as the Site.Params, but we keep it here for legacy reasons. |
| 130 |
// Deprecated: Use the site.Params instead. |
| 131 |
func (l *Language) Params() hmaps.Params { |
| 132 |
// TODO(bep) Remove this for now as it created a little too much noise. Need to think about this. |
| 133 |
// See https://github.com/gohugoio/hugo/issues/11025 |
| 134 |
// DeprecationFunc(".Language.Params", paramsDeprecationWarning, false) |
| 135 |
return l.params |
| 136 |
} |
| 137 |
|
| 138 |
// Deprecated: Use Locale instead. |
| 139 |
func (l *Language) LanguageCode() string { |
| 140 |
hugo.DeprecateWithLogger(".Language.LanguageCode", "Use .Language.Locale instead.", "v0.158.0", l.Logger()) |
| 141 |
return l.Locale() |
| 142 |
} |
| 143 |
|
| 144 |
func (l *Language) Locale() string { |
| 145 |
if l.LanguageConfig.Locale != "" { |
| 146 |
return l.LanguageConfig.Locale |
| 147 |
} |
| 148 |
if l.LanguageConfig.LanguageCode != "" { |
| 149 |
return l.LanguageConfig.LanguageCode |
| 150 |
} |
| 151 |
return l.Lang |
| 152 |
} |
| 153 |
|
| 154 |
// Deprecated: Use Direction instead. |
| 155 |
func (l *Language) LanguageDirection() string { |
| 156 |
hugo.DeprecateWithLogger(".Language.LanguageDirection", "Use .Language.Direction instead.", "v0.158.0", l.Logger()) |
| 157 |
return l.Direction() |
| 158 |
} |
| 159 |
|
| 160 |
func (l *Language) Direction() string { |
| 161 |
if l.LanguageConfig.Direction != "" { |
| 162 |
return l.LanguageConfig.Direction |
| 163 |
} |
| 164 |
return l.LanguageConfig.LanguageDirection |
| 165 |
} |
| 166 |
|
| 167 |
// Deprecated: Use Label instead. |
| 168 |
func (l *Language) LanguageName() string { |
| 169 |
hugo.DeprecateWithLogger(".Language.LanguageName", "Use .Language.Label instead.", "v0.158.0", l.Logger()) |
| 170 |
return l.Label() |
| 171 |
} |
| 172 |
|
| 173 |
func (l *Language) Label() string { |
| 174 |
if l.LanguageConfig.Label != "" { |
| 175 |
return l.LanguageConfig.Label |
| 176 |
} |
| 177 |
return l.LanguageConfig.LanguageName |
| 178 |
} |
| 179 |
|
| 180 |
func (l *Language) loadLocation(tzStr string) error { |
| 181 |
location, err := time.LoadLocation(tzStr) |
| 182 |
if err != nil { |
| 183 |
return fmt.Errorf("invalid timeZone for language %q: %w", l.Lang, err) |
| 184 |
} |
| 185 |
l.location = location |
| 186 |
|
| 187 |
return nil |
| 188 |
} |
| 189 |
|
| 190 |
func (l *Language) String() string { |
| 191 |
return l.Lang |
| 192 |
} |
| 193 |
|
| 194 |
// Languages is a sortable list of languages. |
| 195 |
type Languages []*Language |
| 196 |
|
| 197 |
func (l Languages) AsSet() map[string]bool { |
| 198 |
m := make(map[string]bool) |
| 199 |
for _, lang := range l { |
| 200 |
m[lang.Lang] = true |
| 201 |
} |
| 202 |
|
| 203 |
return m |
| 204 |
} |
| 205 |
|
| 206 |
// AsIndexSet returns a map with the language code as key and index in l as value. |
| 207 |
func (l Languages) AsIndexSet() map[string]int { |
| 208 |
m := make(map[string]int) |
| 209 |
for i, lang := range l { |
| 210 |
m[lang.Lang] = i |
| 211 |
} |
| 212 |
|
| 213 |
return m |
| 214 |
} |
| 215 |
|
| 216 |
// Internal access to unexported Language fields. |
| 217 |
// This construct is to prevent them from leaking to the templates. |
| 218 |
|
| 219 |
func SetParams(l *Language, params hmaps.Params) { |
| 220 |
l.params = params |
| 221 |
} |
| 222 |
|
| 223 |
func GetTimeFormatter(l *Language) htime.TimeFormatter { |
| 224 |
return l.timeFormatter |
| 225 |
} |
| 226 |
|
| 227 |
func GetTranslator(l *Language) golocales.Translator { |
| 228 |
return l.translator |
| 229 |
} |
| 230 |
|
| 231 |
func GetLocation(l *Language) *time.Location { |
| 232 |
return l.location |
| 233 |
} |
| 234 |
|
| 235 |
func GetCollator1(l *Language) *Collator { |
| 236 |
return l.collator1 |
| 237 |
} |
| 238 |
|
| 239 |
func GetCollator2(l *Language) *Collator { |
| 240 |
return l.collator2 |
| 241 |
} |
| 242 |
|
| 243 |
type Collator struct { |
| 244 |
sync.Mutex |
| 245 |
c *collate.Collator |
| 246 |
} |
| 247 |
|
| 248 |
// CompareStrings compares a and b. |
| 249 |
// It returns -1 if a < b, 1 if a > b and 0 if a == b. |
| 250 |
// Note that the Collator is not thread safe, so you may want |
| 251 |
// to acquire a lock on it before calling this method. |
| 252 |
func (c *Collator) CompareStrings(a, b string) int { |
| 253 |
return c.c.CompareString(a, b) |
| 254 |
} |
| 255 |
|
| 256 |
// IndexDefault returns the index of the default language. |
| 257 |
func IndexDefault(languages Languages) int { |
| 258 |
for i, l := range languages { |
| 259 |
if l.isDefault { |
| 260 |
return i |
| 261 |
} |
| 262 |
} |
| 263 |
panic("no default lang found") |
| 264 |
} |