Skip to content

Commit ffc2d80

Browse files
committed
deps: Move from github.com/gohugoio/go-i18n/v2 => github.com/nicksnyder/go-i18n/v2
Closes #14064
1 parent 9425b93 commit ffc2d80

File tree

5 files changed

+262
-45
lines changed

5 files changed

+262
-45
lines changed

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ require (
3636
github.com/gobuffalo/flect v1.0.3
3737
github.com/gobwas/glob v0.2.3
3838
github.com/goccy/go-yaml v1.18.0
39-
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20251018145728-cfcc22d823c6
4039
github.com/gohugoio/hashstructure v0.6.0
4140
github.com/gohugoio/httpcache v0.8.0
4241
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0
@@ -56,6 +55,7 @@ require (
5655
github.com/microcosm-cc/bluemonday v1.0.27
5756
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c
5857
github.com/muesli/smartcrop v0.3.0
58+
github.com/nicksnyder/go-i18n/v2 v2.6.0
5959
github.com/niklasfasching/go-org v1.9.1
6060
github.com/olekukonko/tablewriter v1.1.0
6161
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@ github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
267267
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
268268
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
269269
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
270-
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20251018145728-cfcc22d823c6 h1:pxlAea9eRwuAnt/zKbGqlFO2ZszpIe24YpOVLf+N+4I=
271-
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20251018145728-cfcc22d823c6/go.mod h1:m5hu1im5Qc7LDycVLvee6MPobJiRLBYHklypFJR0/aE=
272270
github.com/gohugoio/hashstructure v0.6.0 h1:7wMB/2CfXoThFYhdWRGv3u3rUM761Cq29CxUW+NltUg=
273271
github.com/gohugoio/hashstructure v0.6.0/go.mod h1:lapVLk9XidheHG1IQ4ZSbyYrXcaILU1ZEP/+vno5rBQ=
274272
github.com/gohugoio/httpcache v0.8.0 h1:hNdsmGSELztetYCsPVgjA960zSa4dfEqqF/SficorCU=
@@ -433,6 +431,8 @@ github.com/muesli/smartcrop v0.3.0/go.mod h1:i2fCI/UorTfgEpPPLWiFBv4pye+YAG78Rwc
433431
github.com/neurosnap/sentences v1.0.6/go.mod h1:pg1IapvYpWCJJm/Etxeh0+gtMf1rI1STY9S7eUCPbDc=
434432
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
435433
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
434+
github.com/nicksnyder/go-i18n/v2 v2.6.0 h1:C/m2NNWNiTB6SK4Ao8df5EWm3JETSTIGNXBpMJTxzxQ=
435+
github.com/nicksnyder/go-i18n/v2 v2.6.0/go.mod h1:88sRqr0C6OPyJn0/KRNaEz1uWorjxIKP7rUUcvycecE=
436436
github.com/niklasfasching/go-org v1.9.1 h1:/3s4uTPOF06pImGa2Yvlp24yKXZoTYM+nsIlMzfpg/0=
437437
github.com/niklasfasching/go-org v1.9.1/go.mod h1:ZAGFFkWvUQcpazmi/8nHqwvARpr1xpb+Es67oUGX/48=
438438
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//JalHPu/3yz+De2J+4aLtSRlHiY=

‎langs/i18n/i18n.go‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/gohugoio/hugo/config"
2727
"github.com/gohugoio/hugo/resources/page"
2828

29-
"github.com/gohugoio/go-i18n/v2/i18n"
29+
"github.com/nicksnyder/go-i18n/v2/i18n"
3030
)
3131

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

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

65-
func (t Translator) initFuncs(bndl *i18n.Bundle) {
65+
func (t Translator) initFuncs(bndl *bundle) {
6666
enableMissingTranslationPlaceholders := t.cfg.EnableMissingTranslationPlaceholders()
67-
for _, lang := range bndl.LanguageTags() {
68-
currentLang := lang
69-
currentLangStr := currentLang.String()
70-
// This may be pt-BR; make it case insensitive.
71-
currentLangKey := strings.ToLower(strings.TrimPrefix(currentLangStr, artificialLangTagPrefix))
72-
localizer := i18n.NewLocalizer(bndl, currentLangStr)
67+
for _, lang := range bndl.b.LanguageTags() {
68+
currentLangTag := lang
69+
currentLangTagStr := currentLangTag.String()
70+
71+
var currentLangKey string
72+
if undefinedLangKey, found := bndl.undefinedLangs[currentLangTag]; found {
73+
currentLangKey = strings.ToLower(undefinedLangKey)
74+
} else {
75+
// This may be pt-BR; make it case insensitive.
76+
currentLangKey = strings.ToLower(currentLangTagStr)
77+
}
78+
79+
localizer := i18n.NewLocalizer(bndl.b, currentLangTagStr)
7380
t.translateFuncs[currentLangKey] = func(ctx context.Context, translationID string, templateData any) string {
7481
pluralCount := getPluralCount(templateData)
7582

@@ -99,7 +106,7 @@ func (t Translator) initFuncs(bndl *i18n.Bundle) {
99106
PluralCount: pluralCount,
100107
})
101108

102-
sameLang := currentLang == translatedLang
109+
sameLang := currentLangTag == translatedLang
103110

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

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

122129
if t.cfg.PrintI18nWarnings() {
123-
t.logger.Warnf("i18n|MISSING_TRANSLATION|%s|%s", currentLangStr, translationID)
130+
t.logger.Warnf("i18n|MISSING_TRANSLATION|%s|%s", currentLangTagStr, translationID)
124131
}
125132

126133
if enableMissingTranslationPlaceholders {

‎langs/i18n/i18n_integration_test.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package i18n_test
1515

1616
import (
17+
"fmt"
18+
"strings"
1719
"testing"
1820

1921
qt "github.com/frankban/quicktest"
@@ -165,3 +167,34 @@ b = 'b translated'
165167
b.Assert(err, qt.IsNotNil)
166168
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")
167169
}
170+
171+
func TestPrivateUseSubTagsLength(t *testing.T) {
172+
t.Parallel()
173+
filesTemplate := `
174+
-- hugo.toml --
175+
[languages."LANG"]
176+
weight = 1
177+
-- i18n/LANG.toml --
178+
[hello]
179+
other = "Hello World"
180+
-- layouts/index.html --
181+
{{ T "hello" }}
182+
`
183+
184+
doTest := func(lang string, ok bool) {
185+
files := strings.ReplaceAll(filesTemplate, "LANG", lang)
186+
b, err := hugolib.TestE(t, files)
187+
if ok {
188+
b.Assert(err, qt.IsNil)
189+
b.AssertFileContent("public/index.html", `Hello World`)
190+
} else {
191+
b.Assert(err, qt.IsNotNil)
192+
b.Assert(err.Error(), qt.Contains, fmt.Sprintf(`%q: language: tag is not well-formed`, lang))
193+
}
194+
}
195+
196+
doTest("art-x-12345678", true)
197+
doTest("12345678", true)
198+
doTest("art-x-123456789", false)
199+
doTest("123456789", false)
200+
}

0 commit comments

Comments
 (0)