fix(i18n): use English runtime text and localize system UI

This commit is contained in:
Arthur Koba
2026-10-25 19:44:15 +03:00
parent ada0550571
commit 65a96670d5
289 changed files with 8092 additions and 3426 deletions
+10 -2
View File
@@ -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"))
}
+7 -3
View File
@@ -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())
}