Skip to content

Commit 23ba779

Browse files
committed
i18n: Support unknown language codes
Fixes #3564
1 parent 550cec0 commit 23ba779

File tree

2 files changed

+74
-10
lines changed

2 files changed

+74
-10
lines changed

‎i18n/i18n_test.go‎

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,24 @@
1414
package i18n
1515

1616
import (
17+
"path/filepath"
1718
"testing"
1819

20+
"github.com/gohugoio/hugo/tpl/tplimpl"
21+
22+
"github.com/spf13/afero"
23+
24+
"github.com/gohugoio/hugo/deps"
25+
1926
"io/ioutil"
2027
"os"
2128

29+
"github.com/gohugoio/hugo/helpers"
30+
2231
"log"
2332

2433
"github.com/gohugoio/hugo/config"
25-
"github.com/nicksnyder/go-i18n/i18n/bundle"
34+
"github.com/gohugoio/hugo/hugofs"
2635
jww "github.com/spf13/jwalterweatherman"
2736
"github.com/spf13/viper"
2837
"github.com/stretchr/testify/require"
@@ -137,22 +146,54 @@ var i18nTests = []i18nTest{
137146
expected: "hello",
138147
expectedFlag: "[i18n] hello",
139148
},
149+
// Unknown language code should get its plural spec from en
150+
{
151+
data: map[string][]byte{
152+
"en.toml": []byte(`[readingTime]
153+
one ="one minute read"
154+
other = "{{.Count}} minutes read"`),
155+
"klingon.toml": []byte(`[readingTime]
156+
one = "eitt minutt med lesing"
157+
other = "{{ .Count }} minuttar lesing"`),
158+
},
159+
args: 3,
160+
lang: "klingon",
161+
id: "readingTime",
162+
expected: "3 minuttar lesing",
163+
expectedFlag: "3 minuttar lesing",
164+
},
140165
}
141166

142167
func doTestI18nTranslate(t *testing.T, test i18nTest, cfg config.Provider) string {
143-
i18nBundle := bundle.New()
168+
assert := require.New(t)
169+
fs := hugofs.NewMem(cfg)
170+
tp := NewTranslationProvider()
171+
depsCfg := newDepsConfig(tp, cfg, fs)
172+
d, err := deps.New(depsCfg)
173+
assert.NoError(err)
144174

145175
for file, content := range test.data {
146-
err := i18nBundle.ParseTranslationFileBytes(file, content)
147-
if err != nil {
148-
t.Errorf("Error parsing translation file: %s", err)
149-
}
176+
err := afero.WriteFile(fs.Source, filepath.Join("i18n", file), []byte(content), 0755)
177+
assert.NoError(err)
150178
}
151179

152-
translator := NewTranslator(i18nBundle, cfg, logger)
153-
f := translator.Func(test.lang)
154-
translated := f(test.id, test.args)
155-
return translated
180+
assert.NoError(d.LoadResources())
181+
f := tp.t.Func(test.lang)
182+
return f(test.id, test.args)
183+
184+
}
185+
186+
func newDepsConfig(tp *TranslationProvider, cfg config.Provider, fs *hugofs.Fs) deps.DepsCfg {
187+
l := helpers.NewLanguage("en", cfg)
188+
l.Set("i18nDir", "i18n")
189+
return deps.DepsCfg{
190+
Language: l,
191+
Cfg: cfg,
192+
Fs: fs,
193+
Logger: logger,
194+
TemplateProvider: tplimpl.DefaultTemplateProvider,
195+
TranslationProvider: tp,
196+
}
156197
}
157198

158199
func TestI18nTranslate(t *testing.T) {

‎i18n/translationProvider.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
package i18n
1515

1616
import (
17+
"errors"
1718
"fmt"
1819

1920
"github.com/gohugoio/hugo/deps"
2021
"github.com/gohugoio/hugo/source"
2122
"github.com/nicksnyder/go-i18n/i18n/bundle"
23+
"github.com/nicksnyder/go-i18n/i18n/language"
2224
)
2325

2426
// TranslationProvider provides translation handling, i.e. loading
@@ -48,6 +50,27 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error {
4850

4951
i18nBundle := bundle.New()
5052

53+
en := language.GetPluralSpec("en")
54+
if en == nil {
55+
return errors.New("The English language has vanished like an old oak table!")
56+
}
57+
var newLangs []string
58+
59+
for _, currentSource := range sources {
60+
for _, r := range currentSource.Files() {
61+
currentSpec := language.GetPluralSpec(r.BaseFileName())
62+
if currentSpec == nil {
63+
// This may is a language code not supported by go-i18n, it may be
64+
// Klingon or ... not even a fake language. Make sure it works.
65+
newLangs = append(newLangs, r.BaseFileName())
66+
}
67+
}
68+
}
69+
70+
if len(newLangs) > 0 {
71+
language.RegisterPluralSpec(newLangs, en)
72+
}
73+
5174
for _, currentSource := range sources {
5275
for _, r := range currentSource.Files() {
5376
err := i18nBundle.ParseTranslationFileBytes(r.LogicalName(), r.Bytes())

0 commit comments

Comments
 (0)