|
14 | 14 | package i18n |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "path/filepath" |
17 | 18 | "testing" |
18 | 19 |
|
| 20 | + "github.com/gohugoio/hugo/tpl/tplimpl" |
| 21 | + |
| 22 | + "github.com/spf13/afero" |
| 23 | + |
| 24 | + "github.com/gohugoio/hugo/deps" |
| 25 | + |
19 | 26 | "io/ioutil" |
20 | 27 | "os" |
21 | 28 |
|
| 29 | + "github.com/gohugoio/hugo/helpers" |
| 30 | + |
22 | 31 | "log" |
23 | 32 |
|
24 | 33 | "github.com/gohugoio/hugo/config" |
25 | | - "github.com/nicksnyder/go-i18n/i18n/bundle" |
| 34 | + "github.com/gohugoio/hugo/hugofs" |
26 | 35 | jww "github.com/spf13/jwalterweatherman" |
27 | 36 | "github.com/spf13/viper" |
28 | 37 | "github.com/stretchr/testify/require" |
@@ -137,22 +146,54 @@ var i18nTests = []i18nTest{ |
137 | 146 | expected: "hello", |
138 | 147 | expectedFlag: "[i18n] hello", |
139 | 148 | }, |
| 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 | + }, |
140 | 165 | } |
141 | 166 |
|
142 | 167 | 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) |
144 | 174 |
|
145 | 175 | 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) |
150 | 178 | } |
151 | 179 |
|
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 | + } |
156 | 197 | } |
157 | 198 |
|
158 | 199 | func TestI18nTranslate(t *testing.T) { |
|
0 commit comments