Skip to content

Commit

Permalink
Merge pull request #1 from muonsoft/tag-alias
Browse files Browse the repository at this point in the history
tag-alias
  • Loading branch information
strider2038 authored Jun 18, 2021
2 parents 95de5ba + 0486f6a commit 294d259
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
8 changes: 3 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ package language

import (
"context"

"golang.org/x/text/language"
)

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)
}
4 changes: 2 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"testing"

"github.com/muonsoft/language"
textlanguage "golang.org/x/text/language"
)

func TestMiddleware_ServeHTTP(t *testing.T) {
tests := []struct {
name string
request *http.Request
options []language.MiddlewareOption
expectedLanguage textlanguage.Tag
expectedLanguage language.Tag
}{
{
name: "no options",
Expand Down
5 changes: 4 additions & 1 deletion tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 294d259

Please sign in to comment.