aboutsummaryrefslogtreecommitdiffhomepage
path: root/langs
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2023-03-04 14:43:23 +0100
committerBjørn Erik Pedersen <[email protected]>2023-03-04 17:19:14 +0100
commit6c798eba607a7e019cc1dfce6fd68be7f770c5bd (patch)
treedeb10a398953f85ecc509585934fe4e0e8445e55 /langs
parentec1c97e7e9d62ce5245135e0906fdedf14af0cae (diff)
downloadhugo-6c798eba607a7e019cc1dfce6fd68be7f770c5bd.tar.gz
hugo-6c798eba607a7e019cc1dfce6fd68be7f770c5bd.zip
Page context handling in i18n
This is a workaround. We need to improve on this, but not today. Fixes #10782
Diffstat (limited to 'langs')
-rw-r--r--langs/i18n/i18n.go18
-rw-r--r--langs/i18n/i18n_test.go8
-rw-r--r--langs/i18n/integration_test.go48
3 files changed, 68 insertions, 6 deletions
diff --git a/langs/i18n/i18n.go b/langs/i18n/i18n.go
index 5594c84cb..b7fdc1060 100644
--- a/langs/i18n/i18n.go
+++ b/langs/i18n/i18n.go
@@ -14,6 +14,7 @@
package i18n
import (
+ "context"
"fmt"
"reflect"
"strings"
@@ -24,11 +25,12 @@ import (
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/helpers"
+ "github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/go-i18n/v2/i18n"
)
-type translateFunc func(translationID string, templateData any) string
+type translateFunc func(ctx context.Context, translationID string, templateData any) string
var i18nWarningLogger = helpers.NewDistinctErrorLogger()
@@ -58,7 +60,7 @@ func (t Translator) Func(lang string) translateFunc {
}
t.logger.Infoln("i18n not initialized; if you need string translations, check that you have a bundle in /i18n that matches the site language or the default language.")
- return func(translationID string, args any) string {
+ return func(ctx context.Context, translationID string, args any) string {
return ""
}
}
@@ -71,7 +73,7 @@ func (t Translator) initFuncs(bndl *i18n.Bundle) {
// This may be pt-BR; make it case insensitive.
currentLangKey := strings.ToLower(strings.TrimPrefix(currentLangStr, artificialLangTagPrefix))
localizer := i18n.NewLocalizer(bndl, currentLangStr)
- t.translateFuncs[currentLangKey] = func(translationID string, templateData any) string {
+ t.translateFuncs[currentLangKey] = func(ctx context.Context, translationID string, templateData any) string {
pluralCount := getPluralCount(templateData)
if templateData != nil {
@@ -81,6 +83,16 @@ func (t Translator) initFuncs(bndl *i18n.Bundle) {
// and we keep it like this to avoid breaking
// lots of sites in the wild.
templateData = intCount(cast.ToInt(templateData))
+ } else {
+ if p, ok := templateData.(page.Page); ok {
+ // See issue 10782.
+ // The i18n has its own template handling and does not know about
+ // the context.Context.
+ // A common pattern is to pass Page to i18n, and use .ReadingTime etc.
+ // We need to improve this, but that requires some upstream changes.
+ // For now, just creata a wrepper.
+ templateData = page.PageWithContext{Page: p, Ctx: ctx}
+ }
}
}
diff --git a/langs/i18n/i18n_test.go b/langs/i18n/i18n_test.go
index 0048d4b1b..cddfaf5a2 100644
--- a/langs/i18n/i18n_test.go
+++ b/langs/i18n/i18n_test.go
@@ -14,6 +14,7 @@
package i18n
import (
+ "context"
"fmt"
"path/filepath"
"testing"
@@ -408,9 +409,10 @@ other = "{{ . }} miesiąca"
c.Assert(d.LoadResources(), qt.IsNil)
f := tp.t.Func(test.lang)
+ ctx := context.Background()
for _, variant := range test.variants {
- c.Assert(f(test.id, variant.Key), qt.Equals, variant.Value, qt.Commentf("input: %v", variant.Key))
+ c.Assert(f(ctx, test.id, variant.Key), qt.Equals, variant.Value, qt.Commentf("input: %v", variant.Key))
c.Assert(int(depsCfg.Logger.LogCounters().WarnCounter.Count()), qt.Equals, 0)
}
@@ -422,7 +424,7 @@ other = "{{ . }} miesiąca"
func doTestI18nTranslate(t testing.TB, test i18nTest, cfg config.Provider) string {
tp := prepareTranslationProvider(t, test, cfg)
f := tp.t.Func(test.lang)
- return f(test.id, test.args)
+ return f(context.Background(), test.id, test.args)
}
type countField struct {
@@ -542,7 +544,7 @@ func BenchmarkI18nTranslate(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
f := tp.t.Func(test.lang)
- actual := f(test.id, test.args)
+ actual := f(context.Background(), test.id, test.args)
if actual != test.expected {
b.Fatalf("expected %v got %v", test.expected, actual)
}
diff --git a/langs/i18n/integration_test.go b/langs/i18n/integration_test.go
index 5599859ee..a433fc4cd 100644
--- a/langs/i18n/integration_test.go
+++ b/langs/i18n/integration_test.go
@@ -55,3 +55,51 @@ l1: {{ i18n "l1" }}|l2: {{ i18n "l2" }}|l3: {{ i18n "l3" }}
l1: l1main|l2: l2main|l3: l3theme
`)
}
+
+func TestPassPageToI18n(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- config.toml --
+-- content/_index.md --
+---
+title: "Home"
+---
+Duis quis irure id nisi sunt minim aliqua occaecat. Aliqua cillum labore consectetur quis culpa tempor quis non officia cupidatat in ad cillum. Velit irure pariatur nisi adipisicing officia reprehenderit commodo esse non.
+
+Ullamco cupidatat nostrud ut reprehenderit. Consequat nisi culpa magna amet tempor velit reprehenderit. Ad minim eiusmod tempor nostrud eu aliquip consectetur commodo ut in aliqua enim. Cupidatat voluptate laborum consequat qui nulla laborum laborum aute ea culpa nulla dolor cillum veniam. Commodo esse tempor qui labore aute aliqua sint nulla do.
+
+Ad deserunt esse nostrud labore. Amet reprehenderit fugiat nostrud eu reprehenderit sit reprehenderit minim deserunt esse id occaecat cillum. Ad qui Lorem cillum laboris ipsum anim in culpa ad dolor consectetur minim culpa.
+
+Lorem cupidatat officia aute in eu commodo anim nulla deserunt occaecat reprehenderit dolore. Eu cupidatat reprehenderit ipsum sit laboris proident. Duis quis nulla tempor adipisicing. Adipisicing amet ad reprehenderit non mollit. Cupidatat proident tempor laborum sit ipsum adipisicing sunt magna labore. Eu irure nostrud cillum exercitation tempor proident. Laborum magna nisi consequat do sint occaecat magna incididunt.
+
+Sit mollit amet esse dolore in labore aliquip eu duis officia incididunt. Esse veniam labore excepteur eiusmod occaecat ullamco magna sunt. Ipsum occaecat exercitation anim fugiat in amet excepteur excepteur aliquip laborum. Aliquip aliqua consequat officia sit sint amet aliqua ipsum eu veniam. Id enim quis ea in eu consequat exercitation occaecat veniam consequat anim nulla adipisicing minim. Ut duis cillum laboris duis non commodo eu aliquip tempor nisi aute do.
+
+Ipsum nulla esse excepteur ut aliqua esse incididunt deserunt veniam dolore est laborum nisi veniam. Magna eiusmod Lorem do tempor incididunt ut aute aliquip ipsum ea laboris culpa. Occaecat do officia velit fugiat culpa eu minim magna sint occaecat sunt. Duis magna proident incididunt est cupidatat proident esse proident ut ipsum non dolor Lorem eiusmod. Officia quis irure id eu aliquip.
+
+Duis anim elit in officia in in aliquip est. Aliquip nisi labore qui elit elit cupidatat ut labore incididunt eiusmod ipsum. Sit irure nulla non cupidatat exercitation sit culpa nisi ex dolore. Culpa nisi duis duis eiusmod commodo nulla.
+
+Et magna aliqua amet qui mollit. Eiusmod aute ut anim ea est fugiat non nisi in laborum ullamco. Proident mollit sunt nostrud irure esse sunt eiusmod deserunt dolor. Irure aute ad magna est consequat duis cupidatat consequat. Enim tempor aute cillum quis ea do enim proident incididunt aliquip cillum tempor minim. Nulla minim tempor proident in excepteur consectetur veniam.
+
+Exercitation tempor nulla incididunt deserunt laboris ad incididunt aliqua exercitation. Adipisicing laboris veniam aute eiusmod qui magna fugiat velit. Aute quis officia anim commodo id fugiat nostrud est. Quis ipsum amet velit adipisicing eu anim minim eu est in culpa aute. Esse in commodo irure enim proident reprehenderit ullamco in dolore aute cillum.
+
+Irure excepteur ex occaecat ipsum laboris fugiat exercitation. Exercitation adipisicing velit excepteur eu culpa consequat exercitation dolore. In laboris aute quis qui mollit minim culpa. Magna velit ea aliquip veniam fugiat mollit veniam.
+-- i18n/en.toml --
+[a]
+other = 'Reading time: {{ .ReadingTime }}'
+-- layouts/index.html --
+i18n: {{ i18n "a" . }}|
+
+`
+
+ b := hugolib.NewIntegrationTestBuilder(
+ hugolib.IntegrationTestConfig{
+ T: t,
+ TxtarString: files,
+ },
+ ).Build()
+
+ b.AssertFileContent("public/index.html", `
+ i18n: Reading time: 3|
+ `)
+}