"github.com/gohugoio/hugo/helpers"
"github.com/nicksnyder/go-i18n/i18n/bundle"
+ "github.com/nicksnyder/go-i18n/i18n/translation"
)
var (
t.logger.INFO.Printf("No translation bundle found for default language %q", defaultContentLanguage)
}
+ translations := bndl.Translations()
+
enableMissingTranslationPlaceholders := t.cfg.GetBool("enableMissingTranslationPlaceholders")
for _, lang := range bndl.LanguageTags() {
currentLang := lang
// then Tfunc returns translationID itself.
// But if user set same translationID and translation, we should check
// if it really untranslated:
- if isIDTranslated(currentLang, translationID, bndl) {
+ if isIDTranslated(translations, currentLang, translationID) {
return translated
}
if translated != translationID {
return translated
}
- if isIDTranslated(defaultContentLanguage, translationID, bndl) {
+ if isIDTranslated(translations, defaultContentLanguage, translationID) {
return translated
}
}
}
}
-// If bndl contains the translationID for specified currentLang,
+// If the translation map contains translationID for specified currentLang,
// then the translationID is actually translated.
-func isIDTranslated(lang, id string, b *bundle.Bundle) bool {
- _, contains := b.Translations()[lang][id]
+func isIDTranslated(translations map[string]map[string]translation.Translation, lang, id string) bool {
+ _, contains := translations[lang][id]
return contains
}
"github.com/gohugoio/hugo/htesting"
"github.com/gohugoio/hugo/langs"
"github.com/spf13/afero"
+ "github.com/spf13/viper"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/hugofs"
- "github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
var logger = loggers.NewErrorLogger()
type i18nTest struct {
+ name string
data map[string][]byte
args interface{}
lang, id, expected, expectedFlag string
var i18nTests = []i18nTest{
// All translations present
{
+ name: "all-present",
data: map[string][]byte{
"en.toml": []byte("[hello]\nother = \"Hello, World!\""),
"es.toml": []byte("[hello]\nother = \"¡Hola, Mundo!\""),
},
// Translation missing in current language but present in default
{
+ name: "present-in-default",
data: map[string][]byte{
"en.toml": []byte("[hello]\nother = \"Hello, World!\""),
"es.toml": []byte("[goodbye]\nother = \"¡Adiós, Mundo!\""),
},
// Translation missing in default language but present in current
{
+ name: "present-in-current",
data: map[string][]byte{
"en.toml": []byte("[goodbye]\nother = \"Goodbye, World!\""),
"es.toml": []byte("[hello]\nother = \"¡Hola, Mundo!\""),
},
// Translation missing in both default and current language
{
+ name: "missing",
data: map[string][]byte{
"en.toml": []byte("[goodbye]\nother = \"Goodbye, World!\""),
"es.toml": []byte("[goodbye]\nother = \"¡Adiós, Mundo!\""),
},
// Default translation file missing or empty
{
+ name: "file-missing",
data: map[string][]byte{
"en.toml": []byte(""),
},
},
// Context provided
{
+ name: "context-provided",
data: map[string][]byte{
"en.toml": []byte("[wordCount]\nother = \"Hello, {{.WordCount}} people!\""),
"es.toml": []byte("[wordCount]\nother = \"¡Hola, {{.WordCount}} gente!\""),
// Same id and translation in current language
// https://github.com/gohugoio/hugo/issues/2607
{
+ name: "same-id-and-translation",
data: map[string][]byte{
"es.toml": []byte("[hello]\nother = \"hello\""),
"en.toml": []byte("[hello]\nother = \"hi\""),
},
// Translation missing in current language, but same id and translation in default
{
+ name: "same-id-and-translation-default",
data: map[string][]byte{
"es.toml": []byte("[bye]\nother = \"bye\""),
"en.toml": []byte("[hello]\nother = \"hello\""),
},
// Unknown language code should get its plural spec from en
{
+ name: "unknown-language-code",
data: map[string][]byte{
"en.toml": []byte(`[readingTime]
one ="one minute read"
},
}
-func doTestI18nTranslate(t *testing.T, test i18nTest, cfg config.Provider) string {
+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)
+
+}
+
+func prepareTranslationProvider(t testing.TB, test i18nTest, cfg config.Provider) *TranslationProvider {
assert := require.New(t)
fs := hugofs.NewMem(cfg)
- tp := NewTranslationProvider()
for file, content := range test.data {
err := afero.WriteFile(fs.Source, filepath.Join("i18n", file), []byte(content), 0755)
assert.NoError(err)
}
+ tp := NewTranslationProvider()
depsCfg := newDepsConfig(tp, cfg, fs)
d, err := deps.New(depsCfg)
assert.NoError(err)
-
assert.NoError(d.LoadResources())
- f := tp.t.Func(test.lang)
- return f(test.id, test.args)
+ return tp
}
func newDepsConfig(tp *TranslationProvider, cfg config.Provider, fs *hugofs.Fs) deps.DepsCfg {
}
}
-func TestI18nTranslate(t *testing.T) {
- var actual, expected string
+func getConfig() *viper.Viper {
v := viper.New()
v.SetDefault("defaultContentLanguage", "en")
v.Set("contentDir", "content")
v.Set("assetDir", "assets")
v.Set("resourceDir", "resources")
v.Set("publishDir", "public")
+ return v
+
+}
+
+func TestI18nTranslate(t *testing.T) {
+ var actual, expected string
+ v := getConfig()
// Test without and with placeholders
for _, enablePlaceholders := range []bool{false, true} {
}
}
}
+
+func BenchmarkI18nTranslate(b *testing.B) {
+ v := getConfig()
+ for _, test := range i18nTests {
+ b.Run(test.name, func(b *testing.B) {
+ tp := prepareTranslationProvider(b, test, v)
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ f := tp.t.Func(test.lang)
+ actual := f(test.id, test.args)
+ if actual != test.expected {
+ b.Fatalf("expected %v got %v", test.expected, actual)
+ }
+ }
+ })
+ }
+
+}