From f68ec8f3a14f17db962fd6229bc5e5bb7f4e865b Mon Sep 17 00:00:00 2001 From: Plynksiy Nikita <39155883+trofen@users.noreply.github.com> Date: Tue, 17 Jun 2025 04:05:10 +0300 Subject: [PATCH] feat(i18n): replace hardcoded messages with translated strings (#289) --- http/controller/admin/file.go | 2 +- http/controller/admin/oauth.go | 6 +++--- http/middleware/admin.go | 4 ++-- http/middleware/admin_privilege.go | 2 +- http/middleware/jwt.go | 10 +++++----- resources/i18n/en.toml | 5 +++++ resources/i18n/es.toml | 5 +++++ resources/i18n/fr.toml | 7 ++++++- resources/i18n/ko.toml | 5 +++++ resources/i18n/ru.toml | 7 ++++++- resources/i18n/zh_CN.toml | 5 +++++ resources/i18n/zh_TW.toml | 5 +++++ 12 files changed, 49 insertions(+), 14 deletions(-) diff --git a/http/controller/admin/file.go b/http/controller/admin/file.go index ca9f60f..aa83b33 100644 --- a/http/controller/admin/file.go +++ b/http/controller/admin/file.go @@ -38,7 +38,7 @@ func (f *File) Notify(c *gin.Context) { res := global.Oss.Verify(c.Request) if !res { - response.Fail(c, 101, "权限错误") + response.Fail(c, 101, response.TranslateMsg(c, "NoAccess")) return } fm := &FileBack{} diff --git a/http/controller/admin/oauth.go b/http/controller/admin/oauth.go index efdf9e6..5155219 100644 --- a/http/controller/admin/oauth.go +++ b/http/controller/admin/oauth.go @@ -68,16 +68,16 @@ func (o *Oauth) Confirm(c *gin.Context) { j := &adminReq.OauthConfirmForm{} err := c.ShouldBindJSON(j) if err != nil { - response.Fail(c, 101, "参数错误"+err.Error()) + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) return } if j.Code == "" { - response.Fail(c, 101, "参数错误: code 不存在") + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")) return } v := service.AllService.OauthService.GetOauthCache(j.Code) if v == nil { - response.Fail(c, 101, "授权已过期") + response.Fail(c, 101, response.TranslateMsg(c, "OauthExpired")) return } u := service.AllService.UserService.CurUser(c) diff --git a/http/middleware/admin.go b/http/middleware/admin.go index 612718a..a52e722 100644 --- a/http/middleware/admin.go +++ b/http/middleware/admin.go @@ -13,13 +13,13 @@ func BackendUserAuth() gin.HandlerFunc { //测试先关闭 token := c.GetHeader("api-token") if token == "" { - response.Fail(c, 403, "请先登录") + response.Fail(c, 403, response.TranslateMsg(c, "NeedLogin")) c.Abort() return } user, ut := service.AllService.UserService.InfoByAccessToken(token) if user.Id == 0 { - response.Fail(c, 403, "请先登录") + response.Fail(c, 403, response.TranslateMsg(c, "NeedLogin")) c.Abort() return } diff --git a/http/middleware/admin_privilege.go b/http/middleware/admin_privilege.go index f72be93..4f9e934 100644 --- a/http/middleware/admin_privilege.go +++ b/http/middleware/admin_privilege.go @@ -12,7 +12,7 @@ func AdminPrivilege() gin.HandlerFunc { u := service.AllService.UserService.CurUser(c) if !service.AllService.UserService.IsAdmin(u) { - response.Fail(c, 403, "无权限") + response.Fail(c, 403, response.TranslateMsg(c, "NoAccess")) c.Abort() return } diff --git a/http/middleware/jwt.go b/http/middleware/jwt.go index 288cc14..9583354 100644 --- a/http/middleware/jwt.go +++ b/http/middleware/jwt.go @@ -12,18 +12,18 @@ func JwtAuth() gin.HandlerFunc { //测试先关闭 token := c.GetHeader("api-token") if token == "" { - response.Fail(c, 403, "请先登录") + response.Fail(c, 403, response.TranslateMsg(c, "NeedLogin")) c.Abort() return } uid, err := global.Jwt.ParseToken(token) if err != nil { - response.Fail(c, 403, "请先登录") + response.Fail(c, 403, response.TranslateMsg(c, "NeedLogin")) c.Abort() return } if uid == 0 { - response.Fail(c, 403, "请先登录") + response.Fail(c, 403, response.TranslateMsg(c, "NeedLogin")) c.Abort() return } @@ -34,12 +34,12 @@ func JwtAuth() gin.HandlerFunc { // Username: "测试用户", //} if user.Id == 0 { - response.Fail(c, 403, "请先登录") + response.Fail(c, 403, response.TranslateMsg(c, "NeedLogin")) c.Abort() return } if !service.AllService.UserService.CheckUserEnable(user) { - response.Fail(c, 101, "你已被禁用") + response.Fail(c, 101, response.TranslateMsg(c, "Banned")) c.Abort() return } diff --git a/resources/i18n/en.toml b/resources/i18n/en.toml index 2c64f9f..3933810 100644 --- a/resources/i18n/en.toml +++ b/resources/i18n/en.toml @@ -33,6 +33,11 @@ description = "No access." one = "No access." other = "No access." +[NeedLogin] +description = "Need login." +one = "Please log in first." +other = "Please log in first." + [UsernameOrPasswordError] description = "Username or password error." one = "Username or password error." diff --git a/resources/i18n/es.toml b/resources/i18n/es.toml index efbba84..ff517e6 100644 --- a/resources/i18n/es.toml +++ b/resources/i18n/es.toml @@ -33,6 +33,11 @@ description = "No access." one = "Sin acceso." other = "Sin acceso." +[NeedLogin] +description = "Need login." +one = "Por favor inicie sesión primero." +other = "Por favor inicie sesión primero." + [UsernameOrPasswordError] description = "Username or password error." one = "Error de usuario o contraseña." diff --git a/resources/i18n/fr.toml b/resources/i18n/fr.toml index 78906f4..52cd150 100644 --- a/resources/i18n/fr.toml +++ b/resources/i18n/fr.toml @@ -33,6 +33,11 @@ description = "No access." one = "Aucun d'access." other = "Aucun d'access." +[NeedLogin] +description = "Need login." +one = "Veuillez d'abord vous connecter." +other = "Veuillez d'abord vous connecter." + [UsernameOrPasswordError] description = "Username or password error." one = "Nom d'utilisateur ou de mot de passe incorrect." @@ -161,4 +166,4 @@ other = "Banni." [RegisterSuccessWaitAdminConfirm] description = "Register success wait admin confirm." one = "Inscription réussie, veuillez attendre la confirmation de l'administrateur." -other = "Inscription réussie, veuillez attendre la confirmation de l'administrateur." \ No newline at end of file +other = "Inscription réussie, veuillez attendre la confirmation de l'administrateur." diff --git a/resources/i18n/ko.toml b/resources/i18n/ko.toml index 16d2bed..6adcc2e 100644 --- a/resources/i18n/ko.toml +++ b/resources/i18n/ko.toml @@ -33,6 +33,11 @@ description = "No access." one = "접근할 수 없습니다." other = "접근할 수 없습니다." +[NeedLogin] +description = "Need login." +one = "먼저 로그인해주세요." +other = "먼저 로그인해주세요." + [UsernameOrPasswordError] description = "Username or password error." one = "사용자 이름이나 비밀번호가 올바르지 않습니다." diff --git a/resources/i18n/ru.toml b/resources/i18n/ru.toml index c7da252..9278a6e 100644 --- a/resources/i18n/ru.toml +++ b/resources/i18n/ru.toml @@ -33,6 +33,11 @@ description = "No access." one = "Нет доступа." other = "Нет доступа." +[NeedLogin] +description = "Need login." +one = "Пожалуйста, войдите в систему." +other = "Пожалуйста, войдите в систему." + [UsernameOrPasswordError] description = "Username or password error." one = "Неправильное имя пользователя или пароль." @@ -161,4 +166,4 @@ other = "Заблокировано." [RegisterSuccessWaitAdminConfirm] description = "Register success wait admin confirm." one = "Регистрация прошла успешно, ожидайте подтверждения администратора." -other = "Регистрация прошла успешно, ожидайте подтверждения администратора." \ No newline at end of file +other = "Регистрация прошла успешно, ожидайте подтверждения администратора." diff --git a/resources/i18n/zh_CN.toml b/resources/i18n/zh_CN.toml index bd86383..c2f60e0 100644 --- a/resources/i18n/zh_CN.toml +++ b/resources/i18n/zh_CN.toml @@ -33,6 +33,11 @@ description = "No access." one = "无权限。" other = "无权限。" +[NeedLogin] +description = "Need login." +one = "请先登录。" +other = "请先登录。" + [UsernameOrPasswordError] description = "Username or password error." one = "用户名或密码错误。" diff --git a/resources/i18n/zh_TW.toml b/resources/i18n/zh_TW.toml index 8821412..f0dac3a 100644 --- a/resources/i18n/zh_TW.toml +++ b/resources/i18n/zh_TW.toml @@ -33,6 +33,11 @@ description = "No access." one = "無許可權。" other = "無許可權。" +[NeedLogin] +description = "Need login." +one = "請先登入。" +other = "請先登入。" + [UsernameOrPasswordError] description = "Username or password error." one = "使用者名稱或密碼錯誤。"