Skip to content

Commit 5d3f99c

Browse files
Gusteddelvh6543wxiaoguangzeripath
authored
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 <[email protected]> * 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 <[email protected]> * Fix live-reloading & check for errors * Make linter happy * live-reload with periodic check (#5) * Fix tests Co-authored-by: delvh <[email protected]> Co-authored-by: 6543 <[email protected]> Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 711cbcc commit 5d3f99c

Some content is hidden

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

60 files changed

+337
-279
lines changed

integrations/auth_ldap_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"code.gitea.io/gitea/models/organization"
1717
"code.gitea.io/gitea/models/unittest"
1818
user_model "code.gitea.io/gitea/models/user"
19-
"code.gitea.io/gitea/modules/translation/i18n"
19+
"code.gitea.io/gitea/modules/translation"
2020
"code.gitea.io/gitea/services/auth"
2121

2222
"github.com/stretchr/testify/assert"
@@ -275,8 +275,7 @@ func TestLDAPUserSigninFailed(t *testing.T) {
275275
addAuthSourceLDAP(t, "")
276276

277277
u := otherLDAPUsers[0]
278-
279-
testLoginFailed(t, u.UserName, u.Password, i18n.Tr("en", "form.username_password_incorrect"))
278+
testLoginFailed(t, u.UserName, u.Password, translation.NewLocale("en-US").Tr("form.username_password_incorrect"))
280279
}
281280

282281
func TestLDAPUserSSHKeySync(t *testing.T) {

integrations/branches_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"net/url"
1010
"testing"
1111

12-
"code.gitea.io/gitea/modules/translation/i18n"
12+
"code.gitea.io/gitea/modules/translation"
1313

1414
"github.com/stretchr/testify/assert"
1515
)
@@ -37,7 +37,7 @@ func TestUndoDeleteBranch(t *testing.T) {
3737
htmlDoc, name := branchAction(t, ".undo-button")
3838
assert.Contains(t,
3939
htmlDoc.doc.Find(".ui.positive.message").Text(),
40-
i18n.Tr("en", "repo.branch.restore_success", name),
40+
translation.NewLocale("en-US").Tr("repo.branch.restore_success", name),
4141
)
4242
})
4343
}
@@ -46,7 +46,7 @@ func deleteBranch(t *testing.T) {
4646
htmlDoc, name := branchAction(t, ".delete-branch-button")
4747
assert.Contains(t,
4848
htmlDoc.doc.Find(".ui.positive.message").Text(),
49-
i18n.Tr("en", "repo.branch.deletion_success", name),
49+
translation.NewLocale("en-US").Tr("repo.branch.deletion_success", name),
5050
)
5151
}
5252

integrations/pull_merge_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"code.gitea.io/gitea/modules/git"
2828
api "code.gitea.io/gitea/modules/structs"
2929
"code.gitea.io/gitea/modules/test"
30-
"code.gitea.io/gitea/modules/translation/i18n"
30+
"code.gitea.io/gitea/modules/translation"
3131
"code.gitea.io/gitea/services/pull"
3232
repo_service "code.gitea.io/gitea/services/repository"
3333
files_service "code.gitea.io/gitea/services/repository/files"
@@ -204,7 +204,7 @@ func TestCantMergeWorkInProgress(t *testing.T) {
204204
text := strings.TrimSpace(htmlDoc.doc.Find(".merge-section > .item").Last().Text())
205205
assert.NotEmpty(t, text, "Can't find WIP text")
206206

207-
assert.Contains(t, text, i18n.Tr("en", "repo.pulls.cannot_merge_work_in_progress"), "Unable to find WIP text")
207+
assert.Contains(t, text, translation.NewLocale("en-US").Tr("repo.pulls.cannot_merge_work_in_progress"), "Unable to find WIP text")
208208
assert.Contains(t, text, "[wip]", "Unable to find WIP text")
209209
})
210210
}

integrations/release_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"code.gitea.io/gitea/models/unittest"
1515
"code.gitea.io/gitea/modules/setting"
1616
"code.gitea.io/gitea/modules/test"
17-
"code.gitea.io/gitea/modules/translation/i18n"
17+
"code.gitea.io/gitea/modules/translation"
1818

1919
"github.com/PuerkitoBio/goquery"
2020
"github.com/stretchr/testify/assert"
@@ -86,7 +86,7 @@ func TestCreateRelease(t *testing.T) {
8686
session := loginUser(t, "user2")
8787
createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, false)
8888

89-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 4)
89+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", translation.NewLocale("en-US").Tr("repo.release.stable"), 4)
9090
}
9191

9292
func TestCreateReleasePreRelease(t *testing.T) {
@@ -95,7 +95,7 @@ func TestCreateReleasePreRelease(t *testing.T) {
9595
session := loginUser(t, "user2")
9696
createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", true, false)
9797

98-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 4)
98+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", translation.NewLocale("en-US").Tr("repo.release.prerelease"), 4)
9999
}
100100

