Skip to content

Commit 99d4a26

Browse files
committed
Unify and simplify TrN for i18n
1 parent 7eb380b commit 99d4a26

File tree

19 files changed

+132
-129
lines changed

19 files changed

+132
-129
lines changed

modules/csv/csv_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ a, b c
4747
e f
4848
g h i
4949
j l
50-
m n,
50+
m n,
5151
p q r
5252
u
5353
v w x
54-
y
54+
y
5555
`,
5656
expectedRows: [][]string{
5757
{"col1", "col2", "col3"},
@@ -74,7 +74,7 @@ y
7474
a, b, c
7575
d,e,f
7676
,h, i
77-
j, ,
77+
j, ,
7878
, , `,
7979
expectedRows: [][]string{
8080
{"col1", "col2", "col3"},
@@ -353,7 +353,7 @@ John Doe [email protected] This,note,had,a,lot,of,commas,to,test,delimters`,
353353
quoted,
354354
text," a
355355
2 "some,
356-
quoted,
356+
quoted,
357357
text," b
358358
3 "some,
359359
quoted,
@@ -459,7 +459,7 @@ func TestGuessFromBeforeAfterQuotes(t *testing.T) {
459459
quoted,
460460
text," a
461461
2 "some,
462-
quoted,
462+
quoted,
463463
text," b
464464
3 "some,
465465
quoted,
@@ -549,6 +549,10 @@ func (l mockLocale) Tr(s string, _ ...interface{}) string {
549549
return s
550550
}
551551

552+
func (l mockLocale) TrN(_cnt interface{}, key1, _keyN string, _args ...interface{}) string {
553+
return key1
554+
}
555+
552556
func TestFormatError(t *testing.T) {
553557
var cases = []struct {
554558
err error

modules/templates/helper.go

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ func NewFuncMap() []template.FuncMap {
239239
"DisableImportLocal": func() bool {
240240
return !setting.ImportLocalPaths
241241
},
242-
"TrN": TrN,
243242
"Dict": func(values ...interface{}) (map[string]interface{}, error) {
244243
if len(values)%2 != 0 {
245244
return nil, errors.New("invalid dict call")
@@ -857,69 +856,6 @@ func DiffLineTypeToStr(diffType int) string {
857856
return "same"
858857
}
859858

860-
// Language specific rules for translating plural texts
861-
var trNLangRules = map[string]func(int64) int{
862-
"en-US": func(cnt int64) int {
863-
if cnt == 1 {
864-
return 0
865-
}
866-
return 1
867-
},
868-
"lv-LV": func(cnt int64) int {
869-
if cnt%10 == 1 && cnt%100 != 11 {
870-
return 0
871-
}
872-
return 1
873-
},
874-
"ru-RU": func(cnt int64) int {
875-
if cnt%10 == 1 && cnt%100 != 11 {
876-
return 0
877-
}
878-
return 1
879-
},
880-
"zh-CN": func(cnt int64) int {
881-
return 0
882-
},
883-
"zh-HK": func(cnt int64) int {
884-
return 0
885-
},
886-
"zh-TW": func(cnt int64) int {
887-
return 0
888-
},
889-
"fr-FR": func(cnt int64) int {
890-
if cnt > -2 && cnt < 2 {
891-
return 0
892-
}
893-
return 1
894-
},
895-
}
896-
897-
// TrN returns key to be used for plural text translation
898-
func TrN(lang string, cnt interface{}, key1, keyN string) string {
899-
var c int64
900-
if t, ok := cnt.(int); ok {
901-
c = int64(t)
902-
} else if t, ok := cnt.(int16); ok {
903-
c = int64(t)
904-
} else if t, ok := cnt.(int32); ok {
905-
c = int64(t)
906-
} else if t, ok := cnt.(int64); ok {
907-
c = t
908-
} else {
909-
return keyN
910-
}
911-
912-
ruleFunc, ok := trNLangRules[lang]
913-
if !ok {
914-
ruleFunc = trNLangRules["en-US"]
915-
}
916-
917-
if ruleFunc(c) == 0 {
918-
return key1
919-
}
920-
return keyN
921-
}
922-
923859
// MigrationIcon returns a SVG name matching the service an issue/comment was migrated from
924860
func MigrationIcon(hostname string) string {
925861
switch hostname {

modules/test/context_tests.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ func (l mockLocale) Tr(s string, _ ...interface{}) string {
103103
return s
104104
}
105105

106+
func (l mockLocale) TrN(_cnt interface{}, key1, _keyN string, _args ...interface{}) string {
107+
return key1
108+
}
109+
106110
type mockResponseWriter struct {
107111
httptest.ResponseRecorder
108112
size int

modules/translation/translation.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
type Locale interface {
1818
Language() string
1919
Tr(string, ...interface{}) string
20+
TrN(cnt interface{}, key1, keyN string, args ...interface{}) string
2021
}
2122

2223
// LangType represents a lang type
@@ -99,3 +100,66 @@ func (l *locale) Language() string {
99100
func (l *locale) Tr(format string, args ...interface{}) string {
100101
return i18n.Tr(l.Lang, format, args...)
101102
}
103+
104+
// Language specific rules for translating plural texts
105+
var trNLangRules = map[string]func(int64) int{
106+
"en-US": func(cnt int64) int {
107+
if cnt == 1 {
108+
return 0
109+
}
110+
return 1
111+
},
112+
"lv-LV": func(cnt int64) int {
113+
if cnt%10 == 1 && cnt%100 != 11 {
114+
return 0
115+
}
116+
return 1
117+
},
118+
"ru-RU": func(cnt int64) int {
119+
if cnt%10 == 1 && cnt%100 != 11 {
120+
return 0
121+
}
122+
return 1
123+
},
124+
"zh-CN": func(cnt int64) int {
125+
return 0
126+
},
127+
"zh-HK": func(cnt int64) int {
128+
return 0
129+
},
130+
"zh-TW": func(cnt int64) int {
131+
return 0
132+
},
133+
"fr-FR": func(cnt int64) int {
134+
if cnt > -2 && cnt < 2 {
135+
return 0
136+
}
137+
return 1
138+
},
139+
}
140+
141+
// TrN returns translated message for plural text translation
142+
func (l *locale) TrN(cnt interface{}, key1, keyN string, args ...interface{}) string {
143+
var c int64
144+
if t, ok := cnt.(int); ok {
145+
c = int64(t)
146+
} else if t, ok := cnt.(int16); ok {
147+
c = int64(t)
148+
} else if t, ok := cnt.(int32); ok {
149+
c = int64(t)
150+
} else if t, ok := cnt.(int64); ok {
151+
c = t
152+
} else {
153+
return l.Tr(keyN, args...)
154+
}
155+
156+
ruleFunc, ok := trNLangRules[l.Lang]
157+
if !ok {
158+
ruleFunc = trNLangRules["en-US"]
159+
}
160+
161+
if ruleFunc(c) == 0 {
162+
return l.Tr(key1, args...)
163+
}
164+
return l.Tr(keyN, args...)
165+
}

routers/web/repo/migrate.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,8 @@ func handleMigrateError(ctx *context.Context, owner *user_model.User, err error,
8181
case migrations.IsTwoFactorAuthError(err):
8282
ctx.RenderWithErr(ctx.Tr("form.2fa_auth_required"), tpl, form)
8383
case repo_model.IsErrReachLimitOfRepo(err):
84-
var msg string
8584
maxCreationLimit := ctx.User.MaxCreationLimit()
86-
if maxCreationLimit == 1 {
87-
msg = ctx.Tr("repo.form.reach_limit_of_creation_1", maxCreationLimit)
88-
} else {
89-
msg = ctx.Tr("repo.form.reach_limit_of_creation_n", maxCreationLimit)
90-
}
85+
msg := ctx.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_1", maxCreationLimit)
9186
ctx.RenderWithErr(msg, tpl, form)
9287
case repo_model.IsErrRepoAlreadyExist(err):
9388
ctx.Data["Err_RepoName"] = true

routers/web/repo/repo.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,8 @@ func Create(ctx *context.Context) {
162162
func handleCreateError(ctx *context.Context, owner *user_model.User, err error, name string, tpl base.TplName, form interface{}) {
163163
switch {
164164
case repo_model.IsErrReachLimitOfRepo(err):
165-
var msg string
166165
maxCreationLimit := ctx.User.MaxCreationLimit()
167-
if maxCreationLimit == 1 {
168-
msg = ctx.Tr("repo.form.reach_limit_of_creation_1", maxCreationLimit)
169-
} else {
170-
msg = ctx.Tr("repo.form.reach_limit_of_creation_n", maxCreationLimit)
171-
}
166+
msg := ctx.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_1", maxCreationLimit)
172167
ctx.RenderWithErr(msg, tpl, form)
173168
case repo_model.IsErrRepoAlreadyExist(err):
174169
ctx.Data["Err_RepoName"] = true

routers/web/repo/setting.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,8 @@ func SettingsPost(ctx *context.Context) {
610610

611611
if !ctx.Repo.Owner.CanCreateRepo() {
612612
maxCreationLimit := ctx.User.MaxCreationLimit()
613-
if maxCreationLimit == 1 {
614-
ctx.Flash.Error(ctx.Tr("repo.form.reach_limit_of_creation_1", maxCreationLimit))
615-
} else {
616-
ctx.Flash.Error(ctx.Tr("repo.form.reach_limit_of_creation_n", maxCreationLimit))
617-
}
613+
msg := ctx.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_1", maxCreationLimit)
614+
ctx.Flash.Error(msg)
618615
ctx.Redirect(repo.Link() + "/settings")
619616
return
620617
}

services/mailer/mail.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func sendUserMail(language string, u *user_model.User, tpl base.TplName, code, s
7878
// helper
7979
"i18n": locale,
8080
"Str2html": templates.Str2html,
81-
"TrN": templates.TrN,
8281
}
8382

8483
var content bytes.Buffer
@@ -129,7 +128,6 @@ func SendActivateEmailMail(u *user_model.User, email *user_model.EmailAddress) {
129128
// helper
130129
"i18n": locale,
131130
"Str2html": templates.Str2html,
132-
"TrN": templates.TrN,
133131
}
134132

135133
var content bytes.Buffer
@@ -160,7 +158,6 @@ func SendRegisterNotifyMail(u *user_model.User) {
160158
// helper
161159
"i18n": locale,
162160
"Str2html": templates.Str2html,
163-
"TrN": templates.TrN,
164161
}
165162

166163
var content bytes.Buffer
@@ -194,7 +191,6 @@ func SendCollaboratorMail(u, doer *user_model.User, repo *repo_model.Repository)
194191
// helper
195192
"i18n": locale,
196193
"Str2html": templates.Str2html,
197-
"TrN": templates.TrN,
198194
}
199195

200196
var content bytes.Buffer
@@ -278,7 +274,6 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient
278274
// helper
279275
"i18n": locale,
280276
"Str2html": templates.Str2html,
281-
"TrN": templates.TrN,
282277
}
283278

284279
var mailSubject bytes.Buffer

services/mailer/mail_release.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ func mailNewRelease(lang string, tos []string, rel *models.Release) {
7676
// helper
7777
"i18n": locale,
7878
"Str2html": templates.Str2html,
79-
"TrN": templates.TrN,
8079
}
8180

8281
var mailBody bytes.Buffer

services/mailer/mail_repo.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func sendRepoTransferNotifyMailPerLang(lang string, newOwner, doer *user_model.U
7171
// helper
7272
"i18n": locale,
7373
"Str2html": templates.Str2html,
74-
"TrN": templates.TrN,
7574
}
7675

7776
if err := bodyTemplates.ExecuteTemplate(&content, string(mailRepoTransferNotify), data); err != nil {

templates/mail/issue/default.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
{{.i18n.Tr "mail.issue.action.force_push" .Doer.Name .Comment.Issue.PullRequest.HeadBranch $oldCommitLink $newCommitLink | Str2html}}
3232
{{else}}
33-
{{.i18n.Tr (TrN .i18n.Lang (len .Comment.Commits) "mail.issue.action.push_1" "mail.issue.action.push_n") .Doer.Name .Comment.Issue.PullRequest.HeadBranch (len .Comment.Commits) | Str2html}}
33+
{{.i18n.TrN (len .Comment.Commits) "mail.issue.action.push_1" "mail.issue.action.push_n" .Doer.Name .Comment.Issue.PullRequest.HeadBranch (len .Comment.Commits) | Str2html}}
3434
{{end}}
3535
</p>
3636
{{end}}

0 commit comments

Comments
 (0)