Skip to content

Commit

Permalink
Merge pull request #1 from kimmelserj/master
Browse files Browse the repository at this point in the history
Added Configurator support.
  • Loading branch information
snovichkov authored Apr 26, 2023
2 parents 2fbc811 + 7d3f881 commit 3833fdb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
14 changes: 14 additions & 0 deletions di.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ package ut
import "github.com/gozix/di"

const (
// TagConfigurator is tag to mark configurators.
TagConfigurator = "universal-translator.configurator"

// TagTranslator is a tag for marking locale translator without overriding existing.
TagTranslator = "universal-translator.locale-translator"

// TagTranslatorOverride is a tag for marking locale translator with overriding existing.
TagTranslatorOverride = "universal-translator.locale-translator.override"
)

// AsConfigurator is syntax sugar for the di container.
func AsConfigurator() di.ProvideOption {
return di.Tags{{
Name: TagConfigurator,
}}
}

func AsTranslator(override bool) di.Tags {
if override {
return di.Tags{{
Expand All @@ -26,6 +36,10 @@ func AsTranslator(override bool) di.Tags {
}}
}

func withConfigurator() di.Modifier {
return di.WithTags(TagConfigurator)
}

func withTranslator(override bool) di.Modifier {
if override {
return di.WithTags(TagTranslatorOverride)
Expand Down
24 changes: 18 additions & 6 deletions ut.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ import (
ut "github.com/go-playground/universal-translator"
)

// Bundle implements the glue.Bundle interface.
type Bundle struct {
fallback locales.Translator
locales []locales.Translator
}
type (
// Bundle implements the glue.Bundle interface.
Bundle struct {
fallback locales.Translator
locales []locales.Translator
}

// Configurator configures universal translator after its creation.
Configurator func(*ut.UniversalTranslator) error
)

// Bundle implements the glue.Bundle interface.
var _ glue.Bundle = (*Bundle)(nil)
Expand Down Expand Up @@ -51,10 +56,11 @@ func (b *Bundle) Build(builder di.Builder) error {
b.provideUT,
di.Constraint(0, di.Optional(true), withTranslator(false)),
di.Constraint(1, di.Optional(true), withTranslator(true)),
di.Constraint(2, di.Optional(true), withConfigurator()),
)
}

func (b *Bundle) provideUT(append []locales.Translator, override []locales.Translator) (_ *ut.UniversalTranslator, err error) {
func (b *Bundle) provideUT(append []locales.Translator, override []locales.Translator, configurators []Configurator) (_ *ut.UniversalTranslator, err error) {
var translator = ut.New(b.fallback, b.locales...)

for _, localeTranslator := range append {
Expand All @@ -69,6 +75,12 @@ func (b *Bundle) provideUT(append []locales.Translator, override []locales.Trans
}
}

for _, configure := range configurators {
if err = configure(translator); err != nil {
return nil, err
}
}

return translator, nil
}

Expand Down

0 comments on commit 3833fdb

Please sign in to comment.