diff --git a/context.go b/context.go index 71f9d2b..921ee7b 100644 --- a/context.go +++ b/context.go @@ -2,8 +2,6 @@ package language import ( "context" - - "golang.org/x/text/language" ) type contextKey string @@ -11,13 +9,13 @@ type contextKey string const languageKey contextKey = "language" // FromContext returns language tag. If language tag does not exist it returns language.Und value. -func FromContext(ctx context.Context) language.Tag { - tag, _ := ctx.Value(languageKey).(language.Tag) +func FromContext(ctx context.Context) Tag { + tag, _ := ctx.Value(languageKey).(Tag) return tag } // WithContext adds language tag to context. -func WithContext(ctx context.Context, tag language.Tag) context.Context { +func WithContext(ctx context.Context, tag Tag) context.Context { return context.WithValue(ctx, languageKey, tag) } diff --git a/middleware.go b/middleware.go index f14cfda..e71cdd0 100644 --- a/middleware.go +++ b/middleware.go @@ -35,7 +35,7 @@ func ReadFromAcceptHeader() MiddlewareOption { } // SupportedLanguages is used to set up list of supported languages. See language.NewMatcher() for details. -func SupportedLanguages(tags ...language.Tag) MiddlewareOption { +func SupportedLanguages(tags ...Tag) MiddlewareOption { return func(middleware *Middleware) { middleware.matcher = language.NewMatcher(tags) } @@ -57,7 +57,7 @@ func NewMiddleware(next http.Handler, options ...MiddlewareOption) *Middleware { setOption(middleware) } if middleware.matcher == nil { - middleware.matcher = language.NewMatcher([]language.Tag{language.English}) + middleware.matcher = language.NewMatcher([]Tag{English}) } if len(middleware.readers) == 0 { middleware.readers = append(middleware.readers, readFromAcceptLanguageHeader) diff --git a/middleware_test.go b/middleware_test.go index f43b212..e303180 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -7,7 +7,6 @@ import ( "testing" "github.com/muonsoft/language" - textlanguage "golang.org/x/text/language" ) func TestMiddleware_ServeHTTP(t *testing.T) { @@ -15,7 +14,7 @@ func TestMiddleware_ServeHTTP(t *testing.T) { name string request *http.Request options []language.MiddlewareOption - expectedLanguage textlanguage.Tag + expectedLanguage language.Tag }{ { name: "no options", diff --git a/tags.go b/tags.go index 7d85f07..a63c2c4 100644 --- a/tags.go +++ b/tags.go @@ -2,8 +2,11 @@ package language import "golang.org/x/text/language" +// Tag is an alias for language.Tag. +type Tag = language.Tag + // Equal compares language tags by base ISO 639 language code. -func Equal(tag1, tag2 language.Tag) bool { +func Equal(tag1, tag2 Tag) bool { base1, _, _ := tag1.Raw() base2, _, _ := tag2.Raw()