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
This commit is contained in:
Bjørn Erik Pedersen 2016-08-09 11:41:56 +02:00
parent 8da040342e
commit f1e1cdab3f
3 changed files with 51 additions and 16 deletions

View file

@ -25,6 +25,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/spf13/hugo/tpl"
"github.com/spf13/hugo/hugofs" "github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/parser" "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().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(&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(&noTimes, "noTimes", "", false, "Don't sync modification time of files")
cmd.Flags().BoolVarP(&tpl.Logi18nWarnings, "i18n-warnings", "", false, "Print missing translations")
// Set bash-completion. // Set bash-completion.
// Each flag must first be defined before using the SetAnnotation() call. // Each flag must first be defined before using the SetAnnotation() call.

View file

@ -136,6 +136,12 @@ This uses a definition like this one in `i18n/en-US.yaml`:
- id: wordCount - id: wordCount
translation: "This article has {{ .WordCount }} words." 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 ### Multilingual Themes support

View file

@ -17,7 +17,14 @@ import (
"fmt" "fmt"
"github.com/nicksnyder/go-i18n/i18n/bundle" "github.com/nicksnyder/go-i18n/i18n/bundle"
"github.com/spf13/hugo/helpers"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)
var (
Logi18nWarnings bool
i18nWarningLogger = helpers.NewDistinctFeedbackLogger()
) )
type translate struct { type translate struct {
@ -33,29 +40,48 @@ var translater *translate = &translate{translateFuncs: make(map[string]bundle.Tr
func SetTranslateLang(lang string) error { func SetTranslateLang(lang string) error {
if f, ok := translater.translateFuncs[lang]; ok { if f, ok := translater.translateFuncs[lang]; ok {
translater.current = f 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 return nil
} }
func SetI18nTfuncs(bndl *bundle.Bundle) { func SetI18nTfuncs(bndl *bundle.Bundle) {
for _, lang := range bndl.LanguageTags() { defaultContentLanguage := viper.GetString("DefaultContentLanguage")
tFunc, err := bndl.Tfunc(lang) var (
if err == nil { defaultT bundle.TranslateFunc
translater.translateFuncs[lang] = tFunc err error
continue )
}
jww.WARN.Printf("could not load translations for language %q (%s), will not translate!\n", lang, err.Error()) defaultT, err = bndl.Tfunc(defaultContentLanguage)
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 if err != nil {
// language's translation. jww.WARN.Printf("No translation bundle found for default language %q", defaultContentLanguage)
// TODO: eventually, we could add --i18n-warnings and print something when
// such things happen.
return fmt.Sprintf("[i18n: %s]", id)
})
} }
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) { func I18nTranslate(id string, args ...interface{}) (string, error) {