github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/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 ` 85 86 pageBundleTemplate := ` 87 --- 88 title: %s 89 weight: %d 90 --- 91 92 Content. 93 94 ` 95 var contentFiles []string 96 section := "sect" 97 98 var contentRoot = func(lang string) string { 99 contentRoot := "content/main" 100 101 switch lang { 102 case "nn": 103 contentRoot = "content/norsk" 104 case "sv": 105 contentRoot = "content/svensk" 106 } 107 return contentRoot + "/" + section 108 } 109 110 for _, lang := range []string{"en", "nn", "sv"} { 111 for j := 1; j <= 10; j++ { 112 if (lang == "nn" || lang == "en") && j%4 == 0 { 113 // Skip 4 and 8 for nn 114 // We also skip it for en, but that is added to the Swedish directory below. 115 continue 116 } 117 118 if lang == "sv" && j%5 == 0 { 119 // Skip 5 and 10 for sv 120 continue 121 } 122 123 base := fmt.Sprintf("p-%s-%d", lang, j) 124 slug := fmt.Sprintf("%s", base) 125 langID := "" 126 127 if lang == "sv" && j%4 == 0 { 128 // Put an English page in the Swedish content dir. 129 langID = ".en" 130 } 131 132 if lang == "en" && j == 8 { 133 // This should win over the sv variant above. 134 langID = ".en" 135 } 136 137 slug += langID 138 139 contentRoot := contentRoot(lang) 140 141 filename := filepath.Join(contentRoot, fmt.Sprintf("page%d%s.md", j, langID)) 142 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, slug, slug, j)) 143 } 144 } 145 146 // Put common translations in all of them 147 for i, lang := range []string{"en", "nn", "sv"} { 148 contentRoot := contentRoot(lang) 149 150 slug := fmt.Sprintf("common_%s", lang) 151 152 filename := filepath.Join(contentRoot, "common.md") 153 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, slug, slug, 100+i)) 154 155 for j, lang2 := range []string{"en", "nn", "sv"} { 156 filename := filepath.Join(contentRoot, fmt.Sprintf("translated_all.%s.md", lang2)) 157 langSlug := slug + "_translated_all_" + lang2 158 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, langSlug, langSlug, 200+i+j)) 159 } 160 161 for j, lang2 := range []string{"sv", "nn"} { 162 if lang == "en" { 163 continue 164 } 165 filename := filepath.Join(contentRoot, fmt.Sprintf("translated_some.%s.md", lang2)) 166 langSlug := slug + "_translated_some_" + lang2 167 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, langSlug, langSlug, 300+i+j)) 168 } 169 } 170 171 // Add a bundle with some images 172 for i, lang := range []string{"en", "nn", "sv"} { 173 contentRoot := contentRoot(lang) 174 slug := fmt.Sprintf("bundle_%s", lang) 175 filename := filepath.Join(contentRoot, "mybundle", "index.md") 176 contentFiles = append(contentFiles, filename, fmt.Sprintf(pageBundleTemplate, slug, 400+i)) 177 if lang == "en" { 178 imageFilename := filepath.Join(contentRoot, "mybundle", "logo.png") 179 contentFiles = append(contentFiles, imageFilename, "PNG Data") 180 } 181 imageFilename := filepath.Join(contentRoot, "mybundle", "featured.png") 182 contentFiles = append(contentFiles, imageFilename, fmt.Sprintf("PNG Data for %s", lang)) 183 184 // Add some bundled pages 185 contentFiles = append(contentFiles, filepath.Join(contentRoot, "mybundle", "p1.md"), fmt.Sprintf(pageBundleTemplate, slug, 401+i)) 186 contentFiles = append(contentFiles, filepath.Join(contentRoot, "mybundle", "sub", "p1.md"), fmt.Sprintf(pageBundleTemplate, slug, 402+i)) 187 188 } 189 190 b := newTestSitesBuilder(t) 191 b.WithWorkingDir("/my/project").WithConfigFile("toml", config).WithContent(contentFiles...).CreateSites() 192 193 _ = os.Stdout 194 //printFs(b.H.BaseFs.ContentFs, "/", os.Stdout) 195 196 b.Build(BuildCfg{}) 197 198 assert.Equal(3, len(b.H.Sites)) 199 200 enSite := b.H.Sites[0] 201 nnSite := b.H.Sites[1] 202 svSite := b.H.Sites[2] 203 204 //dumpPages(nnSite.RegularPages...) 205 assert.Equal(12, len(nnSite.RegularPages)) 206 assert.Equal(13, len(enSite.RegularPages)) 207 208 assert.Equal(10, len(svSite.RegularPages)) 209 210 for i, p := range enSite.RegularPages { 211 j := i + 1 212 msg := fmt.Sprintf("Test %d", j) 213 assert.Equal("en", p.Lang(), msg) 214 assert.Equal("sect", p.Section()) 215 if j < 9 { 216 if j%4 == 0 { 217 assert.Contains(p.Title(), fmt.Sprintf("p-sv-%d.en", i+1), msg) 218 } else { 219 assert.Contains(p.Title(), "p-en", msg) 220 } 221 } 222 } 223 224 // Check bundles 225 bundleEn := enSite.RegularPages[len(enSite.RegularPages)-1] 226 bundleNn := nnSite.RegularPages[len(nnSite.RegularPages)-1] 227 bundleSv := svSite.RegularPages[len(svSite.RegularPages)-1] 228 229 assert.Equal("/en/sect/mybundle/", bundleEn.RelPermalink()) 230 assert.Equal("/sv/sect/mybundle/", bundleSv.RelPermalink()) 231 232 assert.Equal(4, len(bundleEn.Resources)) 233 assert.Equal(4, len(bundleNn.Resources)) 234 assert.Equal(4, len(bundleSv.Resources)) 235 236 assert.Equal("/en/sect/mybundle/logo.png", bundleEn.Resources.GetMatch("logo*").RelPermalink()) 237 assert.Equal("/nn/sect/mybundle/logo.png", bundleNn.Resources.GetMatch("logo*").RelPermalink()) 238 assert.Equal("/sv/sect/mybundle/logo.png", bundleSv.Resources.GetMatch("logo*").RelPermalink()) 239 240 b.AssertFileContent("/my/project/public/sv/sect/mybundle/featured.png", "PNG Data for sv") 241 b.AssertFileContent("/my/project/public/nn/sect/mybundle/featured.png", "PNG Data for nn") 242 b.AssertFileContent("/my/project/public/en/sect/mybundle/featured.png", "PNG Data for en") 243 b.AssertFileContent("/my/project/public/en/sect/mybundle/logo.png", "PNG Data") 244 b.AssertFileContent("/my/project/public/sv/sect/mybundle/logo.png", "PNG Data") 245 b.AssertFileContent("/my/project/public/nn/sect/mybundle/logo.png", "PNG Data") 246 247 nnSect := nnSite.getPage(KindSection, "sect") 248 assert.NotNil(nnSect) 249 assert.Equal(12, len(nnSect.Pages)) 250 nnHome, _ := nnSite.Info.Home() 251 assert.Equal("/nn/", nnHome.RelPermalink()) 252 253 }