Permalink
Browse files

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
  • Loading branch information...
bep committed Aug 9, 2016
1 parent 8da0403 commit f1e1cdab3fd60e256a18149b4e39e6ba5980c803
Showing with 49 additions and 14 deletions.
  1. +3 −0 commands/hugo.go
  2. +6 −0 docs/content/content/multilingual.md
  3. +40 −14 tpl/template_i18n.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.
@@ -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
@@ -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) {
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() {
tFunc, err := bndl.Tfunc(lang)
if err == nil {
translater.translateFuncs[lang] = tFunc
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
}
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)
})
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) {

0 comments on commit f1e1cda

Please sign in to comment.