forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n_helper_test.go
52 lines (45 loc) · 1.95 KB
/
i18n_helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package fosite
import (
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/text/language"
"github.com/ory/fosite/i18n"
)
func TestErrorTranslation(t *testing.T) {
catalog := i18n.NewDefaultMessageCatalog([]*i18n.DefaultLocaleBundle{
{
LangTag: "en",
Messages: []*i18n.DefaultMessage{
{
ID: "badRequestMethod",
FormattedMessage: "HTTP method is '%s', expected 'POST'.",
},
{
ID: "invalid_request",
FormattedMessage: "The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.",
},
},
},
{
LangTag: "es",
Messages: []*i18n.DefaultMessage{
{
ID: "badRequestMethod",
FormattedMessage: "El método HTTP es '%s', esperado 'POST'.",
},
{
ID: "invalid_request",
FormattedMessage: "A la solicitud le falta un parámetro obligatorio, incluye un valor de parámetro no válido, incluye un parámetro más de una vez o tiene un formato incorrecto.",
},
},
},
})
errWithNoCatalog := ErrInvalidRequest.WithHintIDOrDefaultf("badRequestMethod", "HTTP method is '%s', expected 'POST'.", "GET")
errWithCatalog := errWithNoCatalog.WithLocalizer(catalog, language.Spanish)
assert.Equal(t, "The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. HTTP method is 'GET', expected 'POST'.",
errWithNoCatalog.GetDescription(), "Message does not match when no catalog is specified")
assert.Equal(t, "A la solicitud le falta un parámetro obligatorio, incluye un valor de parámetro no válido, incluye un parámetro más de una vez o tiene un formato incorrecto. El método HTTP es 'GET', esperado 'POST'.",
errWithCatalog.GetDescription(), "Message does not match when catalog is specified")
}