| 1 |
// Copyright 2018 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 |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
"iter" |
| 20 |
"slices" |
| 21 |
"sort" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/common/paths" |
| 24 |
"github.com/gohugoio/hugo/common/predicate" |
| 25 |
"github.com/gohugoio/hugo/config" |
| 26 |
"github.com/mitchellh/mapstructure" |
| 27 |
) |
| 28 |
|
| 29 |
// LanguageConfig holds the configuration for a single language. |
| 30 |
// This is what is read from the config file. |
| 31 |
type LanguageConfig struct { |
| 32 |
// Deprecated: Use Label instead. |
| 33 |
LanguageName string `json:"-"` |
| 34 |
|
| 35 |
// Deprecated: Use Locale instead. |
| 36 |
LanguageCode string `json:"-"` |
| 37 |
|
| 38 |
// The language title. When set, this will |
| 39 |
// override site.Title for this language. |
| 40 |
Title string |
| 41 |
|
| 42 |
// Deprecated: Use Direction instead. |
| 43 |
LanguageDirection string `json:"-"` |
| 44 |
|
| 45 |
// The language weight. When set to a non-zero value, this will |
| 46 |
// be the main sort criteria for the language. |
| 47 |
Weight int |
| 48 |
|
| 49 |
// Set to true to disable this language. |
| 50 |
Disabled bool |
| 51 |
|
| 52 |
// The language direction, e.g. "ltr" or "rtl". |
| 53 |
Direction string |
| 54 |
|
| 55 |
// The language name, e.g. "English". |
| 56 |
Label string |
| 57 |
|
| 58 |
// The locale, e.g. "en-US". |
| 59 |
Locale string |
| 60 |
} |
| 61 |
|
| 62 |
type LanguageInternal struct { |
| 63 |
// Name is the name of the role, extracted from the key in the config. |
| 64 |
Name string |
| 65 |
|
| 66 |
// Whether this role is the default role. |
| 67 |
// This will be rendered in the root. |
| 68 |
// There is only be one default role. |
| 69 |
Default bool |
| 70 |
|
| 71 |
LanguageConfig |
| 72 |
} |
| 73 |
|
| 74 |
type LanguagesInternal struct { |
| 75 |
LanguageConfigs map[string]LanguageConfig |
| 76 |
Sorted []LanguageInternal |
| 77 |
} |
| 78 |
|
| 79 |
func (ls LanguagesInternal) IndexDefault() int { |
| 80 |
for i, role := range ls.Sorted { |
| 81 |
if role.Default { |
| 82 |
return i |
| 83 |
} |
| 84 |
} |
| 85 |
panic("no default role found") |
| 86 |
} |
| 87 |
|
| 88 |
func (ls LanguagesInternal) ResolveName(i int) string { |
| 89 |
if i < 0 || i >= len(ls.Sorted) { |
| 90 |
panic(fmt.Sprintf("index %d out of range for languages", i)) |
| 91 |
} |
| 92 |
return ls.Sorted[i].Name |
| 93 |
} |
| 94 |
|
| 95 |
func (ls LanguagesInternal) ResolveIndex(name string) int { |
| 96 |
for i, role := range ls.Sorted { |
| 97 |
if role.Name == name { |
| 98 |
return i |
| 99 |
} |
| 100 |
} |
| 101 |
return -1 |
| 102 |
} |
| 103 |
|
| 104 |
func (ls LanguagesInternal) Len() int { |
| 105 |
return len(ls.Sorted) |
| 106 |
} |
| 107 |
|
| 108 |
// IndexMatch returns an iterator for the roles that match the filter. |
| 109 |
func (ls LanguagesInternal) IndexMatch(match predicate.P[predicate.IndexString]) (iter.Seq[int], error) { |
| 110 |
return func(yield func(i int) bool) { |
| 111 |
for i, l := range ls.Sorted { |
| 112 |
if match(predicate.IndexString{Index: i, String: l.Name}) { |
| 113 |
if !yield(i) { |
| 114 |
return |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
}, nil |
| 119 |
} |
| 120 |
|
| 121 |
// ForEachIndex returns an iterator for the indices of the languages. |
| 122 |
func (ls LanguagesInternal) ForEachIndex() iter.Seq[int] { |
| 123 |
return func(yield func(i int) bool) { |
| 124 |
for i := range ls.Sorted { |
| 125 |
if !yield(i) { |
| 126 |
return |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func (ls *LanguagesInternal) init(defaultContentLanguage, rootLocale, rootLanguageCode string, disabledLanguages []string) (string, error) { |
| 133 |
const en = "en" |
| 134 |
|
| 135 |
if len(ls.LanguageConfigs) == 0 { |
| 136 |
// Add a default language. |
| 137 |
if defaultContentLanguage == "" { |
| 138 |
defaultContentLanguage = en |
| 139 |
} |
| 140 |
ls.LanguageConfigs[defaultContentLanguage] = LanguageConfig{} |
| 141 |
} |
| 142 |
|
| 143 |
var ( |
| 144 |
defaultSeen bool |
| 145 |
enIdx int = -1 |
| 146 |
) |
| 147 |
for k, v := range ls.LanguageConfigs { |
| 148 |
if !v.Disabled && slices.Contains(disabledLanguages, k) { |
| 149 |
// This language is disabled. |
| 150 |
v.Disabled = true |
| 151 |
ls.LanguageConfigs[k] = v |
| 152 |
} |
| 153 |
|
| 154 |
if k == "" { |
| 155 |
return "", errors.New("language name cannot be empty") |
| 156 |
} |
| 157 |
|
| 158 |
if err := paths.ValidateIdentifier(k); err != nil { |
| 159 |
return "", fmt.Errorf("language name %q is invalid: %s", k, err) |
| 160 |
} |
| 161 |
|
| 162 |
var isDefault bool |
| 163 |
if k == defaultContentLanguage { |
| 164 |
isDefault = true |
| 165 |
defaultSeen = true |
| 166 |
} |
| 167 |
|
| 168 |
if isDefault && v.Disabled { |
| 169 |
return "", fmt.Errorf("default language %q is disabled", k) |
| 170 |
} |
| 171 |
|
| 172 |
if !v.Disabled { |
| 173 |
ls.Sorted = append(ls.Sorted, LanguageInternal{Name: k, Default: isDefault, LanguageConfig: v}) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// Sort by weight if set, then by name. |
| 178 |
sort.SliceStable(ls.Sorted, func(i, j int) bool { |
| 179 |
ri, rj := ls.Sorted[i], ls.Sorted[j] |
| 180 |
if ri.Weight == rj.Weight { |
| 181 |
return ri.Name < rj.Name |
| 182 |
} |
| 183 |
if rj.Weight == 0 { |
| 184 |
return true |
| 185 |
} |
| 186 |
if ri.Weight == 0 { |
| 187 |
return false |
| 188 |
} |
| 189 |
return ri.Weight < rj.Weight |
| 190 |
}) |
| 191 |
|
| 192 |
for i, l := range ls.Sorted { |
| 193 |
if l.Name == en { |
| 194 |
enIdx = i |
| 195 |
break |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if !defaultSeen { |
| 200 |
if defaultContentLanguage != "" { |
| 201 |
// Set by the user, but not found in the config. |
| 202 |
return "", fmt.Errorf("defaultContentLanguage %q not found in languages configuration", defaultContentLanguage) |
| 203 |
} |
| 204 |
// Not set by the user, so we use the first language in the config. |
| 205 |
defaultIdx := 0 |
| 206 |
if enIdx != -1 { |
| 207 |
defaultIdx = enIdx |
| 208 |
} |
| 209 |
d := ls.Sorted[defaultIdx] |
| 210 |
d.Default = true |
| 211 |
ls.LanguageConfigs[d.Name] = d.LanguageConfig |
| 212 |
ls.Sorted[defaultIdx] = d |
| 213 |
defaultContentLanguage = d.Name |
| 214 |
} |
| 215 |
|
| 216 |
// Apply root-level locale to the default content language if it has none. |
| 217 |
// Done after defaultContentLanguage is resolved and Sorted is built so both |
| 218 |
// are updated consistently. Other languages fall back to their Lang key. |
| 219 |
// Priority: per-lang locale > root locale > per-lang languageCode > root |
| 220 |
// languageCode. Root languageCode only applies when the default language has |
| 221 |
// neither a per-lang locale nor a per-lang languageCode. |
| 222 |
applyRootLocale := func(locale string) { |
| 223 |
if v, ok := ls.LanguageConfigs[defaultContentLanguage]; ok { |
| 224 |
v.Locale = locale |
| 225 |
ls.LanguageConfigs[defaultContentLanguage] = v |
| 226 |
for i, s := range ls.Sorted { |
| 227 |
if s.Name == defaultContentLanguage { |
| 228 |
ls.Sorted[i].LanguageConfig.Locale = locale |
| 229 |
break |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
if rootLocale != "" { |
| 235 |
if v, ok := ls.LanguageConfigs[defaultContentLanguage]; ok && v.Locale == "" { |
| 236 |
applyRootLocale(rootLocale) |
| 237 |
} |
| 238 |
} else if rootLanguageCode != "" { |
| 239 |
if v, ok := ls.LanguageConfigs[defaultContentLanguage]; ok && v.Locale == "" && v.LanguageCode == "" { |
| 240 |
applyRootLocale(rootLanguageCode) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return defaultContentLanguage, nil |
| 245 |
} |
| 246 |
|
| 247 |
func DecodeConfig(defaultContentLanguage, rootLocale, rootLanguageCode string, disabledLanguages []string, m map[string]any) (*config.ConfigNamespace[map[string]LanguageConfig, LanguagesInternal], string, error) { |
| 248 |
v, err := config.DecodeNamespace[map[string]LanguageConfig](m, func(in any) (LanguagesInternal, any, error) { |
| 249 |
var languages LanguagesInternal |
| 250 |
var conf map[string]LanguageConfig |
| 251 |
if err := mapstructure.Decode(m, &conf); err != nil { |
| 252 |
return languages, nil, err |
| 253 |
} |
| 254 |
languages.LanguageConfigs = conf |
| 255 |
var err error |
| 256 |
if defaultContentLanguage, err = languages.init(defaultContentLanguage, rootLocale, rootLanguageCode, disabledLanguages); err != nil { |
| 257 |
return languages, nil, err |
| 258 |
} |
| 259 |
return languages, languages.LanguageConfigs, nil |
| 260 |
}) |
| 261 |
|
| 262 |
return v, defaultContentLanguage, err |
| 263 |
} |