diff --git a/server/internal/adapter/context.go b/server/internal/adapter/context.go index 5477bba05..cf0851cae 100644 --- a/server/internal/adapter/context.go +++ b/server/internal/adapter/context.go @@ -34,7 +34,7 @@ type AuthInfo struct { EmailVerified *bool } -func AttachLang(ctx context.Context, lang string) context.Context { +func AttachLang(ctx context.Context, lang language.Tag) context.Context { return context.WithValue(ctx, contextLang, lang) } @@ -74,11 +74,11 @@ func Lang(ctx context.Context, lang *language.Tag) string { } if v := ctx.Value(contextLang); v != nil { - if lang, ok := v.(string); ok { - if lang == "" { + if lang, ok := v.(language.Tag); ok { + if lang.IsRoot() { return defaultLang.String() } - return lang + return lang.String() } } diff --git a/server/internal/adapter/context_test.go b/server/internal/adapter/context_test.go new file mode 100644 index 000000000..241d5b67c --- /dev/null +++ b/server/internal/adapter/context_test.go @@ -0,0 +1,54 @@ +package adapter + +import ( + "context" + "testing" + + "github.com/reearth/reearthx/log" + "github.com/stretchr/testify/assert" + "golang.org/x/text/language" +) + +func TestLang(t *testing.T) { + + // Default language for testing + defaultLang := language.English // or set it to whatever your defaultLang is + + t.Run("Lang is provided and valid", func(t *testing.T) { + lang := language.Japanese + result := Lang(context.Background(), &lang) + assert.Equal(t, "ja", result) + }) + + t.Run("Lang is nil, context has valid lang", func(t *testing.T) { + lang := language.French + ctx := context.WithValue(context.Background(), contextLang, lang) + result := Lang(ctx, nil) + log.Infofc(ctx, "result: %s", result) + assert.Equal(t, "fr", result) + }) + + t.Run("Lang is nil, context lang is empty", func(t *testing.T) { + ctx := context.WithValue(context.Background(), contextLang, language.Make("")) + result := Lang(ctx, nil) + assert.Equal(t, defaultLang.String(), result) + }) + + t.Run("Lang is nil, context has no lang", func(t *testing.T) { + result := Lang(context.Background(), nil) + assert.Equal(t, defaultLang.String(), result) + }) + + t.Run("Lang is root, context has no lang", func(t *testing.T) { + rootLang := language.Und + result := Lang(context.Background(), &rootLang) + assert.Equal(t, defaultLang.String(), result) + }) + + t.Run("Lang is french, context has no lang", func(t *testing.T) { + lang := language.French + result := Lang(context.Background(), &lang) + assert.Equal(t, lang.String(), result) + }) + +} diff --git a/server/internal/app/graphql_test.go b/server/internal/app/graphql_test.go index 3f32eeb32..a4cb3211b 100644 --- a/server/internal/app/graphql_test.go +++ b/server/internal/app/graphql_test.go @@ -9,11 +9,12 @@ import ( "github.com/reearth/reearth/server/pkg/apperror" "github.com/stretchr/testify/assert" "github.com/vektah/gqlparser/v2/ast" + "golang.org/x/text/language" ) func TestCustomErrorPresenter(t *testing.T) { ctx := context.Background() - ctx = adapter.AttachLang(ctx, "en") + ctx = adapter.AttachLang(ctx, language.English) appErr := &apperror.AppError{ LocalesError: map[string]*apperror.LocalesError{ diff --git a/server/internal/app/lang.go b/server/internal/app/lang.go index 7d19063ef..1d04062b0 100644 --- a/server/internal/app/lang.go +++ b/server/internal/app/lang.go @@ -5,10 +5,11 @@ import ( "github.com/labstack/echo/v4" "github.com/reearth/reearth/server/internal/adapter" + "golang.org/x/text/language" ) // LanguageExtractor extracts the appropriate language from the request. -func LanguageExtractor(req *http.Request) string { +func LanguageExtractor(req *http.Request) language.Tag { // Extract browser language from header lang := req.Header.Get("lang") @@ -20,7 +21,12 @@ func LanguageExtractor(req *http.Request) string { lang = u.Lang().String() } - return lang + tag, err := language.Parse(lang) + if err != nil { + return language.English + } + + return tag } // AttachLanguageMiddleware attaches the detected language to the request context. diff --git a/server/internal/app/lang_test.go b/server/internal/app/lang_test.go index 322c45f16..fe4b0d8b6 100644 --- a/server/internal/app/lang_test.go +++ b/server/internal/app/lang_test.go @@ -21,25 +21,25 @@ func TestLanguageExtractor(t *testing.T) { name string headerLang string userLang language.Tag - expected string + expected language.Tag }{ { name: "User language overrides browser language", headerLang: "fr", userLang: language.English, - expected: "en", + expected: language.English, }, { name: "No user language, use browser language", headerLang: "fr", userLang: language.Und, - expected: "fr", + expected: language.French, }, { name: "No browser language or user language is und, fallback to default", headerLang: "", userLang: language.Und, - expected: "", + expected: language.English, }, }