Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make better use of i18n #20096

Merged
merged 36 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
24abae5
Prototyping
Jun 21, 2022
48b9b21
Start work on creating offsets
Jun 22, 2022
8b7ca1c
Modify tests
Jun 22, 2022
8427dbd
Start prototyping with actual MPH
Jun 22, 2022
12605ec
Twiddle around
Jun 23, 2022
b25d0d5
Twiddle around comments
Jun 23, 2022
f70b31a
Convert templates
Jun 23, 2022
f466981
Fix external languages
Jun 23, 2022
ddaf0cb
Fix latest translation
Jun 23, 2022
78bdc57
Fix some test
Jun 23, 2022
ace2fd1
Merge branch 'main' into wow-maths
Jun 23, 2022
24c973b
Tidy up code
Jun 23, 2022
4788be5
Merge branch 'main' into wow-maths
Jun 23, 2022
d498472
Use simple map
Jun 24, 2022
c78f1f1
Merge branch 'main' into wow-maths
Jun 24, 2022
957207b
go mod tidy
Jun 24, 2022
6bfa1ba
Move back to data structure
Jun 24, 2022
36c7844
Apply suggestions from code review
Jun 24, 2022
cac046f
Merge branch 'main' into wow-maths
6543 Jun 24, 2022
432e952
Add some comments
Jun 24, 2022
b2935e7
Merge branch 'main' into wow-maths
Jun 24, 2022
3a6a6b4
Fix tests
Jun 24, 2022
b798d02
Try to fix tests
Jun 24, 2022
4ed361f
Merge branch 'main' into wow-maths
Jun 24, 2022
6568ca1
Use en-US as defacto fallback
Jun 25, 2022
47eb2bb
Use correct slices
Jun 25, 2022
8a1b976
refactor (#4)
wxiaoguang Jun 25, 2022
6d3e754
Remove TryTr, add log for missing translation key
Jun 25, 2022
2cffac5
Merge branch 'main' into wow-maths
zeripath Jun 25, 2022
8334002
Refactor i18n
Jun 25, 2022
f20fd87
Fix live-reloading & check for errors
Jun 25, 2022
1978b74
Make linter happy
Jun 25, 2022
b252dfd
Merge branch 'main' into wow-maths
Jun 25, 2022
05d490d
live-reload with periodic check (#5)
wxiaoguang Jun 26, 2022
fbd3e08
Fix tests
Jun 26, 2022
f66ad02
Merge branch 'main' into wow-maths
Jun 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions integrations/auth_ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions integrations/branches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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),
)
})
}
Expand All @@ -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),
)
}

Expand Down
4 changes: 2 additions & 2 deletions integrations/pull_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
})
}
Expand Down
12 changes: 6 additions & 6 deletions integrations/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions integrations/repo_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions integrations/signin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions integrations/signup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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, ""},
}

Expand Down
6 changes: 3 additions & 3 deletions integrations/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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})
Expand Down
4 changes: 2 additions & 2 deletions modules/markup/markdown/toc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"net/url"

"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/translation/i18n"
"code.gitea.io/gitea/modules/translation"

"github.com/yuin/goldmark/ast"
)
Expand All @@ -18,7 +18,7 @@ func createTOCNode(toc []markup.Header, lang string) ast.Node {
details := NewDetails()
summary := NewSummary()

summary.AppendChild(summary, ast.NewString([]byte(i18n.Tr(lang, "toc"))))
summary.AppendChild(summary, ast.NewString([]byte(translation.NewLocale(lang).Tr("toc"))))
details.AppendChild(details, summary)
ul := ast.NewList('-')
details.AppendChild(details, ul)
Expand Down
2 changes: 0 additions & 2 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func NewFuncMap() []template.FuncMap {
"Str2html": Str2html,
"TimeSince": timeutil.TimeSince,
"TimeSinceUnix": timeutil.TimeSinceUnix,
"RawTimeSince": timeutil.RawTimeSince,
"FileSize": base.FileSize,
"PrettyNumber": base.PrettyNumber,
"JsPrettyNumber": JsPrettyNumber,
Expand Down Expand Up @@ -484,7 +483,6 @@ func NewTextFuncMap() []texttmpl.FuncMap {
},
"TimeSince": timeutil.TimeSince,
"TimeSinceUnix": timeutil.TimeSinceUnix,
"RawTimeSince": timeutil.RawTimeSince,
"DateFmtLong": func(t time.Time) string {
return t.Format(time.RFC1123Z)
},
Expand Down
Loading