From f1e1cdab3fd60e256a18149b4e39e6ba5980c803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 9 Aug 2016 11:41:56 +0200 Subject: [PATCH] Improve i18n string handling * Fall back to default language on missing translation file * Add a i18n-warnings build flag * If that flag is set, print a parseable and greppable string on missing translation strings See #2303 --- commands/hugo.go | 3 ++ docs/content/content/multilingual.md | 6 +++ tpl/template_i18n.go | 58 ++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/commands/hugo.go b/commands/hugo.go index 6c28b78bd..d08851658 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -25,6 +25,8 @@ import ( "sync" "time" + "github.com/spf13/hugo/tpl" + "github.com/spf13/hugo/hugofs" "github.com/spf13/hugo/parser" @@ -242,6 +244,7 @@ func initHugoBuildCommonFlags(cmd *cobra.Command) { cmd.Flags().BoolVar(&preserveTaxonomyNames, "preserveTaxonomyNames", false, `Preserve taxonomy names as written ("GĂ©rard Depardieu" vs "gerard-depardieu")`) cmd.Flags().BoolVarP(&forceSync, "forceSyncStatic", "", false, "Copy all files when static is changed.") cmd.Flags().BoolVarP(&noTimes, "noTimes", "", false, "Don't sync modification time of files") + cmd.Flags().BoolVarP(&tpl.Logi18nWarnings, "i18n-warnings", "", false, "Print missing translations") // Set bash-completion. // Each flag must first be defined before using the SetAnnotation() call. diff --git a/docs/content/content/multilingual.md b/docs/content/content/multilingual.md index f23a6afcf..f5ff46f9c 100644 --- a/docs/content/content/multilingual.md +++ b/docs/content/content/multilingual.md @@ -136,6 +136,12 @@ This uses a definition like this one in `i18n/en-US.yaml`: - id: wordCount translation: "This article has {{ .WordCount }} words." ``` +To track down missing translation strings, run Hugo with the `--i18n-warnings` flag: + +```bash + hugo --i18n-warnings | grep i18n +i18n|MISSING_TRANSLATION|en|wordCount +``` ### Multilingual Themes support diff --git a/tpl/template_i18n.go b/tpl/template_i18n.go index 462b30a80..e46e49bdb 100644 --- a/tpl/template_i18n.go +++ b/tpl/template_i18n.go @@ -17,7 +17,14 @@ import ( "fmt" "github.com/nicksnyder/go-i18n/i18n/bundle" + "github.com/spf13/hugo/helpers" jww "github.com/spf13/jwalterweatherman" + "github.com/spf13/viper" +) + +var ( + Logi18nWarnings bool + i18nWarningLogger = helpers.NewDistinctFeedbackLogger() ) type translate struct { @@ -33,29 +40,48 @@ var translater *translate = &translate{translateFuncs: make(map[string]bundle.Tr func SetTranslateLang(lang string) error { if f, ok := translater.translateFuncs[lang]; ok { translater.current = f - return nil + } else { + jww.WARN.Printf("Translation func for language %v not found, use default.", lang) + translater.current = translater.translateFuncs[viper.GetString("DefaultContentLanguage")] } - jww.WARN.Printf("Translation func for language %v not found", lang) return nil } func SetI18nTfuncs(bndl *bundle.Bundle) { - for _, lang := range bndl.LanguageTags() { - tFunc, err := bndl.Tfunc(lang) - if err == nil { - translater.translateFuncs[lang] = tFunc - continue - } - jww.WARN.Printf("could not load translations for language %q (%s), will not translate!\n", lang, err.Error()) - translater.translateFuncs[lang] = bundle.TranslateFunc(func(id string, args ...interface{}) string { - // TODO: depending on the site mode, we might want to fall back on the default - // language's translation. - // TODO: eventually, we could add --i18n-warnings and print something when - // such things happen. - return fmt.Sprintf("[i18n: %s]", id) - }) + defaultContentLanguage := viper.GetString("DefaultContentLanguage") + var ( + defaultT bundle.TranslateFunc + err error + ) + + defaultT, err = bndl.Tfunc(defaultContentLanguage) + + if err != nil { + jww.WARN.Printf("No translation bundle found for default language %q", defaultContentLanguage) } + for _, lang := range bndl.LanguageTags() { + currentLang := lang + tFunc, err := bndl.Tfunc(currentLang) + + if err != nil { + jww.WARN.Printf("could not load translations for language %q (%s), will use default content language.\n", lang, err) + translater.translateFuncs[currentLang] = defaultT + continue + } + translater.translateFuncs[currentLang] = func(translationID string, args ...interface{}) string { + if translated := tFunc(translationID, args...); translated != translationID { + return translated + } + if Logi18nWarnings { + i18nWarningLogger.Printf("i18n|MISSING_TRANSLATION|%s|%s", currentLang, translationID) + } + if defaultT != nil { + return defaultT(translationID, args...) + } + return fmt.Sprintf("[i18n] %s", translationID) + } + } } func I18nTranslate(id string, args ...interface{}) (string, error) {