From 5d3f99c7c6d0f2c304dc13c6fa6aa675daf310cc Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 26 Jun 2022 16:19:22 +0200 Subject: Make better use of i18n (#20096) * Prototyping * Start work on creating offsets * Modify tests * Start prototyping with actual MPH * Twiddle around * Twiddle around comments * Convert templates * Fix external languages * Fix latest translation * Fix some test * Tidy up code * Use simple map * go mod tidy * Move back to data structure - Uses less memory by creating for each language a map. * Apply suggestions from code review Co-authored-by: delvh * Add some comments * Fix tests * Try to fix tests * Use en-US as defacto fallback * Use correct slices * refactor (#4) * Remove TryTr, add log for missing translation key * Refactor i18n - Separate dev and production locale stores. - Allow for live-reloading in dev mode. Co-authored-by: zeripath * Fix live-reloading & check for errors * Make linter happy * live-reload with periodic check (#5) * Fix tests Co-authored-by: delvh Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: wxiaoguang Co-authored-by: zeripath --- integrations/auth_ldap_test.go | 5 ++--- integrations/branches_test.go | 6 +++--- integrations/pull_merge_test.go | 4 ++-- integrations/release_test.go | 12 ++++++------ integrations/repo_branch_test.go | 20 ++++++++++---------- integrations/signin_test.go | 10 +++++----- integrations/signup_test.go | 8 ++++---- integrations/user_test.go | 6 +++--- 8 files changed, 35 insertions(+), 36 deletions(-) (limited to 'integrations') diff --git a/integrations/auth_ldap_test.go b/integrations/auth_ldap_test.go index 296b647e6d..492a4fdadf 100644 --- a/integrations/auth_ldap_test.go +++ b/integrations/auth_ldap_test.go @@ -16,7 +16,7 @@ import ( "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/translation/i18n" + "code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/services/auth" "github.com/stretchr/testify/assert" @@ -275,8 +275,7 @@ func TestLDAPUserSigninFailed(t *testing.T) { addAuthSourceLDAP(t, "") u := otherLDAPUsers[0] - - testLoginFailed(t, u.UserName, u.Password, i18n.Tr("en", "form.username_password_incorrect")) + testLoginFailed(t, u.UserName, u.Password, translation.NewLocale("en-US").Tr("form.username_password_incorrect")) } func TestLDAPUserSSHKeySync(t *testing.T) { diff --git a/integrations/branches_test.go b/integrations/branches_test.go index 551c5f8af8..8b05e24a04 100644 --- a/integrations/branches_test.go +++ b/integrations/branches_test.go @@ -9,7 +9,7 @@ import ( "net/url" "testing" - "code.gitea.io/gitea/modules/translation/i18n" + "code.gitea.io/gitea/modules/translation" "github.com/stretchr/testify/assert" ) @@ -37,7 +37,7 @@ func TestUndoDeleteBranch(t *testing.T) { htmlDoc, name := branchAction(t, ".undo-button") assert.Contains(t, htmlDoc.doc.Find(".ui.positive.message").Text(), - i18n.Tr("en", "repo.branch.restore_success", name), + translation.NewLocale("en-US").Tr("repo.branch.restore_success", name), ) }) } @@ -46,7 +46,7 @@ func deleteBranch(t *testing.T) { htmlDoc, name := branchAction(t, ".delete-branch-button") assert.Contains(t, htmlDoc.doc.Find(".ui.positive.message").Text(), - i18n.Tr("en", "repo.branch.deletion_success", name), + translation.NewLocale("en-US").Tr("repo.branch.deletion_success", name), ) } diff --git a/integrations/pull_merge_test.go b/integrations/pull_merge_test.go index de519094d4..2a3a461efd 100644 --- a/integrations/pull_merge_test.go +++ b/integrations/pull_merge_test.go @@ -27,7 +27,7 @@ import ( "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" - "code.gitea.io/gitea/modules/translation/i18n" + "code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/services/pull" repo_service "code.gitea.io/gitea/services/repository" files_service "code.gitea.io/gitea/services/repository/files" @@ -204,7 +204,7 @@ func TestCantMergeWorkInProgress(t *testing.T) { text := strings.TrimSpace(htmlDoc.doc.Find(".merge-section > .item").Last().Text()) assert.NotEmpty(t, text, "Can't find WIP text") - assert.Contains(t, text, i18n.Tr("en", "repo.pulls.cannot_merge_work_in_progress"), "Unable to find WIP text") + assert.Contains(t, text, translation.NewLocale("en-US").Tr("repo.pulls.cannot_merge_work_in_progress"), "Unable to find WIP text") assert.Contains(t, text, "[wip]", "Unable to find WIP text") }) } diff --git a/integrations/release_test.go b/integrations/release_test.go index d75d74956e..dd32a64ed5 100644 --- a/integrations/release_test.go +++ b/integrations/release_test.go @@ -14,7 +14,7 @@ import ( "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" - "code.gitea.io/gitea/modules/translation/i18n" + "code.gitea.io/gitea/modules/translation" "github.com/PuerkitoBio/goquery" "github.com/stretchr/testify/assert" @@ -86,7 +86,7 @@ func TestCreateRelease(t *testing.T) { session := loginUser(t, "user2") createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, false) - checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 4) + checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", translation.NewLocale("en-US").Tr("repo.release.stable"), 4) } func TestCreateReleasePreRelease(t *testing.T) { @@ -95,7 +95,7 @@ func TestCreateReleasePreRelease(t *testing.T) { session := loginUser(t, "user2") createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", true, false) - checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 4) + checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", translation.NewLocale("en-US").Tr("repo.release.prerelease"), 4) } func TestCreateReleaseDraft(t *testing.T) { @@ -104,7 +104,7 @@ func TestCreateReleaseDraft(t *testing.T) { session := loginUser(t, "user2") createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, true) - checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 4) + checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", translation.NewLocale("en-US").Tr("repo.release.draft"), 4) } func TestCreateReleasePaging(t *testing.T) { @@ -124,11 +124,11 @@ func TestCreateReleasePaging(t *testing.T) { } createNewRelease(t, session, "/user2/repo1", "v0.0.12", "v0.0.12", false, true) - checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.12", i18n.Tr("en", "repo.release.draft"), 10) + checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.12", translation.NewLocale("en-US").Tr("repo.release.draft"), 10) // Check that user4 does not see draft and still see 10 latest releases session2 := loginUser(t, "user4") - checkLatestReleaseAndCount(t, session2, "/user2/repo1", "v0.0.11", i18n.Tr("en", "repo.release.stable"), 10) + checkLatestReleaseAndCount(t, session2, "/user2/repo1", "v0.0.11", translation.NewLocale("en-US").Tr("repo.release.stable"), 10) } func TestViewReleaseListNoLogin(t *testing.T) { diff --git a/integrations/repo_branch_test.go b/integrations/repo_branch_test.go index 30a446ccec..74d85c5b1f 100644 --- a/integrations/repo_branch_test.go +++ b/integrations/repo_branch_test.go @@ -13,7 +13,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" - "code.gitea.io/gitea/modules/translation/i18n" + "code.gitea.io/gitea/modules/translation" "github.com/stretchr/testify/assert" ) @@ -52,37 +52,37 @@ func testCreateBranches(t *testing.T, giteaURL *url.URL) { OldRefSubURL: "branch/master", NewBranch: "feature/test1", ExpectedStatus: http.StatusSeeOther, - FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test1"), + FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature/test1"), }, { OldRefSubURL: "branch/master", NewBranch: "", ExpectedStatus: http.StatusSeeOther, - FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.require_error"), + FlashMessage: translation.NewLocale("en-US").Tr("form.NewBranchName") + translation.NewLocale("en-US").Tr("form.require_error"), }, { OldRefSubURL: "branch/master", NewBranch: "feature=test1", ExpectedStatus: http.StatusSeeOther, - FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature=test1"), + FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature=test1"), }, { OldRefSubURL: "branch/master", NewBranch: strings.Repeat("b", 101), ExpectedStatus: http.StatusSeeOther, - FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.max_size_error", "100"), + FlashMessage: translation.NewLocale("en-US").Tr("form.NewBranchName") + translation.NewLocale("en-US").Tr("form.max_size_error", "100"), }, { OldRefSubURL: "branch/master", NewBranch: "master", ExpectedStatus: http.StatusSeeOther, - FlashMessage: i18n.Tr("en", "repo.branch.branch_already_exists", "master"), + FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.branch_already_exists", "master"), }, { OldRefSubURL: "branch/master", NewBranch: "master/test", ExpectedStatus: http.StatusSeeOther, - FlashMessage: i18n.Tr("en", "repo.branch.branch_name_conflict", "master/test", "master"), + FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.branch_name_conflict", "master/test", "master"), }, { OldRefSubURL: "commit/acd1d892867872cb47f3993468605b8aa59aa2e0", @@ -93,21 +93,21 @@ func testCreateBranches(t *testing.T, giteaURL *url.URL) { OldRefSubURL: "commit/65f1bf27bc3bf70f64657658635e66094edbcb4d", NewBranch: "feature/test3", ExpectedStatus: http.StatusSeeOther, - FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test3"), + FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature/test3"), }, { OldRefSubURL: "branch/master", NewBranch: "v1.0.0", CreateRelease: "v1.0.0", ExpectedStatus: http.StatusSeeOther, - FlashMessage: i18n.Tr("en", "repo.branch.tag_collision", "v1.0.0"), + FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.tag_collision", "v1.0.0"), }, { OldRefSubURL: "tag/v1.0.0", NewBranch: "feature/test4", CreateRelease: "v1.0.1", ExpectedStatus: http.StatusSeeOther, - FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test4"), + FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature/test4"), }, } for _, test := range tests { diff --git a/integrations/signin_test.go b/integrations/signin_test.go index 811f9326ec..952efcfdd9 100644 --- a/integrations/signin_test.go +++ b/integrations/signin_test.go @@ -11,7 +11,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/translation/i18n" + "code.gitea.io/gitea/modules/translation" "github.com/stretchr/testify/assert" ) @@ -47,10 +47,10 @@ func TestSignin(t *testing.T) { password string message string }{ - {username: "wrongUsername", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")}, - {username: "wrongUsername", password: "password", message: i18n.Tr("en", "form.username_password_incorrect")}, - {username: "user15", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")}, - {username: "user1@example.com", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")}, + {username: "wrongUsername", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")}, + {username: "wrongUsername", password: "password", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")}, + {username: "user15", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")}, + {username: "user1@example.com", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")}, } for _, s := range samples { diff --git a/integrations/signup_test.go b/integrations/signup_test.go index 7b45674376..b34e40f286 100644 --- a/integrations/signup_test.go +++ b/integrations/signup_test.go @@ -13,7 +13,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/translation/i18n" + "code.gitea.io/gitea/modules/translation" "github.com/stretchr/testify/assert" ) @@ -68,9 +68,9 @@ func TestSignupEmail(t *testing.T) { wantStatus int wantMsg string }{ - {"exampleUser@example.com\r\n", http.StatusOK, i18n.Tr("en", "form.email_invalid")}, - {"exampleUser@example.com\r", http.StatusOK, i18n.Tr("en", "form.email_invalid")}, - {"exampleUser@example.com\n", http.StatusOK, i18n.Tr("en", "form.email_invalid")}, + {"exampleUser@example.com\r\n", http.StatusOK, translation.NewLocale("en-US").Tr("form.email_invalid")}, + {"exampleUser@example.com\r", http.StatusOK, translation.NewLocale("en-US").Tr("form.email_invalid")}, + {"exampleUser@example.com\n", http.StatusOK, translation.NewLocale("en-US").Tr("form.email_invalid")}, {"exampleUser@example.com", http.StatusSeeOther, ""}, } diff --git a/integrations/user_test.go b/integrations/user_test.go index 41127a4e40..33113369a7 100644 --- a/integrations/user_test.go +++ b/integrations/user_test.go @@ -14,7 +14,7 @@ import ( user_model "code.gitea.io/gitea/models/user" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" - "code.gitea.io/gitea/modules/translation/i18n" + "code.gitea.io/gitea/modules/translation" "github.com/stretchr/testify/assert" ) @@ -67,7 +67,7 @@ func TestRenameInvalidUsername(t *testing.T) { htmlDoc := NewHTMLParser(t, resp.Body) assert.Contains(t, htmlDoc.doc.Find(".ui.negative.message").Text(), - i18n.Tr("en", "form.alpha_dash_dot_error"), + translation.NewLocale("en-US").Tr("form.alpha_dash_dot_error"), ) unittest.AssertNotExistsBean(t, &user_model.User{Name: invalidUsername}) @@ -131,7 +131,7 @@ func TestRenameReservedUsername(t *testing.T) { htmlDoc := NewHTMLParser(t, resp.Body) assert.Contains(t, htmlDoc.doc.Find(".ui.negative.message").Text(), - i18n.Tr("en", "user.form.name_reserved", reservedUsername), + translation.NewLocale("en-US").Tr("user.form.name_reserved", reservedUsername), ) unittest.AssertNotExistsBean(t, &user_model.User{Name: reservedUsername}) -- cgit v1.2.3