Skip to content

Commit 3ea09eb

Browse files
committed
remove legacy i18n
1 parent cf5d4a7 commit 3ea09eb

File tree

20 files changed

+239
-31
lines changed

20 files changed

+239
-31
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ require (
7878
github.com/stretchr/testify v1.7.0
7979
github.com/syndtr/goleveldb v1.0.0
8080
github.com/tstranex/u2f v1.0.0
81-
github.com/unknwon/i18n v0.0.0-20210904045753-ff3a8617e361
8281
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae
8382
github.com/unrolled/render v1.4.1
8483
github.com/urfave/cli v1.22.5

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,8 +1503,6 @@ github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0o
15031503
github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM=
15041504
github.com/unknwon/com v1.0.1 h1:3d1LTxD+Lnf3soQiD4Cp/0BRB+Rsa/+RTvz8GMMzIXs=
15051505
github.com/unknwon/com v1.0.1/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM=
1506-
github.com/unknwon/i18n v0.0.0-20210904045753-ff3a8617e361 h1:4Ij5sX4JEzCCY/CCl8trJHey1tPsIDomYTZf145GKk0=
1507-
github.com/unknwon/i18n v0.0.0-20210904045753-ff3a8617e361/go.mod h1:+5rDk6sDGpl3azws3O+f+GpFSyN9GVr0K8cvQLQM2ZQ=
15081506
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae h1:ihaXiJkaca54IaCSnEXtE/uSZOmPxKZhDfVLrzZLFDs=
15091507
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae/go.mod h1:1fdkY6xxl6ExVs2QFv7R0F5IRZHKA8RahhB9fMC9RvM=
15101508
github.com/unrolled/render v1.4.1 h1:VdpMc2YkAOWzbmC/P2yoHhRDXgsaCQHcTJ1KK6SNCA4=
@@ -2272,7 +2270,6 @@ gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AW
22722270
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
22732271
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
22742272
gopkg.in/ini.v1 v1.44.2/go.mod h1:M3Cogqpuv0QCi3ExAY5V4uOt4qb/R3xZubo9m8lK5wg=
2275-
gopkg.in/ini.v1 v1.46.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
22762273
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
22772274
gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
22782275
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

integrations/auth_ldap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ 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"
1920
"code.gitea.io/gitea/services/auth"
2021

2122
"github.com/stretchr/testify/assert"
22-
"github.com/unknwon/i18n"
2323
)
2424

2525
type ldapUser struct {

integrations/branches_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
"net/url"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/translation/i18n"
13+
1214
"github.com/stretchr/testify/assert"
13-
"github.com/unknwon/i18n"
1415
)
1516

1617
func TestViewBranches(t *testing.T) {

integrations/pull_merge_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
"code.gitea.io/gitea/modules/git"
2525
api "code.gitea.io/gitea/modules/structs"
2626
"code.gitea.io/gitea/modules/test"
27+
"code.gitea.io/gitea/modules/translation/i18n"
2728
"code.gitea.io/gitea/services/pull"
2829

2930
"github.com/stretchr/testify/assert"
30-
"github.com/unknwon/i18n"
3131
)
3232

3333
func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum string, mergeStyle repo_model.MergeStyle) *httptest.ResponseRecorder {

integrations/release_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ 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"
1718

1819
"github.com/PuerkitoBio/goquery"
1920
"github.com/stretchr/testify/assert"
20-
"github.com/unknwon/i18n"
2121
)
2222

2323
func createNewRelease(t *testing.T, session *TestSession, repoURL, tag, title string, preRelease, draft bool) {

integrations/repo_branch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313

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

1718
"github.com/stretchr/testify/assert"
18-
"github.com/unknwon/i18n"
1919
)
2020