101101
func TestCreateReleaseDraft(t *testing.T) {
@@ -104,7 +104,7 @@ func TestCreateReleaseDraft(t *testing.T) {
104104
session := loginUser(t, "user2")
105105
createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, true)
106106

107-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 4)
107+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", translation.NewLocale("en-US").Tr("repo.release.draft"), 4)
108108
}
109109

110110
func TestCreateReleasePaging(t *testing.T) {
@@ -124,11 +124,11 @@ func TestCreateReleasePaging(t *testing.T) {
124124
}
125125
createNewRelease(t, session, "/user2/repo1", "v0.0.12", "v0.0.12", false, true)
126126

127-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.12", i18n.Tr("en", "repo.release.draft"), 10)
127+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.12", translation.NewLocale("en-US").Tr("repo.release.draft"), 10)
128128

129129
// Check that user4 does not see draft and still see 10 latest releases
130130
session2 := loginUser(t, "user4")
131-
checkLatestReleaseAndCount(t, session2, "/user2/repo1", "v0.0.11", i18n.Tr("en", "repo.release.stable"), 10)
131+
checkLatestReleaseAndCount(t, session2, "/user2/repo1", "v0.0.11", translation.NewLocale("en-US").Tr("repo.release.stable"), 10)
132132
}
133133

