Skip to content

Commit 1bb3d2d

Browse files
authored
feat: refactor backend i18n (casdoor#1373)
* fix: handle the dataSourceName when DB changes * reduce duplication of code * feat: refactor translation error message * feat: use json intsead of ini file * remove useless translation * fix translate problems * remove useless addition * fix pr problems * fix pr problems * fix split problem * use gofumpt to fmt code * use crowdin to execute backend translation * fix pr problems * refactor: change translation file structure same as frontend * delete useless output * update go.mod
1 parent 96566a6 commit 1bb3d2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1604
-1301
lines changed

.github/workflows/sync.yml

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ jobs:
2929
crowdin_branch_name: l10n_branch
3030
config: './web/crowdin.yml'
3131

32+
- name: crowdin backend action
33+
uses: crowdin/[email protected]
34+
with:
35+
upload_translations: true
36+
37+
download_translations: true
38+
push_translations: true
39+
commit_message: 'refactor: New Crowdin Backend translations by Github Action'
40+
41+
localization_branch_name: l10n_crowdin_action
42+
create_pull_request: true
43+
pull_request_title: 'refactor: New Crowdin Backend translations'
44+
45+
crowdin_branch_name: l10n_branch
46+
config: './crowdin.yml'
47+
3248
env:
3349
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3450
CROWDIN_PROJECT_ID: '463556'

controllers/account.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type Captcha struct {
102102
// @router /signup [post]
103103
func (c *ApiController) Signup() {
104104
if c.GetSessionUsername() != "" {
105-
c.ResponseError(c.T("SignUpErr.SignOutFirst"), c.GetSessionUsername())
105+
c.ResponseError(c.T("account:Please sign out first before signing up"), c.GetSessionUsername())
106106
return
107107
}
108108

@@ -115,7 +115,7 @@ func (c *ApiController) Signup() {
115115

116116
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
117117
if !application.EnableSignUp {
118-
c.ResponseError(c.T("SignUpErr.DoNotAllowSignUp"))
118+
c.ResponseError(c.T("account:The application does not allow to sign up new account"))
119119
return
120120
}
121121

@@ -129,7 +129,7 @@ func (c *ApiController) Signup() {
129129
if application.IsSignupItemVisible("Email") && application.GetSignupItemRule("Email") != "No verification" && form.Email != "" {
130130
checkResult := object.CheckVerificationCode(form.Email, form.EmailCode, c.GetAcceptLanguage())
131131
if len(checkResult) != 0 {
132-
c.ResponseError(c.T("EmailErr.EmailCheckResult"), checkResult)
132+
c.ResponseError(c.T("account:Email: %s"), checkResult)
133133
return
134134
}
135135
}
@@ -139,7 +139,7 @@ func (c *ApiController) Signup() {
139139
checkPhone = fmt.Sprintf("+%s%s", form.PhonePrefix, form.Phone)
140140
checkResult := object.CheckVerificationCode(checkPhone, form.PhoneCode, c.GetAcceptLanguage())
141141
if len(checkResult) != 0 {
142-
c.ResponseError(c.T("PhoneErr.PhoneCheckResult"), checkResult)
142+
c.ResponseError(c.T("account:Phone: %s"), checkResult)
143143
return
144144
}
145145
}
@@ -163,7 +163,7 @@ func (c *ApiController) Signup() {
163163

164164
initScore, err := getInitScore()
165165
if err != nil {
166-
c.ResponseError(fmt.Errorf(c.T("InitErr.InitScoreFailed"), err).Error())
166+
c.ResponseError(fmt.Errorf(c.T("account:Get init score failed, error: %w"), err).Error())
167167
return
168168
}
169169

@@ -209,7 +209,7 @@ func (c *ApiController) Signup() {
209209

210210
affected := object.AddUser(user)
211211
if !affected {
212-
c.ResponseError(c.T("UserErr.InvalidInformation"), util.StructToJson(user))
212+
c.ResponseError(c.T("account:Invalid information"), util.StructToJson(user))
213213
return
214214
}
215215

controllers/application.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (c *ApiController) GetUserApplication() {
8686
id := c.Input().Get("id")
8787
user := object.GetUser(id)
8888
if user == nil {
89-
c.ResponseError(fmt.Sprintf(c.T("UserErr.DoNotExist"), id))
89+
c.ResponseError(fmt.Sprintf(c.T("application:The user: %s doesn't exist"), id))
9090
return
9191
}
9292

@@ -113,7 +113,7 @@ func (c *ApiController) GetOrganizationApplications() {
113113
sortOrder := c.Input().Get("sortOrder")
114114

115115
if organization == "" {
116-
c.ResponseError(c.T("ParameterErr.OrgMissingErr"))
116+
c.ResponseError(c.T("application:Parameter organization is missing"))
117117
return
118118
}
119119

controllers/auth.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (c *ApiController) HandleLoggedIn(application *object.Application, user *ob
6565
return
6666
}
6767
if !allowed {
68-
c.ResponseError(c.T("AuthErr.Unauthorized"))
68+
c.ResponseError(c.T("auth:Unauthorized operation"))
6969
return
7070
}
7171

@@ -84,7 +84,7 @@ func (c *ApiController) HandleLoggedIn(application *object.Application, user *ob
8484
codeChallenge := c.Input().Get("code_challenge")
8585

8686
if challengeMethod != "S256" && challengeMethod != "null" && challengeMethod != "" {
87-
c.ResponseError(c.T("AuthErr.ChallengeMethodErr"))
87+
c.ResponseError(c.T("auth:Challenge method should be S256"))
8888
return
8989
}
9090
code := object.GetOAuthCode(userId, clientId, responseType, redirectUri, scope, state, nonce, codeChallenge, c.Ctx.Request.Host, c.GetAcceptLanguage())
@@ -205,7 +205,7 @@ func (c *ApiController) Login() {
205205
if form.Username != "" {
206206
if form.Type == ResponseTypeLogin {
207207
if c.GetSessionUsername() != "" {
208-
c.ResponseError(c.T("LoginErr.SignOutFirst"), c.GetSessionUsername())
208+
c.ResponseError(c.T("auth:Please sign out first before signing in"), c.GetSessionUsername())
209209
return
210210
}
211211
}
@@ -231,7 +231,7 @@ func (c *ApiController) Login() {
231231
} else {
232232
verificationCodeType = "phone"
233233
if len(form.PhonePrefix) == 0 {
234-
responseText := fmt.Sprintf(c.T("PhoneErr.NoPrefix"), verificationCodeType)
234+
responseText := fmt.Sprintf(c.T("auth:%s No phone prefix"), verificationCodeType)
235235
c.ResponseError(responseText)
236236
return
237237
}
@@ -256,13 +256,13 @@ func (c *ApiController) Login() {
256256

257257
user = object.GetUserByFields(form.Organization, form.Username)
258258
if user == nil {
259-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.UserDoNotExist"), form.Organization, form.Username))
259+
c.ResponseError(fmt.Sprintf(c.T("auth:The user: %s/%s doesn't exist"), form.Organization, form.Username))
260260
return
261261
}
262262
} else {
263263
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
264264
if application == nil {
265-
c.ResponseError(fmt.Sprintf("The application: %s does not exist", form.Application))
265+
c.ResponseError(fmt.Sprintf(c.T("auth:The application: %s does not exist"), form.Application))
266266
return
267267
}
268268

@@ -274,7 +274,7 @@ func (c *ApiController) Login() {
274274
}
275275

276276
if !isHuman {
277-
c.ResponseError("Turing test failed.")
277+
c.ResponseError(c.T("auth:Turing test failed."))
278278
return
279279
}
280280
}
@@ -288,7 +288,7 @@ func (c *ApiController) Login() {
288288
} else {
289289
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
290290
if application == nil {
291-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.AppDoNotExist"), form.Application))
291+
c.ResponseError(fmt.Sprintf(c.T("auth:The application: %s does not exist"), form.Application))
292292
return
293293
}
294294

@@ -302,15 +302,15 @@ func (c *ApiController) Login() {
302302
} else if form.Provider != "" {
303303
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
304304
if application == nil {
305-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.AppDoNotExist"), form.Application))
305+
c.ResponseError(fmt.Sprintf(c.T("auth:The application: %s does not exist"), form.Application))
306306
return
307307
}
308308

309309
organization := object.GetOrganization(fmt.Sprintf("%s/%s", "admin", application.Organization))
310310
provider := object.GetProvider(util.GetId("admin", form.Provider))
311311
providerItem := application.GetProviderItem(provider.Name)
312312
if !providerItem.IsProviderVisible() {
313-
c.ResponseError(fmt.Sprintf(c.T("ProviderErr.ProviderNotEnabled"), provider.Name))
313+
c.ResponseError(fmt.Sprintf(c.T("auth:The provider: %s is not enabled for the application"), provider.Name))
314314
return
315315
}
316316

@@ -334,14 +334,14 @@ func (c *ApiController) Login() {
334334

335335
idProvider := idp.GetIdProvider(provider.Type, provider.SubType, clientId, clientSecret, provider.AppId, form.RedirectUri, provider.Domain, provider.CustomAuthUrl, provider.CustomTokenUrl, provider.CustomUserInfoUrl)
336336
if idProvider == nil {
337-
c.ResponseError(fmt.Sprintf(c.T("ProviderErr.ProviderNotSupported"), provider.Type))
337+
c.ResponseError(fmt.Sprintf(c.T("auth:The provider type: %s is not supported"), provider.Type))
338338
return
339339
}
340340

341341
setHttpClient(idProvider, provider.Type)
342342

343343
if form.State != conf.GetConfigString("authState") && form.State != application.Name {
344-
c.ResponseError(fmt.Sprintf(c.T("AuthErr.AuthStateWrong"), conf.GetConfigString("authState"), form.State))
344+
c.ResponseError(fmt.Sprintf(c.T("auth:State expected: %s, but got: %s"), conf.GetConfigString("authState"), form.State))
345345
return
346346
}
347347

@@ -353,13 +353,13 @@ func (c *ApiController) Login() {
353353
}
354354

355355
if !token.Valid() {
356-
c.ResponseError(c.T("TokenErr.InvalidToken"))
356+
c.ResponseError(c.T("auth:Invalid token"))
357357
return
358358
}
359359

360360
userInfo, err = idProvider.GetUserInfo(token)
361361
if err != nil {
362-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.LoginFail"), err.Error()))
362+
c.ResponseError(fmt.Sprintf(c.T("auth:Failed to login in: %s"), err.Error()))
363363
return
364364
}
365365
}
@@ -376,7 +376,7 @@ func (c *ApiController) Login() {
376376
// Sign in via OAuth (want to sign up but already have account)
377377

378378
if user.IsForbidden {
379-
c.ResponseError(c.T("LoginErr.UserIsForbidden"))
379+
c.ResponseError(c.T("auth:The user is forbidden to sign in, please contact the administrator"))
380380
}
381381

382382
resp = c.HandleLoggedIn(application, user, &form)
@@ -388,12 +388,12 @@ func (c *ApiController) Login() {
388388
} else if provider.Category == "OAuth" {
389389
// Sign up via OAuth
390390
if !application.EnableSignUp {
391-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.AppNotEnableSignUp"), provider.Type, userInfo.Username, userInfo.DisplayName))
391+
c.ResponseError(fmt.Sprintf(c.T("auth:The account for provider: %s and username: %s (%s) does not exist and is not allowed to sign up as new account, please contact your IT support"), provider.Type, userInfo.Username, userInfo.DisplayName))
392392
return
393393
}
394394

395395
if !providerItem.CanSignUp {
396-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.ProviderCanNotSignUp"), provider.Type, userInfo.Username, userInfo.DisplayName, provider.Type))
396+
c.ResponseError(fmt.Sprintf(c.T("auth:The account for provider: %s and username: %s (%s) does not exist and is not allowed to sign up as new account via %%s, please use another way to sign up"), provider.Type, userInfo.Username, userInfo.DisplayName, provider.Type))
397397
return
398398
}
399399

@@ -414,7 +414,7 @@ func (c *ApiController) Login() {
414414
properties["no"] = strconv.Itoa(len(object.GetUsers(application.Organization)) + 2)
415415
initScore, err := getInitScore()
416416
if err != nil {
417-
c.ResponseError(fmt.Errorf(c.T("InitErr.InitScoreFailed"), err).Error())
417+
c.ResponseError(fmt.Errorf(c.T("auth:Get init score failed, error: %w"), err).Error())
418418
return
419419
}
420420

@@ -441,7 +441,7 @@ func (c *ApiController) Login() {
441441

442442
affected := object.AddUser(user)
443443
if !affected {
444-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.InvalidUserInformation"), util.StructToJson(user)))
444+
c.ResponseError(fmt.Sprintf(c.T("auth:Failed to create user, user information is invalid: %s"), util.StructToJson(user)))
445445
return
446446
}
447447

@@ -466,13 +466,13 @@ func (c *ApiController) Login() {
466466
} else { // form.Method != "signup"
467467
userId := c.GetSessionUsername()
468468
if userId == "" {
469-
c.ResponseError(c.T("LoginErr.AccountDoNotExist"), userInfo)
469+
c.ResponseError(c.T("auth:The account does not exist"), userInfo)
470470
return
471471
}
472472

473473
oldUser := object.GetUserByField(application.Organization, provider.Type, userInfo.Id)
474474
if oldUser != nil {
475-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.OldUser"), provider.Type, userInfo.Username, userInfo.DisplayName, oldUser.Name, oldUser.DisplayName))
475+
c.ResponseError(fmt.Sprintf(c.T("auth:The account for provider: %s and username: %s (%s) is already linked to another account: %s (%s)"), provider.Type, userInfo.Username, userInfo.DisplayName, oldUser.Name, oldUser.DisplayName))
476476
return
477477
}
478478

@@ -493,7 +493,7 @@ func (c *ApiController) Login() {
493493
// user already signed in to Casdoor, so let the user click the avatar button to do the quick sign-in
494494
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
495495
if application == nil {
496-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.AppDoNotExist"), form.Application))
496+
c.ResponseError(fmt.Sprintf(c.T("auth:The application: %s does not exist"), form.Application))
497497
return
498498
}
499499

@@ -505,7 +505,7 @@ func (c *ApiController) Login() {
505505
record.User = user.Name
506506
util.SafeGoroutine(func() { object.AddRecord(record) })
507507
} else {
508-
c.ResponseError(fmt.Sprintf(c.T("LoginErr.UnknownAuthentication"), util.StructToJson(form)))
508+
c.ResponseError(fmt.Sprintf(c.T("auth:Unknown authentication type (not password or provider), form = %s"), util.StructToJson(form)))
509509
return
510510
}
511511
}

controllers/cas.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (c *RootController) SamlValidate() {
210210
}
211211

212212
if !strings.HasPrefix(target, service) {
213-
c.ResponseError(fmt.Sprintf(c.T("CasErr.ServiceDoNotMatch"), target, service))
213+
c.ResponseError(fmt.Sprintf(c.T("cas:Service %s and %s do not match"), target, service))
214214
return
215215
}
216216

controllers/enforcer.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *ApiController) BatchEnforce() {
4747
func (c *ApiController) GetAllObjects() {
4848
userId := c.GetSessionUsername()
4949
if userId == "" {
50-
c.ResponseError(c.T("EnforcerErr.SignInFirst"))
50+
c.ResponseError(c.T("enforcer:Please sign in first"))
5151
return
5252
}
5353

@@ -58,7 +58,7 @@ func (c *ApiController) GetAllObjects() {
5858
func (c *ApiController) GetAllActions() {
5959
userId := c.GetSessionUsername()
6060
if userId == "" {
61-
c.ResponseError(c.T("EnforcerErr.SignInFirst"))
61+
c.ResponseError(c.T("enforcer:Please sign in first"))
6262
return
6363
}
6464

@@ -69,7 +69,7 @@ func (c *ApiController) GetAllActions() {
6969
func (c *ApiController) GetAllRoles() {
7070
userId := c.GetSessionUsername()
7171
if userId == "" {
72-
c.ResponseError(c.T("EnforcerErr.SignInFirst"))
72+
c.ResponseError(c.T("enforcer:Please sign in first"))
7373
return
7474
}
7575

controllers/ldap.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (c *ApiController) GetLdapUser() {
5252
ldapServer := LdapServer{}
5353
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ldapServer)
5454
if err != nil || util.IsStrsEmpty(ldapServer.Host, ldapServer.Admin, ldapServer.Passwd, ldapServer.BaseDn) {
55-
c.ResponseError(c.T("ParameterErr.Missing"))
55+
c.ResponseError(c.T("ldap:Missing parameter"))
5656
return
5757
}
5858

@@ -120,7 +120,7 @@ func (c *ApiController) GetLdap() {
120120
id := c.Input().Get("id")
121121

122122
if util.IsStrsEmpty(id) {
123-
c.ResponseError(c.T("ParameterErr.Missing"))
123+
c.ResponseError(c.T("ldap:Missing parameter"))
124124
return
125125
}
126126

@@ -136,17 +136,17 @@ func (c *ApiController) AddLdap() {
136136
var ldap object.Ldap
137137
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ldap)
138138
if err != nil {
139-
c.ResponseError(c.T("ParameterErr.Missing"))
139+
c.ResponseError(c.T("ldap:Missing parameter"))
140140
return
141141
}
142142

143143
if util.IsStrsEmpty(ldap.Owner, ldap.ServerName, ldap.Host, ldap.Admin, ldap.Passwd, ldap.BaseDn) {
144-
c.ResponseError(c.T("ParameterErr.Missing"))
144+
c.ResponseError(c.T("ldap:Missing parameter"))
145145
return
146146
}
147147

148148
if object.CheckLdapExist(&ldap) {
149-
c.ResponseError(c.T("LdapErr.ServerExisted"))
149+
c.ResponseError(c.T("ldap:Ldap server exist"))
150150
return
151151
}
152152

@@ -171,7 +171,7 @@ func (c *ApiController) UpdateLdap() {
171171
var ldap object.Ldap
172172
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ldap)
173173
if err != nil || util.IsStrsEmpty(ldap.Owner, ldap.ServerName, ldap.Host, ldap.Admin, ldap.Passwd, ldap.BaseDn) {
174-
c.ResponseError(c.T("ParameterErr.Missing"))
174+
c.ResponseError(c.T("ldap:Missing parameter"))
175175
return
176176
}
177177

0 commit comments

Comments
 (0)