Skip to content

Commit

Permalink
language package updated to v0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
strider2038 committed Jun 18, 2021
1 parent 6dc5576 commit 9b429f7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,12 @@ for _, violation := range violations {
The last way is to pass language via context. It is provided by the `github.com/muonsoft/language` package and can be useful in combination with [language middleware](https://github.com/muonsoft/language/blob/main/middleware.go). You can pass the context by using the `validation.Context()` argument or by creating a scoped validator with the `validator.WithContext()` method.

```golang
// import mslanguage "github.com/muonsoft/language"
// import "github.com/muonsoft/language"

validator, _ := validation.NewValidator(
validation.Translations(russian.Messages),
)
ctx := mslanguage.WithContext(context.Background(), language.Russian)
ctx := language.WithContext(context.Background(), language.Russian)
validator = validator.WithContext(ctx)

s := ""
Expand Down
7 changes: 3 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"regexp"
"time"

mslanguage "github.com/muonsoft/language"
"github.com/muonsoft/language"
"github.com/muonsoft/validation"
"github.com/muonsoft/validation/it"
"github.com/muonsoft/validation/message/translations/russian"
"github.com/muonsoft/validation/validator"
"golang.org/x/text/feature/plural"
"golang.org/x/text/language"
"golang.org/x/text/message/catalog"
)

Expand Down Expand Up @@ -543,7 +542,7 @@ func ExampleValidator_Validate_translationsByContextArgument() {
}

s := ""
ctx := mslanguage.WithContext(context.Background(), language.Russian)
ctx := language.WithContext(context.Background(), language.Russian)
err = validator.Validate(
validation.Context(ctx),
validation.String(&s, it.IsNotBlank()),
Expand All @@ -561,7 +560,7 @@ func ExampleValidator_Validate_translationsByContextValidator() {
if err != nil {
log.Fatal(err)
}
ctx := mslanguage.WithContext(context.Background(), language.Russian)
ctx := language.WithContext(context.Background(), language.Russian)
validator = validator.WithContext(ctx)

s := ""
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/muonsoft/language v0.2.0
github.com/muonsoft/language v0.3.0
github.com/stretchr/testify v1.6.1
golang.org/x/text v0.3.6
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ github.com/muonsoft/language v0.1.0 h1:rOrN6dKMjhDuw7oxdIz7f6Hf5U/faWkvcjJ5UtGeg
github.com/muonsoft/language v0.1.0/go.mod h1:be1X9wxDjKiqqKfXU6eiUHxu4BrreJZx1GXguR29QFo=
github.com/muonsoft/language v0.2.0 h1:YOWQ2T/prZx6z3t32xMUKibIrP2CQuHVGJ+xyZ2sPyc=
github.com/muonsoft/language v0.2.0/go.mod h1:be1X9wxDjKiqqKfXU6eiUHxu4BrreJZx1GXguR29QFo=
github.com/muonsoft/language v0.2.1-0.20210618104203-c0fcb03fa8b6 h1:MiktBECUqSFNr2iYFRUwd95y/zyOraB8Yy+FhgONOP0=
github.com/muonsoft/language v0.2.1-0.20210618104203-c0fcb03fa8b6/go.mod h1:be1X9wxDjKiqqKfXU6eiUHxu4BrreJZx1GXguR29QFo=
github.com/muonsoft/language v0.3.0 h1:KITWsOKhKavZUAA1v63NyG1IrPVYPeZkdltKfYXydI4=
github.com/muonsoft/language v0.3.0/go.mod h1:be1X9wxDjKiqqKfXU6eiUHxu4BrreJZx1GXguR29QFo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
5 changes: 2 additions & 3 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"fmt"

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

// Scope holds the current state of validation. On the client-side of the package,
Expand Down Expand Up @@ -34,7 +33,7 @@ func (s Scope) BuildViolation(code, message string) *ViolationBuilder {
if s.language != language.Und {
b.SetLanguage(s.language)
} else if s.context != nil {
b.SetLanguage(mslanguage.FromContext(s.context))
b.SetLanguage(language.FromContext(s.context))
}

return b
Expand Down
14 changes: 7 additions & 7 deletions test/translations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"strconv"
"testing"

mslanguage "github.com/muonsoft/language"
"github.com/muonsoft/language"
"github.com/muonsoft/validation"
"github.com/muonsoft/validation/code"
"github.com/muonsoft/validation/it"
"github.com/muonsoft/validation/message/translations/russian"
"github.com/muonsoft/validation/validationtest"
"github.com/muonsoft/validation/validator"
"github.com/stretchr/testify/assert"
"golang.org/x/text/language"
textlanguage "golang.org/x/text/language"
"golang.org/x/text/message/catalog"
)

Expand Down Expand Up @@ -103,7 +103,7 @@ func TestValidator_Validate_WhenDefaultLanguageIsNotLoaded_ExpectError(t *testin
func TestValidator_Validate_WhenTranslationLanguageInContextArgument_ExpectTranslationLanguageUsed(t *testing.T) {
v := newValidator(t, validation.Translations(russian.Messages))

ctx := mslanguage.WithContext(context.Background(), language.Russian)
ctx := language.WithContext(context.Background(), language.Russian)
err := v.Validate(
validation.Context(ctx),
validation.String(stringValue(""), it.IsNotBlank()),
Expand All @@ -129,7 +129,7 @@ func TestValidator_Validate_WhenTranslationLanguageInScopedValidator_ExpectTrans
}

func TestValidator_Validate_WhenTranslationLanguageInContextOfScopedValidator_ExpectTranslationLanguageUsed(t *testing.T) {
ctx := mslanguage.WithContext(context.Background(), language.Russian)
ctx := language.WithContext(context.Background(), language.Russian)
v := newValidator(t, validation.Translations(russian.Messages)).WithContext(ctx)

err := v.ValidateString(stringValue(""), it.IsNotBlank())
Expand All @@ -144,9 +144,9 @@ func TestValidator_Validate_WhenTranslationLanguageInContextOfScopedValidator_Ex
func TestValidator_Validate_WhenTranslationLanguageParsedFromAcceptLanguageHeader_ExpectTranslationLanguageUsed(t *testing.T) {
v := newValidator(t, validation.Translations(russian.Messages))

matcher := language.NewMatcher([]language.Tag{language.Russian})
tag, _ := language.MatchStrings(matcher, "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7")
ctx := mslanguage.WithContext(context.Background(), tag)
matcher := textlanguage.NewMatcher([]language.Tag{language.Russian})
tag, _ := textlanguage.MatchStrings(matcher, "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7")
ctx := language.WithContext(context.Background(), tag)
err := v.Validate(
validation.Context(ctx),
validation.String(stringValue(""), it.IsNotBlank()),
Expand Down

0 comments on commit 9b429f7

Please sign in to comment.