134134
func TestViewReleaseListNoLogin(t *testing.T) {

integrations/repo_branch_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
"code.gitea.io/gitea/modules/setting"
1515
"code.gitea.io/gitea/modules/test"
16-
"code.gitea.io/gitea/modules/translation/i18n"
16+
"code.gitea.io/gitea/modules/translation"
1717

1818
"github.com/stretchr/testify/assert"
1919
)
@@ -52,37 +52,37 @@ func testCreateBranches(t *testing.T, giteaURL *url.URL) {
5252
OldRefSubURL: "branch/master",
5353
NewBranch: "feature/test1",
5454
ExpectedStatus: http.StatusSeeOther,
55-
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test1"),
55+
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature/test1"),
5656
},
5757
{
5858
OldRefSubURL: "branch/master",
5959
NewBranch: "",
6060
ExpectedStatus: http.StatusSeeOther,
61-
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.require_error"),
61+
FlashMessage: translation.NewLocale("en-US").Tr("form.NewBranchName") + translation.NewLocale("en-US").Tr("form.require_error"),
6262
},
6363
{
6464
OldRefSubURL: "branch/master",
6565
NewBranch: "feature=test1",
6666
ExpectedStatus: http.StatusSeeOther,
67-
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature=test1"),
67+
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature=test1"),
6868
},
6969
{
7070
OldRefSubURL: "branch/master",
7171
NewBranch: strings.Repeat("b", 101),
7272
ExpectedStatus: http.StatusSeeOther,
73-
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.max_size_error", "100"),
73+
FlashMessage: translation.NewLocale("en-US").Tr("form.NewBranchName") + translation.NewLocale("en-US").Tr("form.max_size_error", "100"),
7474
},
7575
{
7676
OldRefSubURL: "branch/master",
7777
NewBranch: "master",
7878
ExpectedStatus: http.StatusSeeOther,
79-
FlashMessage: i18n.Tr("en", "repo.branch.branch_already_exists", "master"),
79+
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.branch_already_exists", "master"),
8080
},
8181
{
8282
OldRefSubURL: "branch/master",
8383
NewBranch: "master/test",
8484
ExpectedStatus: http.StatusSeeOther,
85-
FlashMessage: i18n.Tr("en", "repo.branch.branch_name_conflict", "master/test", "master"),
85+
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.branch_name_conflict", "master/test", "master"),
8686
},
8787
{
8888
OldRefSubURL: "commit/acd1d892867872cb47f3993468605b8aa59aa2e0",
@@ -93,21 +93,21 @@ func testCreateBranches(t *testing.T, giteaURL *url.URL) {
9393
OldRefSubURL: "commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
9494
NewBranch: "feature/test3",
9595
ExpectedStatus: http.StatusSeeOther,
96-
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test3"),
96+
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature/test3"),
9797
},
9898
{
9999
OldRefSubURL: "branch/master",
100100
NewBranch: "v1.0.0",
101101
CreateRelease: "v1.0.0",
102102
ExpectedStatus: http.StatusSeeOther,
103-
FlashMessage: i18n.Tr("en", "repo.branch.tag_collision", "v1.0.0"),
103+
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.tag_collision", "v1.0.0"),
104104
},
105105
{
106106
OldRefSubURL: "tag/v1.0.0",
107107
NewBranch: "feature/test4",
108108
CreateRelease: "v1.0.1",
109109
ExpectedStatus: http.StatusSeeOther,
110-
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test4"),
110+
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature/test4"),
111111
},
112112
}
113113
for _, test := range tests {

integrations/signin_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/models/unittest"
1313
user_model "code.gitea.io/gitea/models/user"
14-
"code.gitea.io/gitea/modules/translation/i18n"
14+
"code.gitea.io/gitea/modules/translation"
1515

1616
"github.com/stretchr/testify/assert"
1717
)
@@ -47,10 +47,10 @@ func TestSignin(t *testing.T) {
4747
password string
4848
message string
4949
}{
50-
{username: "wrongUsername", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
51-
{username: "wrongUsername", password: "password", message: i18n.Tr("en", "form.username_password_incorrect")},
52-
{username: "user15", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
53-
{username: "[email protected]", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
50+
{username: "wrongUsername", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
51+
{username: "wrongUsername", password: "password", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
52+
{username: "user15", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
53+
{username: "[email protected]", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
5454
}
5555

5656
for _, s := range samples {

integrations/signup_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"code.gitea.io/gitea/models/unittest"
1414
user_model "code.gitea.io/gitea/models/user"
1515
"code.gitea.io/gitea/modules/setting"
16-
"code.gitea.io/gitea/modules/translation/i18n"
16+
"code.gitea.io/gitea/modules/translation"
1717

1818
"github.com/stretchr/testify/assert"
1919
)
@@ -68,9 +68,9 @@ func TestSignupEmail(t *testing.T) {
6868
wantStatus int
6969
wantMsg string
7070
}{
71-
{"[email protected]\r\n", http.StatusOK, i18n.Tr("en", "form.email_invalid")},
72-
{"[email protected]\r", http.StatusOK, i18n.Tr("en", "form.email_invalid")},
73-
{"[email protected]\n", http.StatusOK, i18n.Tr("en", "form.email_invalid")},
71+
{"[email protected]\r\n", http.StatusOK, translation.NewLocale("en-US").Tr("form.email_invalid")},
72+
{"[email protected]\r", http.StatusOK, translation.NewLocale("en-US").Tr("form.email_invalid")},
73+
{"[email protected]\n", http.StatusOK, translation.NewLocale("en-US").Tr("form.email_invalid")},
7474
{"[email protected]", http.StatusSeeOther, ""},
7575
}
7676

integrations/user_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
user_model "code.gitea.io/gitea/models/user"
1515
api "code.gitea.io/gitea/modules/structs"
1616
"code.gitea.io/gitea/modules/test"
17-
"code.gitea.io/gitea/modules/translation/i18n"
17+
"code.gitea.io/gitea/modules/translation"
1818

1919
"github.com/stretchr/testify/assert"
2020
)
@@ -67,7 +67,7 @@ func TestRenameInvalidUsername(t *testing.T) {
6767
htmlDoc := NewHTMLParser(t, resp.Body)
6868
assert.Contains(t,
6969
htmlDoc.doc.Find(".ui.negative.message").Text(),
70-
i18n.Tr("en", "form.alpha_dash_dot_error"),
70+
translation.NewLocale("en-US").Tr("form.alpha_dash_dot_error"),
7171
)
7272

7373
unittest.AssertNotExistsBean(t, &user_model.User{Name: invalidUsername})
@@ -131,7 +131,7 @@ func TestRenameReservedUsername(t *testing.T) {
131131
htmlDoc := NewHTMLParser(t, resp.Body)
132132
assert.Contains(t,
133133
htmlDoc.doc.Find(".ui.negative.message").Text(),
134-
i18n.Tr("en", "user.form.name_reserved", reservedUsername),
134+
translation.NewLocale("en-US").Tr("user.form.name_reserved", reservedUsername),
135135
)
136136

137137
unittest.AssertNotExistsBean(t, &user_model.User{Name: reservedUsername})

modules/markup/markdown/toc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"net/url"
1010

1111
"code.gitea.io/gitea/modules/markup"
12-
"code.gitea.io/gitea/modules/translation/i18n"
12+
"code.gitea.io/gitea/modules/translation"
1313

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

21-
summary.AppendChild(summary, ast.NewString([]byte(i18n.Tr(lang, "toc"))))
21+
summary.AppendChild(summary, ast.NewString([]byte(translation.NewLocale(lang).Tr("toc"))))
2222
details.AppendChild(details, summary)
2323
ul := ast.NewList('-')
2424
details.AppendChild(details, ul)

modules/templates/helper.go

-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func NewFuncMap() []template.FuncMap {
105105
"Str2html": Str2html,
106106
"TimeSince": timeutil.TimeSince,
107107
"TimeSinceUnix": timeutil.TimeSinceUnix,
108-
"RawTimeSince": timeutil.RawTimeSince,
109108
"FileSize": base.FileSize,
110109
"PrettyNumber": base.PrettyNumber,
111110
"JsPrettyNumber": JsPrettyNumber,
@@ -484,7 +483,6 @@ func NewTextFuncMap() []texttmpl.FuncMap {
484483
},
485484
"TimeSince": timeutil.TimeSince,
486485
"TimeSinceUnix": timeutil.TimeSinceUnix,
487-
"RawTimeSince": timeutil.RawTimeSince,
488486
"DateFmtLong": func(t time.Time) string {
489487
return t.Format(time.RFC1123Z)
490488
},

0 commit comments

Comments
 (0)