| 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 hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
) |
| 22 |
|
| 23 |
func TestI18n(t *testing.T) { |
| 24 |
c := qt.New(t) |
| 25 |
|
| 26 |
testCases := []struct { |
| 27 |
name string |
| 28 |
langCode string |
| 29 |
}{ |
| 30 |
{ |
| 31 |
name: "pt-br lowercase", |
| 32 |
langCode: "pt-br", |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: "pt-br uppercase", |
| 36 |
langCode: "PT-BR", |
| 37 |
}, |
| 38 |
} |
| 39 |
|
| 40 |
for _, tc := range testCases { |
| 41 |
c.Run(tc.name, func(c *qt.C) { |
| 42 |
files := fmt.Sprintf(` |
| 43 |
-- hugo.toml -- |
| 44 |
baseURL = "https://example.com" |
| 45 |
defaultContentLanguage = "%s" |
| 46 |
|
| 47 |
[languages] |
| 48 |
[languages.%s] |
| 49 |
weight = 1 |
| 50 |
-- i18n/%s.toml -- |
| 51 |
hello.one = "Hello" |
| 52 |
-- layouts/home.html -- |
| 53 |
Hello: {{ i18n "hello" 1 }} |
| 54 |
-- content/p1.md -- |
| 55 |
`, tc.langCode, tc.langCode, tc.langCode) |
| 56 |
|
| 57 |
b := Test(c, files) |
| 58 |
b.AssertFileContent("public/index.html", "Hello: Hello") |
| 59 |
}) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestLanguageBugs(t *testing.T) { |
| 64 |
c := qt.New(t) |
| 65 |
|
| 66 |
// Issue #8672 |
| 67 |
c.Run("Config with language, menu in root only", func(c *qt.C) { |
| 68 |
files := ` |
| 69 |
-- hugo.toml -- |
| 70 |
theme = "test-theme" |
| 71 |
[[menus.foo]] |
| 72 |
name = "foo-a" |
| 73 |
[languages.en] |
| 74 |
-- themes/test-theme/hugo.toml -- |
| 75 |
[languages.en] |
| 76 |
` |
| 77 |
b := Test(c, files) |
| 78 |
|
| 79 |
menus := b.H.Sites[0].Menus() |
| 80 |
c.Assert(menus, qt.HasLen, 1) |
| 81 |
}) |
| 82 |
} |
| 83 |
|
| 84 |
func TestLanguageNumberFormatting(t *testing.T) { |
| 85 |
files := ` |
| 86 |
-- hugo.toml -- |
| 87 |
baseURL = "https://example.org" |
| 88 |
|
| 89 |
defaultContentLanguage = "en" |
| 90 |
defaultContentLanguageInSubDir = true |
| 91 |
|
| 92 |
[languages] |
| 93 |
[languages.en] |
| 94 |
timeZone="UTC" |
| 95 |
weight=10 |
| 96 |
[languages.nn] |
| 97 |
weight=20 |
| 98 |
-- layouts/home.html -- |
| 99 |
|
| 100 |
FormatNumber: {{ 512.5032 | lang.FormatNumber 2 }} |
| 101 |
FormatPercent: {{ 512.5032 | lang.FormatPercent 2 }} |
| 102 |
FormatCurrency: {{ 512.5032 | lang.FormatCurrency 2 "USD" }} |
| 103 |
FormatAccounting: {{ 512.5032 | lang.FormatAccounting 2 "NOK" }} |
| 104 |
FormatNumberCustom: {{ lang.FormatNumberCustom 2 12345.6789 }} |
| 105 |
-- content/p1.md -- |
| 106 |
` |
| 107 |
|
| 108 |
b := Test(t, files) |
| 109 |
|
| 110 |
b.AssertFileContent("public/en/index.html", ` |
| 111 |
FormatNumber: 512.50 |
| 112 |
FormatPercent: 512.50% |
| 113 |
FormatCurrency: $512.50 |
| 114 |
FormatAccounting: NOK512.50 |
| 115 |
FormatNumberCustom: 12,345.68 |
| 116 |
`, |
| 117 |
) |
| 118 |
|
| 119 |
b.AssertFileContent("public/nn/index.html", ` |
| 120 |
FormatNumber: 512,50 |
| 121 |
FormatPercent: 512,50 % |
| 122 |
FormatCurrency: 512,50 USD |
| 123 |
FormatAccounting: 512,50 kr |
| 124 |
FormatNumberCustom: 12,345.68 |
| 125 |
|
| 126 |
`) |
| 127 |
} |
| 128 |
|
| 129 |
// Issue 11993. |
| 130 |
func TestI18nDotFile(t *testing.T) { |
| 131 |
files := ` |
| 132 |
-- hugo.toml -- |
| 133 |
baseURL = "https://example.com" |
| 134 |
-- i18n/.keep -- |
| 135 |
-- data/.keep -- |
| 136 |
` |
| 137 |
Test(t, files) |
| 138 |
} |