github.com/jbramsden/hugo@v0.47.1/hugolib/language_content_dir_test.go (about) 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 hugolib 15 16 import ( 17 "fmt" 18 "os" 19 "path/filepath" 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 ) 24 25 /* 26 27 /en/p1.md 28 /nn/p1.md 29 30 .Readdir 31 32 - Name() => p1.en.md, p1.nn.md 33 34 .Stat(name) 35 36 .Open() --- real file name 37 38 39 */ 40 41 func TestLanguageContentRoot(t *testing.T) { 42 t.Parallel() 43 assert := require.New(t) 44 45 config := ` 46 baseURL = "https://example.org/" 47 48 defaultContentLanguage = "en" 49 defaultContentLanguageInSubdir = true 50 51 contentDir = "content/main" 52 workingDir = "/my/project" 53 54 [Languages] 55 [Languages.en] 56 weight = 10 57 title = "In English" 58 languageName = "English" 59 60 [Languages.nn] 61 weight = 20 62 title = "På Norsk" 63 languageName = "Norsk" 64 # This tells Hugo that all content in this directory is in the Norwegian language. 65 # It does not have to have the "my-page.nn.md" format. It can, but that is optional. 66 contentDir = "content/norsk" 67 68 [Languages.sv] 69 weight = 30 70 title = "På Svenska" 71 languageName = "Svensk" 72 contentDir = "content/svensk" 73 ` 74 75 pageTemplate := ` 76 --- 77 title: %s 78 slug: %s 79 weight: %d 80 --- 81 82 Content. 83 84 SVP3-REF: {{< ref path="/sect/page3.md" lang="sv" >}} 85 SVP3-RELREF: {{< relref path="/sect/page3.md" lang="sv" >}} 86 87 ` 88 89 pageBundleTemplate := ` 90 --- 91 title: %s 92 weight: %d 93 --- 94 95 Content. 96 97 ` 98 var contentFiles []string 99 section := "sect" 100 101 var contentRoot = func(lang string) string { 102 contentRoot := "content/main" 103 104 switch lang { 105 case "nn": 106 contentRoot = "content/norsk" 107 case "sv": 108 contentRoot = "content/svensk" 109 } 110 return contentRoot + "/" + section 111 } 112 113 for _, lang := range []string{"en", "nn", "sv"} { 114 for j := 1; j <= 10; j++ { 115 if (lang == "nn" || lang == "en") && j%4 == 0 { 116 // Skip 4 and 8 for nn 117 // We also skip it for en, but that is added to the Swedish directory below. 118 continue 119 } 120 121 if lang == "sv" && j%5 == 0 { 122 // Skip 5 and 10 for sv 123 continue 124 } 125 126 base := fmt.Sprintf("p-%s-%d", lang, j) 127 slug := fmt.Sprintf("%s", base) 128 langID := "" 129 130 if lang == "sv" && j%4 == 0 { 131 // Put an English page in the Swedish content dir. 132 langID = ".en" 133 } 134 135 if lang == "en" && j == 8 { 136 // This should win over the sv variant above. 137 langID = ".en" 138 } 139 140 slug += langID 141 142 contentRoot := contentRoot(lang) 143 144 filename := filepath.Join(contentRoot, fmt.Sprintf("page%d%s.md", j, langID)) 145 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, slug, slug, j)) 146 } 147 } 148 149 // Put common translations in all of them 150 for i, lang := range []string{"en", "nn", "sv"} { 151 contentRoot := contentRoot(lang) 152 153 slug := fmt.Sprintf("common_%s", lang) 154 155 filename := filepath.Join(contentRoot, "common.md") 156 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, slug, slug, 100+i)) 157 158 for j, lang2 := range []string{"en", "nn", "sv"} { 159 filename := filepath.Join(contentRoot, fmt.Sprintf("translated_all.%s.md", lang2)) 160 langSlug := slug + "_translated_all_" + lang2 161 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, langSlug, langSlug, 200+i+j)) 162 } 163 164 for j, lang2 := range []string{"sv", "nn"} { 165 if lang == "en" { 166 continue 167 } 168 filename := filepath.Join(contentRoot, fmt.Sprintf("translated_some.%s.md", lang2)) 169 langSlug := slug + "_translated_some_" + lang2 170 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, langSlug, langSlug, 300+i+j)) 171 } 172 } 173 174 // Add a bundle with some images 175 for i, lang := range []string{"en", "nn", "sv"} { 176 contentRoot := contentRoot(lang) 177 slug := fmt.Sprintf("bundle_%s", lang) 178 filename := filepath.Join(contentRoot, "mybundle", "index.md") 179 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageBundleTemplate, slug, 400+i)) 180 if lang == "en" { 181 imageFilename := filepath.Join(contentRoot, "mybundle", "logo.png") 182 contentFiles = append(contentFiles, imageFilename, "PNG Data") 183 } 184 imageFilename := filepath.Join(contentRoot, "mybundle", "featured.png") 185 contentFiles = append(contentFiles, imageFilename, fmt.Sprintf("PNG Data for %s", lang)) 186 187 // Add some bundled pages 188 contentFiles = append(contentFiles, filepath.Join(contentRoot, "mybundle", "p1.md"), fmt.Sprintf(pageBundleTemplate, slug, 401+i)) 189 contentFiles = append(contentFiles, filepath.Join(contentRoot, "mybundle", "sub", "p1.md"), fmt.Sprintf(pageBundleTemplate, slug, 402+i)) 190 191 } 192 193 b := newTestSitesBuilder(t) 194 b.WithWorkingDir("/my/project").WithConfigFile("toml", config).WithContent(contentFiles...).CreateSites() 195 196 _ = os.Stdout 197 //printFs(b.H.BaseFs.ContentFs, "/", os.Stdout) 198 199 b.Build(BuildCfg{}) 200 201 assert.Equal(3, len(b.H.Sites)) 202 203 enSite := b.H.Sites[0] 204 nnSite := b.H.Sites[1] 205 svSite := b.H.Sites[2] 206 207 //dumpPages(nnSite.RegularPages...) 208 assert.Equal(12, len(nnSite.RegularPages)) 209 assert.Equal(13, len(enSite.RegularPages)) 210 211 assert.Equal(10, len(svSite.RegularPages)) 212 213 svP2, err := svSite.getPageNew(nil, "/sect/page2.md") 214 assert.NoError(err) 215 nnP2, err := nnSite.getPageNew(nil, "/sect/page2.md") 216 assert.NoError(err) 217 218 enP2, err := enSite.getPageNew(nil, "/sect/page2.md") 219 assert.NoError(err) 220 assert.Equal("en", enP2.Lang()) 221 assert.Equal("sv", svP2.Lang()) 222 assert.Equal("nn", nnP2.Lang()) 223 224 content, _ := nnP2.Content() 225 assert.Contains(content, "SVP3-REF: https://example.org/sv/sect/p-sv-3/") 226 assert.Contains(content, "SVP3-RELREF: /sv/sect/p-sv-3/") 227 228 // Test RelRef with and without language indicator. 229 nn3RefArgs := map[string]interface{}{ 230 "path": "/sect/page3.md", 231 "lang": "nn", 232 } 233 nnP3RelRef, err := svP2.RelRef( 234 nn3RefArgs, 235 ) 236 assert.NoError(err) 237 assert.Equal("/nn/sect/p-nn-3/", nnP3RelRef) 238 nnP3Ref, err := svP2.Ref( 239 nn3RefArgs, 240 ) 241 assert.NoError(err) 242 assert.Equal("https://example.org/nn/sect/p-nn-3/", nnP3Ref) 243 244 for i, p := range enSite.RegularPages { 245 j := i + 1 246 msg := fmt.Sprintf("Test %d", j) 247 assert.Equal("en", p.Lang(), msg) 248 assert.Equal("sect", p.Section()) 249 if j < 9 { 250 if j%4 == 0 { 251 assert.Contains(p.Title(), fmt.Sprintf("p-sv-%d.en", i+1), msg) 252 } else { 253 assert.Contains(p.Title(), "p-en", msg) 254 } 255 } 256 } 257 258 // Check bundles 259 bundleEn := enSite.RegularPages[len(enSite.RegularPages)-1] 260 bundleNn := nnSite.RegularPages[len(nnSite.RegularPages)-1] 261 bundleSv := svSite.RegularPages[len(svSite.RegularPages)-1] 262 263 assert.Equal("/en/sect/mybundle/", bundleEn.RelPermalink()) 264 assert.Equal("/sv/sect/mybundle/", bundleSv.RelPermalink()) 265 266 assert.Equal(4, len(bundleEn.Resources)) 267 assert.Equal(4, len(bundleNn.Resources)) 268 assert.Equal(4, len(bundleSv.Resources)) 269 270 assert.Equal("/en/sect/mybundle/logo.png", bundleEn.Resources.GetMatch("logo*").RelPermalink()) 271 assert.Equal("/nn/sect/mybundle/logo.png", bundleNn.Resources.GetMatch("logo*").RelPermalink()) 272 assert.Equal("/sv/sect/mybundle/logo.png", bundleSv.Resources.GetMatch("logo*").RelPermalink()) 273 274 b.AssertFileContent("/my/project/public/sv/sect/mybundle/featured.png", "PNG Data for sv") 275 b.AssertFileContent("/my/project/public/nn/sect/mybundle/featured.png", "PNG Data for nn") 276 b.AssertFileContent("/my/project/public/en/sect/mybundle/featured.png", "PNG Data for en") 277 b.AssertFileContent("/my/project/public/en/sect/mybundle/logo.png", "PNG Data") 278 b.AssertFileContent("/my/project/public/sv/sect/mybundle/logo.png", "PNG Data") 279 b.AssertFileContent("/my/project/public/nn/sect/mybundle/logo.png", "PNG Data") 280 281 nnSect := nnSite.getPage(KindSection, "sect") 282 assert.NotNil(nnSect) 283 assert.Equal(12, len(nnSect.Pages)) 284 nnHome, _ := nnSite.Info.Home() 285 assert.Equal("/nn/", nnHome.RelPermalink()) 286 287 }