diff options
Diffstat (limited to 'langs/i18n/translationProvider.go')
-rw-r--r-- | langs/i18n/translationProvider.go | 58 |
1 files changed, 28 insertions, 30 deletions
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 } |