Skip to content

Commit

Permalink
feat: refactor backend i18n (casdoor#1373)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
forestmgy authored Dec 7, 2022
1 parent 96566a6 commit 1bb3d2d
Show file tree
Hide file tree
Showing 49 changed files with 1,604 additions and 1,301 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ jobs:
crowdin_branch_name: l10n_branch
config: './web/crowdin.yml'

- name: crowdin backend action
uses: crowdin/[email protected]
with:
upload_translations: true

download_translations: true
push_translations: true
commit_message: 'refactor: New Crowdin Backend translations by Github Action'

localization_branch_name: l10n_crowdin_action
create_pull_request: true
pull_request_title: 'refactor: New Crowdin Backend translations'

crowdin_branch_name: l10n_branch
config: './crowdin.yml'

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CROWDIN_PROJECT_ID: '463556'
Expand Down
12 changes: 6 additions & 6 deletions controllers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type Captcha struct {
// @router /signup [post]
func (c *ApiController) Signup() {
if c.GetSessionUsername() != "" {
c.ResponseError(c.T("SignUpErr.SignOutFirst"), c.GetSessionUsername())
c.ResponseError(c.T("account:Please sign out first before signing up"), c.GetSessionUsername())
return
}

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

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

Expand All @@ -129,7 +129,7 @@ func (c *ApiController) Signup() {
if application.IsSignupItemVisible("Email") && application.GetSignupItemRule("Email") != "No verification" && form.Email != "" {
checkResult := object.CheckVerificationCode(form.Email, form.EmailCode, c.GetAcceptLanguage())
if len(checkResult) != 0 {
c.ResponseError(c.T("EmailErr.EmailCheckResult"), checkResult)
c.ResponseError(c.T("account:Email: %s"), checkResult)
return
}
}
Expand All @@ -139,7 +139,7 @@ func (c *ApiController) Signup() {
checkPhone = fmt.Sprintf("+%s%s", form.PhonePrefix, form.Phone)
checkResult := object.CheckVerificationCode(checkPhone, form.PhoneCode, c.GetAcceptLanguage())
if len(checkResult) != 0 {
c.ResponseError(c.T("PhoneErr.PhoneCheckResult"), checkResult)
c.ResponseError(c.T("account:Phone: %s"), checkResult)
return
}
}
Expand All @@ -163,7 +163,7 @@ func (c *ApiController) Signup() {

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

Expand Down Expand Up @@ -209,7 +209,7 @@ func (c *ApiController) Signup() {

affected := object.AddUser(user)
if !affected {
c.ResponseError(c.T("UserErr.InvalidInformation"), util.StructToJson(user))
c.ResponseError(c.T("account:Invalid information"), util.StructToJson(user))
return
}

Expand Down
4 changes: 2 additions & 2 deletions controllers/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *ApiController) GetUserApplication() {
id := c.Input().Get("id")
user := object.GetUser(id)
if user == nil {
c.ResponseError(fmt.Sprintf(c.T("UserErr.DoNotExist"), id))
c.ResponseError(fmt.Sprintf(c.T("application:The user: %s doesn't exist"), id))
return
}

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

if organization == "" {
c.ResponseError(c.T("ParameterErr.OrgMissingErr"))
c.ResponseError(c.T("application:Parameter organization is missing"))
return
}

Expand Down
46 changes: 23 additions & 23 deletions controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *ApiController) HandleLoggedIn(application *object.Application, user *ob
return
}
if !allowed {
c.ResponseError(c.T("AuthErr.Unauthorized"))
c.ResponseError(c.T("auth:Unauthorized operation"))
return
}

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

if challengeMethod != "S256" && challengeMethod != "null" && challengeMethod != "" {
c.ResponseError(c.T("AuthErr.ChallengeMethodErr"))
c.ResponseError(c.T("auth:Challenge method should be S256"))
return
}
code := object.GetOAuthCode(userId, clientId, responseType, redirectUri, scope, state, nonce, codeChallenge, c.Ctx.Request.Host, c.GetAcceptLanguage())
Expand Down Expand Up @@ -205,7 +205,7 @@ func (c *ApiController) Login() {
if form.Username != "" {
if form.Type == ResponseTypeLogin {
if c.GetSessionUsername() != "" {
c.ResponseError(c.T("LoginErr.SignOutFirst"), c.GetSessionUsername())
c.ResponseError(c.T("auth:Please sign out first before signing in"), c.GetSessionUsername())
return
}
}
Expand All @@ -231,7 +231,7 @@ func (c *ApiController) Login() {
} else {
verificationCodeType = "phone"
if len(form.PhonePrefix) == 0 {
responseText := fmt.Sprintf(c.T("PhoneErr.NoPrefix"), verificationCodeType)
responseText := fmt.Sprintf(c.T("auth:%s No phone prefix"), verificationCodeType)
c.ResponseError(responseText)
return
}
Expand All @@ -256,13 +256,13 @@ func (c *ApiController) Login() {

user = object.GetUserByFields(form.Organization, form.Username)
if user == nil {
c.ResponseError(fmt.Sprintf(c.T("LoginErr.UserDoNotExist"), form.Organization, form.Username))
c.ResponseError(fmt.Sprintf(c.T("auth:The user: %s/%s doesn't exist"), form.Organization, form.Username))
return
}
} else {
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
if application == nil {
c.ResponseError(fmt.Sprintf("The application: %s does not exist", form.Application))
c.ResponseError(fmt.Sprintf(c.T("auth:The application: %s does not exist"), form.Application))
return
}

Expand All @@ -274,7 +274,7 @@ func (c *ApiController) Login() {
}

if !isHuman {
c.ResponseError("Turing test failed.")
c.ResponseError(c.T("auth:Turing test failed."))
return
}
}
Expand All @@ -288,7 +288,7 @@ func (c *ApiController) Login() {
} else {
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
if application == nil {
c.ResponseError(fmt.Sprintf(c.T("LoginErr.AppDoNotExist"), form.Application))
c.ResponseError(fmt.Sprintf(c.T("auth:The application: %s does not exist"), form.Application))
return
}

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

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

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

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

setHttpClient(idProvider, provider.Type)

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

Expand All @@ -353,13 +353,13 @@ func (c *ApiController) Login() {
}

if !token.Valid() {
c.ResponseError(c.T("TokenErr.InvalidToken"))
c.ResponseError(c.T("auth:Invalid token"))
return
}

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

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

resp = c.HandleLoggedIn(application, user, &form)
Expand All @@ -388,12 +388,12 @@ func (c *ApiController) Login() {
} else if provider.Category == "OAuth" {
// Sign up via OAuth
if !application.EnableSignUp {
c.ResponseError(fmt.Sprintf(c.T("LoginErr.AppNotEnableSignUp"), provider.Type, userInfo.Username, userInfo.DisplayName))
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))
return
}

if !providerItem.CanSignUp {
c.ResponseError(fmt.Sprintf(c.T("LoginErr.ProviderCanNotSignUp"), provider.Type, userInfo.Username, userInfo.DisplayName, provider.Type))
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))
return
}

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

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

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

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

oldUser := object.GetUserByField(application.Organization, provider.Type, userInfo.Id)
if oldUser != nil {
c.ResponseError(fmt.Sprintf(c.T("LoginErr.OldUser"), provider.Type, userInfo.Username, userInfo.DisplayName, oldUser.Name, oldUser.DisplayName))
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))
return
}

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

Expand All @@ -505,7 +505,7 @@ func (c *ApiController) Login() {
record.User = user.Name
util.SafeGoroutine(func() { object.AddRecord(record) })
} else {
c.ResponseError(fmt.Sprintf(c.T("LoginErr.UnknownAuthentication"), util.StructToJson(form)))
c.ResponseError(fmt.Sprintf(c.T("auth:Unknown authentication type (not password or provider), form = %s"), util.StructToJson(form)))
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/cas.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (c *RootController) SamlValidate() {
}

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

Expand Down
6 changes: 3 additions & 3 deletions controllers/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *ApiController) BatchEnforce() {
func (c *ApiController) GetAllObjects() {
userId := c.GetSessionUsername()
if userId == "" {
c.ResponseError(c.T("EnforcerErr.SignInFirst"))
c.ResponseError(c.T("enforcer:Please sign in first"))
return
}

Expand All @@ -58,7 +58,7 @@ func (c *ApiController) GetAllObjects() {
func (c *ApiController) GetAllActions() {
userId := c.GetSessionUsername()
if userId == "" {
c.ResponseError(c.T("EnforcerErr.SignInFirst"))
c.ResponseError(c.T("enforcer:Please sign in first"))
return
}

Expand All @@ -69,7 +69,7 @@ func (c *ApiController) GetAllActions() {
func (c *ApiController) GetAllRoles() {
userId := c.GetSessionUsername()
if userId == "" {
c.ResponseError(c.T("EnforcerErr.SignInFirst"))
c.ResponseError(c.T("enforcer:Please sign in first"))
return
}

Expand Down
12 changes: 6 additions & 6 deletions controllers/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *ApiController) GetLdapUser() {
ldapServer := LdapServer{}
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ldapServer)
if err != nil || util.IsStrsEmpty(ldapServer.Host, ldapServer.Admin, ldapServer.Passwd, ldapServer.BaseDn) {
c.ResponseError(c.T("ParameterErr.Missing"))
c.ResponseError(c.T("ldap:Missing parameter"))
return
}

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

if util.IsStrsEmpty(id) {
c.ResponseError(c.T("ParameterErr.Missing"))
c.ResponseError(c.T("ldap:Missing parameter"))
return
}

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

if util.IsStrsEmpty(ldap.Owner, ldap.ServerName, ldap.Host, ldap.Admin, ldap.Passwd, ldap.BaseDn) {
c.ResponseError(c.T("ParameterErr.Missing"))
c.ResponseError(c.T("ldap:Missing parameter"))
return
}

if object.CheckLdapExist(&ldap) {
c.ResponseError(c.T("LdapErr.ServerExisted"))
c.ResponseError(c.T("ldap:Ldap server exist"))
return
}

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

Expand Down
Loading

0 comments on commit 1bb3d2d

Please sign in to comment.