| 1 |
// Copyright 2021 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 templates_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/hugolib" |
| 20 |
) |
| 21 |
|
| 22 |
func TestExecuteAsTemplateMultipleLanguages(t *testing.T) { |
| 23 |
t.Parallel() |
| 24 |
|
| 25 |
files := ` |
| 26 |
-- hugo.toml -- |
| 27 |
baseURL = "http://example.com/blog" |
| 28 |
defaultContentLanguage = "fr" |
| 29 |
defaultContentLanguageInSubdir = true |
| 30 |
[Languages] |
| 31 |
[Languages.en] |
| 32 |
weight = 10 |
| 33 |
title = "In English" |
| 34 |
label = "English" |
| 35 |
[Languages.fr] |
| 36 |
weight = 20 |
| 37 |
title = "Le Français" |
| 38 |
label = "Français" |
| 39 |
-- i18n/en.toml -- |
| 40 |
[hello] |
| 41 |
other = "Hello" |
| 42 |
-- i18n/fr.toml -- |
| 43 |
[hello] |
| 44 |
other = "Bonjour" |
| 45 |
-- layouts/home.fr.html -- |
| 46 |
Lang: {{ site.Language.Lang }} |
| 47 |
{{ $templ := "{{T \"hello\"}}" | resources.FromString "f1.html" }} |
| 48 |
{{ $helloResource := $templ | resources.ExecuteAsTemplate (print "f%s.html" .Lang) . }} |
| 49 |
Hello1: {{T "hello"}} |
| 50 |
Hello2: {{ $helloResource.Content }} |
| 51 |
LangURL: {{ relLangURL "foo" }} |
| 52 |
-- layouts/home.html -- |
| 53 |
Lang: {{ site.Language.Lang }} |
| 54 |
{{ $templ := "{{T \"hello\"}}" | resources.FromString "f1.html" }} |
| 55 |
{{ $helloResource := $templ | resources.ExecuteAsTemplate (print "f%s.html" .Lang) . }} |
| 56 |
Hello1: {{T "hello"}} |
| 57 |
Hello2: {{ $helloResource.Content }} |
| 58 |
LangURL: {{ relLangURL "foo" }} |
| 59 |
|
| 60 |
` |
| 61 |
|
| 62 |
b := hugolib.Test(t, files) |
| 63 |
|
| 64 |
b.AssertFileContent("public/en/index.html", ` |
| 65 |
Hello1: Hello |
| 66 |
Hello2: Hello |
| 67 |
`) |
| 68 |
|
| 69 |
b.AssertFileContent("public/fr/index.html", ` |
| 70 |
Hello1: Bonjour |
| 71 |
Hello2: Bonjour |
| 72 |
`) |
| 73 |
} |