2121
func testCreateBranch(t testing.TB, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {

integrations/signin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ 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"
1415

1516
"github.com/stretchr/testify/assert"
16-
"github.com/unknwon/i18n"
1717
)
1818

1919
func testLoginFailed(t *testing.T, username, password, message string) {

integrations/signup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ 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"
1617

1718
"github.com/stretchr/testify/assert"
18-
"github.com/unknwon/i18n"
1919
)
2020

2121
func TestSignup(t *testing.T) {

integrations/user_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"code.gitea.io/gitea/models/unittest"
1212
user_model "code.gitea.io/gitea/models/user"
1313
"code.gitea.io/gitea/modules/test"
14+
"code.gitea.io/gitea/modules/translation/i18n"
1415

1516
"github.com/stretchr/testify/assert"
16-
"github.com/unknwon/i18n"
1717
)
1818

1919
func TestViewUser(t *testing.T) {

modules/markup/markdown/toc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"fmt"
99
"net/url"
1010

11-
"github.com/unknwon/i18n"
11+
"code.gitea.io/gitea/modules/translation/i18n"
12+
1213
"github.com/yuin/goldmark/ast"
1314
)
1415

modules/timeutil/since.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import (
1212
"time"
1313

1414
"code.gitea.io/gitea/modules/setting"
15-
16-
"github.com/unknwon/i18n"
15+
"code.gitea.io/gitea/modules/translation/i18n"
1716
)
1817

1918
// Seconds-based time units

modules/timeutil/since_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212

1313
"code.gitea.io/gitea/modules/setting"
1414
"code.gitea.io/gitea/modules/translation"
15+
"code.gitea.io/gitea/modules/translation/i18n"
1516

1617
"github.com/stretchr/testify/assert"
17-
"github.com/unknwon/i18n"
1818
)
1919

2020
var BaseDate time.Time

modules/translation/i18n/i18n.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package i18n
6+
7+
import (
8+
"errors"
9+
"fmt"
10+
"reflect"
11+
"strings"
12+
13+
"code.gitea.io/gitea/modules/log"
14+
15+
"gopkg.in/ini.v1"
16+
)
17+
18+
var (
19+
ErrLocaleAlreadyExist = errors.New("lang already exists")
20+
ErrLocaleAlreadyAdded = errors.New("lang was already added to another store")
21+
22+
DefaultLocales = NewLocaleStore()
23+
)
24+
25+
type locale struct {
26+
store *LocaleStore
27+
langName string
28+
langDesc string
29+
messages *ini.File
30+
}
31+
32+
type LocaleStore struct {
33+
// at the moment, all these fields are readonly after initialization
34+
langNames []string
35+
langDescs []string
36+
localeMap map[string]*locale
37+
defaultLang string
38+
}
39+
40+
func NewLocaleStore() *LocaleStore {
41+
return &LocaleStore{localeMap: make(map[string]*locale)}
42+
}
43+
44+
// AddLocale adds locale into the store, and the locale is attached to the store, it can not be added to another one
45+
func (ls *LocaleStore) AddLocale(lc *locale) error {
46+
if _, ok := ls.localeMap[lc.langName]; ok {
47+
return ErrLocaleAlreadyExist
48+
}
49+
50+
if lc.store != nil {
51+
return ErrLocaleAlreadyAdded
52+
}
53+
lc.store = ls
54+
ls.langNames = append(ls.langNames, lc.langName)
55+
ls.langDescs = append(ls.langDescs, lc.langDesc)
56+
ls.localeMap[lc.langName] = lc
57+
return nil
58+
}
59+
60+
// AddLocaleByIni adds locale by ini into the store
61+
func (ls *LocaleStore) AddLocaleByIni(langName, langDesc string, localeFile interface{}, otherLocaleFiles ...interface{}) error {
62+
iniFile, err := ini.LoadSources(ini.LoadOptions{
63+
IgnoreInlineComment: true,
64+
UnescapeValueCommentSymbols: true,
65+
}, localeFile, otherLocaleFiles...)
66+
if err == nil {
67+
iniFile.BlockMode = false
68+
return ls.AddLocale(&locale{langName: langName, langDesc: langDesc, messages: iniFile})
69+
}
70+
return err
71+
}
72+
73+
func (ls *LocaleStore) HasLang(langName string) bool {
74+
_, ok := ls.localeMap[langName]
75+
return ok
76+
}
77+
78+
func (ls *LocaleStore) ListLangNameDesc() (names, desc []string) {
79+
return ls.langNames, ls.langDescs
80+
}
81+
82+
// SetDefaultLang sets default language as a fallback
83+
func (ls *LocaleStore) SetDefaultLang(lang string) {
84+
ls.defaultLang = lang
85+
}
86+
87+
// Tr translates content to target language. fall back to default language.
88+
func (ls *LocaleStore) Tr(lang, trKey string, trArgs ...interface{}) string {
89+
l, ok := ls.localeMap[lang]
90+
if !ok {
91+
l, ok = ls.localeMap[ls.defaultLang]
92+
}
93+
if ok {
94+
return l.Tr(trKey, trArgs...)
95+
}
96+
return trKey
97+
}
98+
99+
// Tr translates content to locale language. fall back to default language.
100+
func (l *locale) Tr(trKey string, trArgs ...interface{}) string {
101+
var section string
102+
103+
idx := strings.IndexByte(trKey, '.')
104+
if idx > 0 {
105+
section = trKey[:idx]
106+
trKey = trKey[idx+1:]
107+
}
108+
109+
trMsg := trKey
110+
if trIni, err := l.messages.Section(section).GetKey(trKey); err == nil {
111+
trMsg = trIni.Value()
112+
} else if l.store.defaultLang != "" && l.langName != l.store.defaultLang {
113+
// try to fall back to default
114+
if defaultLocale, ok := l.store.localeMap[l.store.defaultLang]; ok {
115+
if trIni, err = defaultLocale.messages.Section(section).GetKey(trKey); err == nil {
116+
trMsg = trIni.Value()
117+
}
118+
}
119+
}
120+
121+
if len(trArgs) > 0 {
122+
fmtArgs := make([]interface{}, 0, len(trArgs))
123+
for _, arg := range trArgs {
124+
val := reflect.ValueOf(arg)
125+
if val.Kind() == reflect.Slice {
126+
// before, it can accept Tr(lang, key, a, [b, c], d, [e, f]) as Sprintf(msg, a, b, c, d, e, f), it's an unstable behavior
127+
// now, we restrict the strange behavior and only support:
128+
// 1. Tr(lang, key, [slice-items]) as Sprintf(msg, items...)
129+
// 2. Tr(lang, key, args...) as Sprintf(msg, args...)
130+
if len(trArgs) == 1 {
131+
for i := 0; i < val.Len(); i++ {
132+
fmtArgs = append(fmtArgs, val.Index(i).Interface())
133+
}
134+
} else {
135+
log.Error("the args for i18n shouldn't contain uncertain slices, key=%q, args=%v", trKey, trArgs)
136+
break
137+
}
138+
} else {
139+
fmtArgs = append(fmtArgs, arg)
140+
}
141+
}
142+
return fmt.Sprintf(trMsg, fmtArgs...)
143+
}
144+
return trMsg
145+
}
146+
147+
func ResetDefaultLocales() {
148+
DefaultLocales = NewLocaleStore()
149+
}
150+
151+
// Tr use default locales to translate content to target language.
152+
func Tr(lang, trKey string, trArgs ...interface{}) string {
153+
return DefaultLocales.Tr(lang, trKey, trArgs...)
154+
}

