mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-11-16 14:54:23 +08:00
fix(i18n): use English runtime text and localize system UI
This commit is contained in:
@@ -6,6 +6,6 @@ package shared
|
||||
|
||||
// 通用业务错误消息常量
|
||||
const (
|
||||
BannedAccount = "账号已被封禁"
|
||||
UnAuthorized = "未登录"
|
||||
BannedAccount = "account is disabled"
|
||||
UnAuthorized = "Not signed in"
|
||||
)
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/pkg/i18n"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
@@ -31,10 +33,16 @@ func ErrorHandlerMiddleware() gin.HandlerFunc {
|
||||
|
||||
var apiErr *APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
c.JSON(apiErr.Code, Err(apiErr.Msg))
|
||||
c.JSON(apiErr.Code, Err(i18n.Translate(Locale(c), apiErr.Msg)))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusInternalServerError, Err("内部系统错误"))
|
||||
c.JSON(http.StatusInternalServerError, Err(i18n.Translate(Locale(c), "Internal system error")))
|
||||
}
|
||||
}
|
||||
|
||||
// Locale uses the same cookie as the frontend, with Accept-Language for API clients.
|
||||
func Locale(c *gin.Context) string {
|
||||
cookie, _ := c.Cookie("NEXT_LOCALE")
|
||||
return i18n.ResolveLocale(cookie, c.GetHeader("Accept-Language"))
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ func TestErrorHandlerMiddleware_FallbackForNonAPIError(t *testing.T) {
|
||||
|
||||
var body Response[any]
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
assert.Equal(t, "内部系统错误", body.ErrorMsg)
|
||||
assert.Equal(t, "Internal system error", body.ErrorMsg)
|
||||
assert.Nil(t, body.Data)
|
||||
}
|
||||
|
||||
@@ -150,19 +150,23 @@ func TestErrorHandlerMiddleware_RecordsSpanOnAPIError(t *testing.T) {
|
||||
r.Use(ErrorHandlerMiddleware())
|
||||
r.GET("/err", func(c *gin.Context) {
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
AbortBadRequest(c, "bad request")
|
||||
AbortBadRequest(c, "invalid target log database: custom-中文")
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/err", nil)
|
||||
req.AddCookie(&http.Cookie{Name: "NEXT_LOCALE", Value: "zh-CN"})
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
span.End()
|
||||
|
||||
require.Equal(t, http.StatusBadRequest, w.Code)
|
||||
var body Any
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
assert.Equal(t, "目标日志库不合法: custom-中文", body.ErrorMsg)
|
||||
|
||||
spans := sr.Ended()
|
||||
require.Len(t, spans, 1)
|
||||
assert.Equal(t, codes.Error, spans[0].Status().Code)
|
||||
assert.Equal(t, "bad request", spans[0].Status().Description)
|
||||
assert.Equal(t, "invalid target log database: custom-中文", spans[0].Status().Description)
|
||||
require.NotEmpty(t, spans[0].Events())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user