diff options
author | Bjørn Erik Pedersen <[email protected]> | 2019-06-02 11:11:46 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2020-09-29 17:48:07 +0200 |
commit | 97987e5c0254e35668dca7f89e67b79553e617c8 (patch) | |
tree | 3ea30314a22d25f8ffa0caad5db129fc977657b0 /langs | |
parent | 111344113bf8c16ae45528d67ff408da15961727 (diff) | |
download | hugo-97987e5c0254e35668dca7f89e67b79553e617c8.tar.gz hugo-97987e5c0254e35668dca7f89e67b79553e617c8.zip |
langs/i18n: Upgrade to go-i18n v2
Fixes #5242
Diffstat (limited to 'langs')
-rw-r--r-- | langs/i18n/i18n.go | 90 | ||||
-rw-r--r-- | langs/i18n/translationProvider.go | 58 |
2 files changed, 70 insertions, 78 deletions
diff --git a/langs/i18n/i18n.go b/langs/i18n/i18n.go index 5beef8683..922b06367 100644 --- a/langs/i18n/i18n.go +++ b/langs/i18n/i18n.go @@ -14,35 +14,40 @@ package i18n import ( + "reflect" + "strings" + + "github.com/gohugoio/hugo/common/hreflect" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/helpers" - "github.com/nicksnyder/go-i18n/i18n/bundle" - "github.com/nicksnyder/go-i18n/i18n/translation" + "github.com/nicksnyder/go-i18n/v2/i18n" ) +type translateFunc func(translationID string, templateData interface{}) string + var ( i18nWarningLogger = helpers.NewDistinctFeedbackLogger() ) // Translator handles i18n translations. type Translator struct { - translateFuncs map[string]bundle.TranslateFunc + translateFuncs map[string]translateFunc cfg config.Provider logger *loggers.Logger } // NewTranslator creates a new Translator for the given language bundle and configuration. -func NewTranslator(b *bundle.Bundle, cfg config.Provider, logger *loggers.Logger) Translator { - t := Translator{cfg: cfg, logger: logger, translateFuncs: make(map[string]bundle.TranslateFunc)} +func NewTranslator(b *i18n.Bundle, cfg config.Provider, logger *loggers.Logger) Translator { + t := Translator{cfg: cfg, logger: logger, translateFuncs: make(map[string]translateFunc)} t.initFuncs(b) return t } // Func gets the translate func for the given language, or for the default // configured language if not found. -func (t Translator) Func(lang string) bundle.TranslateFunc { +func (t Translator) Func(lang string) translateFunc { if f, ok := t.translateFuncs[lang]; ok { return f } @@ -50,68 +55,57 @@ func (t Translator) Func(lang string) bundle.TranslateFunc { if f, ok := t.translateFuncs[t.cfg.GetString("defaultContentLanguage")]; ok { return f } + t.logger.INFO.Println("i18n not initialized; if you need string translations, check that you have a bundle in /i18n that matches the site language or the default language.") - return func(translationID string, args ...interface{}) string { + return func(translationID string, args interface{}) string { return "" } } -func (t Translator) initFuncs(bndl *bundle.Bundle) { - defaultContentLanguage := t.cfg.GetString("defaultContentLanguage") - - defaultT, err := bndl.Tfunc(defaultContentLanguage) - if err != nil { - t.logger.INFO.Printf("No translation bundle found for default language %q", defaultContentLanguage) - } - - translations := bndl.Translations() - +func (t Translator) initFuncs(bndl *i18n.Bundle) { enableMissingTranslationPlaceholders := t.cfg.GetBool("enableMissingTranslationPlaceholders") for _, lang := range bndl.LanguageTags() { - currentLang := lang - t.translateFuncs[currentLang] = func(translationID string, args ...interface{}) string { - tFunc, err := bndl.Tfunc(currentLang) - if err != nil { - t.logger.WARN.Printf("could not load translations for language %q (%s), will use default content language.\n", lang, err) + currentLang := lang + currentLangStr := currentLang.String() + currentLangKey := strings.TrimPrefix(currentLangStr, artificialLangTagPrefix) + localizer := i18n.NewLocalizer(bndl, currentLangStr) + + t.translateFuncs[currentLangKey] = func(translationID string, templateData interface{}) string { + + if templateData != nil { + tp := reflect.TypeOf(templateData) + if hreflect.IsNumber(tp.Kind()) { + // This was how go-i18n worked in v1. + templateData = map[string]interface{}{ + "Count": templateData, + } + } } - translated := tFunc(translationID, args...) - if translated != translationID { + translated, translatedLang, err := localizer.LocalizeWithTag(&i18n.LocalizeConfig{ + MessageID: translationID, + TemplateData: templateData, + }) + + if err == nil && currentLang == translatedLang { return translated } - // If there is no translation for translationID, - // then Tfunc returns translationID itself. - // But if user set same translationID and translation, we should check - // if it really untranslated: - if isIDTranslated(translations, currentLang, translationID) { - return translated + + if _, ok := err.(*i18n.MessageNotFoundErr); !ok { + t.logger.WARN.Printf("Failed to get translated string for language %q and ID %q: %s", currentLangStr, translationID, err) } if t.cfg.GetBool("logI18nWarnings") { - i18nWarningLogger.Printf("i18n|MISSING_TRANSLATION|%s|%s", currentLang, translationID) + i18nWarningLogger.Printf("i18n|MISSING_TRANSLATION|%s|%s", currentLangStr, translationID) } + if enableMissingTranslationPlaceholders { return "[i18n] " + translationID } - if defaultT != nil { - translated := defaultT(translationID, args...) - if translated != translationID { - return translated - } - if isIDTranslated(translations, defaultContentLanguage, translationID) { - return translated - } - } - return "" + + return translated } } } - -// If the translation map contains translationID for specified currentLang, -// then the translationID is actually translated. -func isIDTranslated(translations map[string]map[string]translation.Translation, lang, id string) bool { - _, contains := translations[lang][id] - return contains -} diff --git a/langs/i18n/translationProvider.go b/langs/i18n/translationProvider.go index 4ce9b59fe..d191c0077 100644 --- a/langs/i18n/translationProvider.go +++ b/langs/i18n/translationProvider.go @@ -14,16 +14,19 @@ package i18n import ( - "errors" + "encoding/json" "github.com/gohugoio/hugo/common/herrors" + "golang.org/x/text/language" + yaml "gopkg.in/yaml.v2" - "github.com/gohugoio/hugo/deps" + "github.com/BurntSushi/toml" "github.com/gohugoio/hugo/helpers" + "github.com/nicksnyder/go-i18n/v2/i18n" + + "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/source" - "github.com/nicksnyder/go-i18n/i18n/bundle" - "github.com/nicksnyder/go-i18n/i18n/language" _errors "github.com/pkg/errors" ) @@ -42,13 +45,10 @@ func NewTranslationProvider() *TranslationProvider { func (tp *TranslationProvider) Update(d *deps.Deps) error { spec := source.NewSourceSpec(d.PathSpec, nil) - i18nBundle := bundle.New() - - en := language.GetPluralSpec("en") - if en == nil { - return errors.New("the English language has vanished like an old oak table") - } - var newLangs []string + bundle := i18n.NewBundle(language.English) + bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) + bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal) + bundle.RegisterUnmarshalFunc("json", json.Unmarshal) // The source dirs are ordered so the most important comes first. Since this is a // last key win situation, we have to reverse the iteration order. @@ -56,33 +56,18 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error { for i := len(dirs) - 1; i >= 0; i-- { dir := dirs[i] src := spec.NewFilesystemFromFileMetaInfo(dir) - files, err := src.Files() if err != nil { return err } - - for _, r := range files { - currentSpec := language.GetPluralSpec(r.BaseFileName()) - if currentSpec == nil { - // This may is a language code not supported by go-i18n, it may be - // Klingon or ... not even a fake language. Make sure it works. - newLangs = append(newLangs, r.BaseFileName()) - } - } - - if len(newLangs) > 0 { - language.RegisterPluralSpec(newLangs, en) - } - for _, file := range files { - if err := addTranslationFile(i18nBundle, file); err != nil { + if err := addTranslationFile(bundle, file); err != nil { return err } } } - tp.t = NewTranslator(i18nBundle, d.Cfg, d.Log) + tp.t = NewTranslator(bundle, d.Cfg, d.Log) d.Translate = tp.t.Func(d.Language.Lang) @@ -90,16 +75,29 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error { } -func addTranslationFile(bundle *bundle.Bundle, r source.File) error { +const artificialLangTagPrefix = "art-x-" + +func addTranslationFile(bundle *i18n.Bundle, r source.File) error { f, err := r.FileInfo().Meta().Open() if err != nil { return _errors.Wrapf(err, "failed to open translations file %q:", r.LogicalName()) } - err = bundle.ParseTranslationFileBytes(r.LogicalName(), helpers.ReaderToBytes(f)) + + b := helpers.ReaderToBytes(f) f.Close() + + name := r.LogicalName() + lang := helpers.Filename(name) + tag := language.Make(lang) + if tag == language.Und { + name = artificialLangTagPrefix + name + } + + _, err = bundle.ParseMessageFileBytes(b, name) if err != nil { return errWithFileContext(_errors.Wrapf(err, "failed to load translations"), r) } + return nil } |