modules/translation/i18n/i18n_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package i18n
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func Test_Tr(t *testing.T) {
14+
testData1 := []byte(`
15+
.dot.name = Dot Name
16+
fmt = %[1]s %[2]s
17+
18+
[section]
19+
sub = Sub String
20+
mixed = test value; <span style="color: red\; background: none;">more text</span>
21+
`)
22+
23+
testData2 := []byte(`
24+
fmt = %[2]s %[1]s
25+
26+
[section]
27+
sub = Changed Sub String
28+
`)
29+
30+
ls := NewLocaleStore()
31+
assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", testData1))
32+
assert.NoError(t, ls.AddLocaleByIni("lang2", "Lang2", testData2))
33+
ls.SetDefaultLang("lang1")
34+
35+
result := ls.Tr("lang1", "fmt", "a", "b")
36+
assert.Equal(t, "a b", result)
37+
38+
result = ls.Tr("lang2", "fmt", "a", "b")
39+
assert.Equal(t, "b a", result)
40+
41+
result = ls.Tr("lang1", "section.sub")
42+
assert.Equal(t, "Sub String", result)
43+
44+
result = ls.Tr("lang2", "section.sub")
45+
assert.Equal(t, "Changed Sub String", result)
46+
47+
result = ls.Tr("", ".dot.name")
48+
assert.Equal(t, "Dot Name", result)
49+
50+
result = ls.Tr("lang2", "section.mixed")
51+
assert.Equal(t, `test value; <span style="color: red; background: none;">more text</span>`, result)
52+
53+
langs, descs := ls.ListLangNameDesc()
54+
assert.Equal(t, []string{"lang1", "lang2"}, langs)
55+
assert.Equal(t, []string{"Lang1", "Lang2"}, descs)
56+
}

0 commit comments

Comments
 (0)