Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ require (
github.com/gobuffalo/flect v1.0.3
github.com/gobwas/glob v0.2.3
github.com/goccy/go-yaml v1.18.0
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20251018145728-cfcc22d823c6
github.com/gohugoio/hashstructure v0.6.0
github.com/gohugoio/httpcache v0.8.0
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0
Expand All @@ -56,6 +55,7 @@ require (
github.com/microcosm-cc/bluemonday v1.0.27
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c
github.com/muesli/smartcrop v0.3.0
github.com/nicksnyder/go-i18n/v2 v2.6.0
github.com/niklasfasching/go-org v1.9.1
github.com/olekukonko/tablewriter v1.1.0
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20251018145728-cfcc22d823c6 h1:pxlAea9eRwuAnt/zKbGqlFO2ZszpIe24YpOVLf+N+4I=
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20251018145728-cfcc22d823c6/go.mod h1:m5hu1im5Qc7LDycVLvee6MPobJiRLBYHklypFJR0/aE=
github.com/gohugoio/hashstructure v0.6.0 h1:7wMB/2CfXoThFYhdWRGv3u3rUM761Cq29CxUW+NltUg=
github.com/gohugoio/hashstructure v0.6.0/go.mod h1:lapVLk9XidheHG1IQ4ZSbyYrXcaILU1ZEP/+vno5rBQ=
github.com/gohugoio/httpcache v0.8.0 h1:hNdsmGSELztetYCsPVgjA960zSa4dfEqqF/SficorCU=
Expand Down Expand Up @@ -433,6 +431,8 @@ github.com/muesli/smartcrop v0.3.0/go.mod h1:i2fCI/UorTfgEpPPLWiFBv4pye+YAG78Rwc
github.com/neurosnap/sentences v1.0.6/go.mod h1:pg1IapvYpWCJJm/Etxeh0+gtMf1rI1STY9S7eUCPbDc=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/nicksnyder/go-i18n/v2 v2.6.0 h1:C/m2NNWNiTB6SK4Ao8df5EWm3JETSTIGNXBpMJTxzxQ=
github.com/nicksnyder/go-i18n/v2 v2.6.0/go.mod h1:88sRqr0C6OPyJn0/KRNaEz1uWorjxIKP7rUUcvycecE=
github.com/niklasfasching/go-org v1.9.1 h1:/3s4uTPOF06pImGa2Yvlp24yKXZoTYM+nsIlMzfpg/0=
github.com/niklasfasching/go-org v1.9.1/go.mod h1:ZAGFFkWvUQcpazmi/8nHqwvARpr1xpb+Es67oUGX/48=
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//JalHPu/3yz+De2J+4aLtSRlHiY=
Expand Down
33 changes: 20 additions & 13 deletions langs/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/resources/page"

"github.com/gohugoio/go-i18n/v2/i18n"
"github.com/nicksnyder/go-i18n/v2/i18n"
)

type translateFunc func(ctx context.Context, translationID string, templateData any) string
Expand All @@ -38,8 +38,8 @@ type Translator struct {
logger loggers.Logger
}

// NewTranslator creates a new Translator for the given language bundle and configuration.
func NewTranslator(b *i18n.Bundle, cfg config.AllProvider, logger loggers.Logger) Translator {
// newTranslator creates a new Translator for the given language bundle and configuration.
func newTranslator(b *bundle, cfg config.AllProvider, logger loggers.Logger) Translator {
t := Translator{cfg: cfg, logger: logger, translateFuncs: make(map[string]translateFunc)}
t.initFuncs(b)
return t
Expand All @@ -62,14 +62,21 @@ func (t Translator) Func(lang string) translateFunc {
}
}

func (t Translator) initFuncs(bndl *i18n.Bundle) {
func (t Translator) initFuncs(bndl *bundle) {
enableMissingTranslationPlaceholders := t.cfg.EnableMissingTranslationPlaceholders()
for _, lang := range bndl.LanguageTags() {
currentLang := lang
currentLangStr := currentLang.String()
// This may be pt-BR; make it case insensitive.
currentLangKey := strings.ToLower(strings.TrimPrefix(currentLangStr, artificialLangTagPrefix))
localizer := i18n.NewLocalizer(bndl, currentLangStr)
for _, lang := range bndl.b.LanguageTags() {
currentLangTag := lang
currentLangTagStr := currentLangTag.String()

var currentLangKey string
if undefinedLangKey, found := bndl.undefinedLangs[currentLangTag]; found {
currentLangKey = strings.ToLower(undefinedLangKey)
} else {
// This may be pt-BR; make it case insensitive.
currentLangKey = strings.ToLower(currentLangTagStr)
}

localizer := i18n.NewLocalizer(bndl.b, currentLangTagStr)
t.translateFuncs[currentLangKey] = func(ctx context.Context, translationID string, templateData any) string {
pluralCount := getPluralCount(templateData)

Expand Down Expand Up @@ -99,7 +106,7 @@ func (t Translator) initFuncs(bndl *i18n.Bundle) {
PluralCount: pluralCount,
})

sameLang := currentLang == translatedLang
sameLang := currentLangTag == translatedLang

if err == nil && sameLang {
return translated
Expand All @@ -116,11 +123,11 @@ func (t Translator) initFuncs(bndl *i18n.Bundle) {
}

if _, ok := err.(*i18n.MessageNotFoundErr); !ok {
t.logger.Warnf("Failed to get translated string for language %q and ID %q: %s", currentLangStr, translationID, err)
t.logger.Warnf("Failed to get translated string for language %q and ID %q: %s", currentLangTagStr, translationID, err)
}

if t.cfg.PrintI18nWarnings() {
t.logger.Warnf("i18n|MISSING_TRANSLATION|%s|%s", currentLangStr, translationID)
t.logger.Warnf("i18n|MISSING_TRANSLATION|%s|%s", currentLangTagStr, translationID)
}

if enableMissingTranslationPlaceholders {
Expand Down
33 changes: 33 additions & 0 deletions langs/i18n/i18n_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package i18n_test

import (
"fmt"
"strings"
"testing"

qt "github.com/frankban/quicktest"
Expand Down Expand Up @@ -165,3 +167,34 @@ b = 'b translated'
b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, "failed to load translations: reserved keys [description] mixed with unreserved keys [a b]: see the lang.Translate documentation for a list of reserved keys")
}

func TestPrivateUseSubTagsLength(t *testing.T) {
t.Parallel()
filesTemplate := `
-- hugo.toml --
[languages."LANG"]
weight = 1
-- i18n/LANG.toml --
[hello]
other = "Hello World"
-- layouts/index.html --
{{ T "hello" }}
`

doTest := func(lang string, ok bool) {
files := strings.ReplaceAll(filesTemplate, "LANG", lang)
b, err := hugolib.TestE(t, files)
if ok {
b.Assert(err, qt.IsNil)
b.AssertFileContent("public/index.html", `Hello World`)
} else {
b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, fmt.Sprintf(`%q: language: tag is not well-formed`, lang))
}
}

doTest("art-x-12345678", true)
doTest("12345678", true)
doTest("art-x-123456789", false)
doTest("123456789", false)
}
Loading
Loading