| 1 |
// Copyright 2025 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_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/hugolib" |
| 21 |
) |
| 22 |
|
| 23 |
func TestLanguagesContentSimple(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
|
| 26 |
files := ` |
| 27 |
-- hugo.toml -- |
| 28 |
baseURL = "https://example.org/" |
| 29 |
defaultContentLanguage = "en" |
| 30 |
defaultContentLanguageInSubdir = true |
| 31 |
[languages] |
| 32 |
[languages.en] |
| 33 |
weight = 2 |
| 34 |
[languages.nn] |
| 35 |
weight = 1 |
| 36 |
-- content/_index.md -- |
| 37 |
--- |
| 38 |
title: "Home" |
| 39 |
--- |
| 40 |
|
| 41 |
Welcome to the home page. |
| 42 |
-- content/_index.nn.md -- |
| 43 |
--- |
| 44 |
title: "Heim" |
| 45 |
--- |
| 46 |
Welkomen heim! |
| 47 |
-- layouts/all.html -- |
| 48 |
title: {{ .Title }}| |
| 49 |
` |
| 50 |
b := hugolib.Test(t, files) |
| 51 |
|
| 52 |
b.AssertFileContent("public/en/index.html", `title: Home|`) |
| 53 |
b.AssertFileContent("public/nn/index.html", `title: Heim|`) |
| 54 |
} |
| 55 |
|
| 56 |
func TestLanguagesContentSharedResource(t *testing.T) { |
| 57 |
t.Parallel() |
| 58 |
|
| 59 |
files := ` |
| 60 |
-- hugo.toml -- |
| 61 |
disableKinds = ["taxonomy", "term", "sitemap", "404"] |
| 62 |
baseURL = "https://example.org/" |
| 63 |
defaultContentLanguage = "en" |
| 64 |
defaultContentLanguageInSubdir = true |
| 65 |
[languages] |
| 66 |
[languages.en] |
| 67 |
weight = 2 |
| 68 |
[languages.nn] |
| 69 |
weight = 1 |
| 70 |
-- content/mytext.txt -- |
| 71 |
This is a shared text file. |
| 72 |
-- content/_index.md -- |
| 73 |
--- |
| 74 |
title: "Home" |
| 75 |
--- |
| 76 |
|
| 77 |
Welcome to the home page. |
| 78 |
-- content/_index.nn.md -- |
| 79 |
--- |
| 80 |
title: "Heim" |
| 81 |
--- |
| 82 |
Welkomen heim! |
| 83 |
-- layouts/home.html -- |
| 84 |
{{ $text := .Resources.Get "mytext.txt" }} |
| 85 |
title: {{ .Title }}|text: {{ with $text }}{{ .Content | safeHTML }}{{ end }}|{{ .Resources | len}} |
| 86 |
|
| 87 |
` |
| 88 |
b := hugolib.Test(t, files) |
| 89 |
|
| 90 |
b.AssertFileContent("public/nn/index.html", `title: Heim|text: This is a shared text file.|1`) |
| 91 |
b.AssertFileContent("public/en/index.html", `title: Home|text: This is a shared text file.|1`) |
| 92 |
} |
| 93 |
|
| 94 |
// Issue 14031 |
| 95 |
func TestDraftTermIssue14031(t *testing.T) { |
| 96 |
t.Parallel() |
| 97 |
|
| 98 |
files := ` |
| 99 |
-- hugo.toml -- |
| 100 |
disableKinds = ['home','rss','section','sitemap'] |
| 101 |
capitalizeListTitles = false |
| 102 |
pluralizeListTitles = false |
| 103 |
defaultContentLanguage = 'en' |
| 104 |
defaultContentLanguageInSubdir = true |
| 105 |
[languages.en] |
| 106 |
weight = 1 |
| 107 |
[languages.fr] |
| 108 |
weight = 2 |
| 109 |
[taxonomies] |
| 110 |
tag = 'tags' |
| 111 |
-- content/p1.en.md -- |
| 112 |
--- |
| 113 |
title: P1 (en) |
| 114 |
tags: [a,b] |
| 115 |
--- |
| 116 |
-- content/p1.fr.md -- |
| 117 |
--- |
| 118 |
title: P1 (fr) |
| 119 |
tags: [a,b] |
| 120 |
--- |
| 121 |
-- content/tags/a/_index.en.md -- |
| 122 |
--- |
| 123 |
title: a (en) |
| 124 |
--- |
| 125 |
-- content/tags/a/_index.fr.md -- |
| 126 |
--- |
| 127 |
title: a (fr) |
| 128 |
--- |
| 129 |
-- content/tags/b/_index.en.md -- |
| 130 |
--- |
| 131 |
title: b (en) |
| 132 |
--- |
| 133 |
-- content/tags/b/_index.fr.md -- |
| 134 |
--- |
| 135 |
title: b (fr) |
| 136 |
draft: false |
| 137 |
--- |
| 138 |
-- layouts/list.html -- |
| 139 |
{{- .Title -}}| |
| 140 |
{{- range .Pages -}} |
| 141 |
TITLE: {{ .Title }} RELPERMALINK: {{ .RelPermalink }}| |
| 142 |
{{- end -}} |
| 143 |
-- layouts/page.html -- |
| 144 |
{{- .Title -}}| |
| 145 |
{{- range .GetTerms "tags" -}} |
| 146 |
TITLE: {{ .Title }} RELPERMALINK: {{ .RelPermalink }}| |
| 147 |
{{- end -}} |
| 148 |
` |
| 149 |
|
| 150 |
b := hugolib.Test(t, files) |
| 151 |
|
| 152 |
b.AssertFileExists("public/en/tags/a/index.html", true) |
| 153 |
b.AssertFileExists("public/fr/tags/a/index.html", true) |
| 154 |
b.AssertFileExists("public/en/tags/b/index.html", true) |
| 155 |
b.AssertFileExists("public/fr/tags/b/index.html", true) |
| 156 |
|
| 157 |
b.AssertFileContentEquals("public/en/p1/index.html", |
| 158 |
"P1 (en)|TITLE: a (en) RELPERMALINK: /en/tags/a/|TITLE: b (en) RELPERMALINK: /en/tags/b/|", |
| 159 |
) |
| 160 |
b.AssertFileContentEquals("public/fr/p1/index.html", |
| 161 |
"P1 (fr)|TITLE: a (fr) RELPERMALINK: /fr/tags/a/|TITLE: b (fr) RELPERMALINK: /fr/tags/b/|", |
| 162 |
) |
| 163 |
b.AssertFileContentEquals("public/en/tags/index.html", |
| 164 |
"tags|TITLE: a (en) RELPERMALINK: /en/tags/a/|TITLE: b (en) RELPERMALINK: /en/tags/b/|", |
| 165 |
) |
| 166 |
b.AssertFileContentEquals("public/fr/tags/index.html", |
| 167 |
"tags|TITLE: a (fr) RELPERMALINK: /fr/tags/a/|TITLE: b (fr) RELPERMALINK: /fr/tags/b/|", |
| 168 |
) |
| 169 |
b.AssertFileContentEquals("public/en/tags/a/index.html", |
| 170 |
"a (en)|TITLE: P1 (en) RELPERMALINK: /en/p1/|", |
| 171 |
) |
| 172 |
b.AssertFileContentEquals("public/fr/tags/a/index.html", |
| 173 |
"a (fr)|TITLE: P1 (fr) RELPERMALINK: /fr/p1/|", |
| 174 |
) |
| 175 |
b.AssertFileContentEquals("public/en/tags/b/index.html", |
| 176 |
"b (en)|TITLE: P1 (en) RELPERMALINK: /en/p1/|", |
| 177 |
) |
| 178 |
b.AssertFileContentEquals("public/fr/tags/b/index.html", |
| 179 |
"b (fr)|TITLE: P1 (fr) RELPERMALINK: /fr/p1/|", |
| 180 |
) |
| 181 |
|
| 182 |
// Set draft to true on content/tags/b/_index.fr.md. |
| 183 |
files = strings.ReplaceAll(files, "draft: false", "draft: true") |
| 184 |
|
| 185 |
b = hugolib.Test(t, files) |
| 186 |
|
| 187 |
b.AssertFileExists("public/en/tags/a/index.html", true) |
| 188 |
b.AssertFileExists("public/fr/tags/a/index.html", true) |
| 189 |
b.AssertFileExists("public/en/tags/b/index.html", true) |
| 190 |
b.AssertFileExists("public/fr/tags/b/index.html", false) |
| 191 |
|
| 192 |
// The assertion below fails. |
| 193 |
// Got: P1 (en)|TITLE: a (en) RELPERMALINK: /en/tags/a/| |
| 194 |
b.AssertFileContentEquals("public/en/p1/index.html", |
| 195 |
"P1 (en)|TITLE: a (en) RELPERMALINK: /en/tags/a/|TITLE: b (en) RELPERMALINK: /en/tags/b/|", |
| 196 |
) |
| 197 |
b.AssertFileContentEquals("public/fr/p1/index.html", |
| 198 |
"P1 (fr)|TITLE: a (fr) RELPERMALINK: /fr/tags/a/|", |
| 199 |
) |
| 200 |
b.AssertFileContentEquals("public/en/tags/index.html", |
| 201 |
"tags|TITLE: a (en) RELPERMALINK: /en/tags/a/|TITLE: b (en) RELPERMALINK: /en/tags/b/|", |
| 202 |
) |
| 203 |
b.AssertFileContentEquals("public/fr/tags/index.html", |
| 204 |
"tags|TITLE: a (fr) RELPERMALINK: /fr/tags/a/|", |
| 205 |
) |
| 206 |
b.AssertFileContentEquals("public/en/tags/a/index.html", |
| 207 |
"a (en)|TITLE: P1 (en) RELPERMALINK: /en/p1/|", |
| 208 |
) |
| 209 |
b.AssertFileContentEquals("public/fr/tags/a/index.html", |
| 210 |
"a (fr)|TITLE: P1 (fr) RELPERMALINK: /fr/p1/|", |
| 211 |
) |
| 212 |
// The assertion below fails. |
| 213 |
// Got: b (en)| |
| 214 |
b.AssertFileContentEquals("public/en/tags/b/index.html", |
| 215 |
"b (en)|TITLE: P1 (en) RELPERMALINK: /en/p1/|", |
| 216 |
) |
| 217 |
} |
| 218 |
|
| 219 |
func TestLocaleAsLocalizationKeyIssue9109(t *testing.T) { |
| 220 |
t.Parallel() |
| 221 |
|
| 222 |
files := ` |
| 223 |
-- hugo.toml -- |
| 224 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 225 |
defaultContentLanguage = 'de' |
| 226 |
defaultContentLanguageInSubdir = true |
| 227 |
|
| 228 |
[languages.de] |
| 229 |
weight = 1 |
| 230 |
|
| 231 |
[languages.en] |
| 232 |
locale = 'en-US' |
| 233 |
weight = 2 |
| 234 |
|
| 235 |
[languages.fr] |
| 236 |
locale = 'bogus' |
| 237 |
weight = 3 |
| 238 |
|
| 239 |
[languages.nn] |
| 240 |
weight = 4 |
| 241 |
|
| 242 |
[languages.xx] |
| 243 |
locale = 'bogus' |
| 244 |
weight = 5 |
| 245 |
|
| 246 |
[languages.zh-cn] |
| 247 |
locale = 'zh-Hans' |
| 248 |
weight = 6 |
| 249 |
-- layouts/home.html -- |
| 250 |
Time: {{ "2026-04-01T20:11:31+08:00" | time.Format ":date_long" }}| |
| 251 |
FormatAccounting: {{ 512.5032 | lang.FormatAccounting 2 "USD" }}| |
| 252 |
FormatCurrency: {{ 512.5032 | lang.FormatCurrency 2 "USD" }}| |
| 253 |
` |
| 254 |
|
| 255 |
b := hugolib.Test(t, files) |
| 256 |
|
| 257 |
// de: no locale => localize with lang. |
| 258 |
b.AssertFileContent("public/de/index.html", |
| 259 |
`Time: 1. April 2026|`, |
| 260 |
"FormatAccounting: 512,50\u00a0$|", |
| 261 |
"FormatCurrency: 512,50\u00a0$|", |
| 262 |
) |
| 263 |
|
| 264 |
// en: valid locale => localize with locale. |
| 265 |
b.AssertFileContent("public/en/index.html", |
| 266 |
`Time: April 1, 2026|`, |
| 267 |
`FormatAccounting: $512.50|`, |
| 268 |
`FormatCurrency: $512.50|`, |
| 269 |
) |
| 270 |
|
| 271 |
// fr: invalid locale => localize with lang. |
| 272 |
b.AssertFileContent("public/fr/index.html", |
| 273 |
`Time: 1 avril 2026|`, |
| 274 |
"FormatAccounting: 512,50\u00a0$US|", |
| 275 |
"FormatCurrency: 512,50\u00a0$US|", |
| 276 |
) |
| 277 |
|
| 278 |
// nn: no locale => localize with lang. |
| 279 |
b.AssertFileContent("public/nn/index.html", |
| 280 |
`Time: 1. april 2026|`, |
| 281 |
"FormatAccounting: 512,50\u00a0USD|", |
| 282 |
"FormatCurrency: 512,50\u00a0USD|", |
| 283 |
) |
| 284 |
|
| 285 |
// xx: invalid locale + invalid lang => localize with defaultContentLanguage. |
| 286 |
b.AssertFileContent("public/xx/index.html", |
| 287 |
`Time: 1. April 2026|`, |
| 288 |
"FormatAccounting: 512,50\u00a0$|", |
| 289 |
"FormatCurrency: 512,50\u00a0$|", |
| 290 |
) |
| 291 |
|
| 292 |
// zh-cn: invalid lang, but valid locale => localize with locale. |
| 293 |
b.AssertFileContent("public/zh-cn/index.html", |
| 294 |
`Time: 2026年4月1日|`, |
| 295 |
`FormatAccounting: US$512.50|`, |
| 296 |
`FormatCurrency: US$512.50|`, |
| 297 |
) |
| 298 |
} |
| 299 |
|
| 300 |
func TestMultipleLanguageVariants7982(t *testing.T) { |
| 301 |
t.Parallel() |
| 302 |
|
| 303 |
files := ` |
| 304 |
-- hugo.toml -- |
| 305 |
disableKinds = ['page','section','rss','sitemap','taxonomy','term'] |
| 306 |
defaultContentLanguageInSubdir = true |
| 307 |
[languages.en] |
| 308 |
weight = 1 |
| 309 |
[languages.de] |
| 310 |
weight = 2 |
| 311 |
[languages.de-de] |
| 312 |
weight = 3 |
| 313 |
-- i18n/en.toml -- |
| 314 |
file = 'en' |
| 315 |
-- i18n/de.toml -- |
| 316 |
file = 'de' |
| 317 |
-- i18n/de-de.toml -- |
| 318 |
file = 'de-de' |
| 319 |
-- layouts/index.html -- |
| 320 |
language: {{ site.Language.Name }} file: {{ T "file" }}| |
| 321 |
` |
| 322 |
|
| 323 |
b := hugolib.Test(t, files) |
| 324 |
|
| 325 |
b.AssertFileContent("public/en/index.html", "language: en file: en|") |
| 326 |
b.AssertFileContent("public/de/index.html", "language: de file: de|") |
| 327 |
b.AssertFileContent("public/de-de/index.html", "language: de-de file: de-de|") |
| 328 |
} |
| 329 |
|
| 330 |
func TestDefaultContentLanguageFallback14243(t *testing.T) { |
| 331 |
t.Parallel() |
| 332 |
|
| 333 |
files := ` |
| 334 |
-- hugo.toml -- |
| 335 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 336 |
defaultContentLanguage = 'es' |
| 337 |
defaultContentLanguageInSubdir = true |
| 338 |
|
| 339 |
[languages.es] |
| 340 |
locale = 'es-AR' |
| 341 |
weight = 1 |
| 342 |
|
| 343 |
[languages.pt] |
| 344 |
locale = 'pt-BR' |
| 345 |
weight = 2 |
| 346 |
-- layouts/home.html -- |
| 347 |
{{ T "foo"}}| |
| 348 |
-- i18n/es-ar.toml -- |
| 349 |
foo = 'foo es-ar' |
| 350 |
-- i18n/es.toml -- |
| 351 |
foo = 'foo es' |
| 352 |
-- i18n/pt-br.toml -- |
| 353 |
foo = 'foo pt-br' |
| 354 |
-- i18n/pt.toml -- |
| 355 |
foo = 'foo pt' |
| 356 |
` |
| 357 |
|
| 358 |
b := hugolib.Test(t, files) |
| 359 |
b.AssertFileContent("public/pt/index.html", "foo pt-br|") |
| 360 |
|
| 361 |
files = strings.ReplaceAll(files, "i18n/pt-br.toml", "unused-a.toml") |
| 362 |
|
| 363 |
b = hugolib.Test(t, files) |
| 364 |
b.AssertFileContent("public/pt/index.html", "foo pt|") |
| 365 |
|
| 366 |
files = strings.ReplaceAll(files, "i18n/pt.toml", "unused-b.toml") |
| 367 |
|
| 368 |
b = hugolib.Test(t, files) |
| 369 |
b.AssertFileContent("public/pt/index.html", "foo es-ar|") |
| 370 |
|
| 371 |
files = strings.ReplaceAll(files, "i18n/es-ar.toml", "unused-c.toml") |
| 372 |
|
| 373 |
b = hugolib.Test(t, files) |
| 374 |
b.AssertFileContent("public/pt/index.html", "foo es|") |
| 375 |
|
| 376 |
files = strings.ReplaceAll(files, "i18n/es.toml", "unused-d.toml") |
| 377 |
|
| 378 |
b = hugolib.Test(t, files) |
| 379 |
b.AssertFileContent("public/pt/index.html", "|") |
| 380 |
} |