Skip to content

Commit 3dabadc

Browse files
committed
Convert locale files from ini to json format
1 parent 4fe1066 commit 3dabadc

Some content is hidden

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

84 files changed

+77790
-82451
lines changed

crowdin.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ base_path: "."
44
base_url: "https://api.crowdin.com"
55
preserve_hierarchy: true
66
files:
7-
- source: "/options/locale/locale_en-US.ini"
8-
translation: "/options/locale/locale_%locale%.ini"
9-
type: "ini"
7+
- source: "/options/locale/locale_en-US.json"
8+
translation: "/options/locale/locale_%locale%.json"
9+
type: "json"
1010
skip_untranslated_strings: true
1111
export_only_approved: true
1212
update_option: "update_as_unapproved"

modules/translation/i18n/i18n.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type LocaleStore interface {
3232
// HasLang returns whether a given language is present in the store
3333
HasLang(langName string) bool
3434
// AddLocaleByIni adds a new language to the store
35-
AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error
35+
AddLocaleByJSON(langName, langDesc string, source, moreSource []byte) error
3636
}
3737

3838
// ResetDefaultLocales resets the current default locales

modules/translation/i18n/i18n_test.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,23 @@ import (
1313

1414
func TestLocaleStore(t *testing.T) {
1515
testData1 := []byte(`
16-
.dot.name = Dot Name
17-
fmt = %[1]s %[2]s
16+
{
17+
".dot.name": "Dot Name",
18+
"fmt": "%[1]s %[2]s",
1819
19-
[section]
20-
sub = Sub String
21-
mixed = test value; <span style="color: red\; background: none;">%s</span>
22-
`)
20+
"section.sub": "Sub String",
21+
"section.mixed": "test value; <span style=\"color: red; background: none;\">%s</span>"
22+
}`)
2323

2424
testData2 := []byte(`
25-
fmt = %[2]s %[1]s
26-
27-
[section]
28-
sub = Changed Sub String
29-
`)
25+
{
26+
"fmt": "%[2]s %[1]s",
27+
"section.sub": "Changed Sub String"
28+
}`)
3029

3130
ls := NewLocaleStore()
32-
assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", testData1, nil))
33-
assert.NoError(t, ls.AddLocaleByIni("lang2", "Lang2", testData2, nil))
31+
assert.NoError(t, ls.AddLocaleByJSON("lang1", "Lang1", testData1, nil))
32+
assert.NoError(t, ls.AddLocaleByJSON("lang2", "Lang2", testData2, nil))
3433
ls.SetDefaultLang("lang1")
3534

3635
lang1, _ := ls.Locale("lang1")
@@ -66,17 +65,21 @@ sub = Changed Sub String
6665

6766
func TestLocaleStoreMoreSource(t *testing.T) {
6867
testData1 := []byte(`
69-
a=11
70-
b=12
68+
{
69+
"a": "11",
70+
"b": "12"
71+
}
7172
`)
7273

7374
testData2 := []byte(`
74-
b=21
75-
c=22
75+
{
76+
"b": "21",
77+
"c": "22"
78+
}
7679
`)
7780

7881
ls := NewLocaleStore()
79-
assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", testData1, testData2))
82+
assert.NoError(t, ls.AddLocaleByJSON("lang1", "Lang1", testData1, testData2))
8083
lang1, _ := ls.Locale("lang1")
8184
assert.Equal(t, "11", lang1.TrString("a"))
8285
assert.Equal(t, "21", lang1.TrString("b"))
@@ -117,7 +120,7 @@ func (e *errorPointerReceiver) Error() string {
117120

118121
func TestLocaleWithTemplate(t *testing.T) {
119122
ls := NewLocaleStore()
120-
assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", []byte(`key=<a>%s</a>`), nil))
123+
assert.NoError(t, ls.AddLocaleByJSON("lang1", "Lang1", []byte(`{"key":"<a>%s</a>"}`), nil))
121124
lang1, _ := ls.Locale("lang1")
122125

123126
tmpl := template.New("test").Funcs(template.FuncMap{"tr": lang1.TrHTML})
@@ -180,7 +183,7 @@ func TestLocaleStoreQuirks(t *testing.T) {
180183

181184
for _, testData := range testDataList {
182185
ls := NewLocaleStore()
183-
err := ls.AddLocaleByIni("lang1", "Lang1", []byte("a="+testData.in), nil)
186+
err := ls.AddLocaleByJSON("lang1", "Lang1", []byte(`{"a":"`+testData.in+`"}`), nil)
184187
lang1, _ := ls.Locale("lang1")
185188
assert.NoError(t, err, testData.hint)
186189
assert.Equal(t, testData.out, lang1.TrString("a"), testData.hint)

modules/translation/i18n/localestore.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
package i18n
55

66
import (
7+
"encoding/json"
78
"errors"
89
"fmt"
910
"html/template"
1011
"slices"
1112

1213
"code.gitea.io/gitea/modules/log"
13-
"code.gitea.io/gitea/modules/setting"
1414
)
1515

1616
// This file implements the static LocaleStore that will not watch for changes
@@ -39,8 +39,8 @@ func NewLocaleStore() LocaleStore {
3939
return &localeStore{localeMap: make(map[string]*locale), trKeyToIdxMap: make(map[string]int)}
4040
}
4141

42-
// AddLocaleByIni adds locale by ini into the store
43-
func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error {
42+
// AddLocaleByJSON adds locale by JSON into the store
43+
func (store *localeStore) AddLocaleByJSON(langName, langDesc string, source, moreSource []byte) error {
4444
if _, ok := store.localeMap[langName]; ok {
4545
return errors.New("lang has already been added")
4646
}
@@ -51,28 +51,46 @@ func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, more
5151
l := &locale{store: store, langName: langName, idxToMsgMap: make(map[int]string)}
5252
store.localeMap[l.langName] = l
5353

54-
iniFile, err := setting.NewConfigProviderForLocale(source, moreSource)
55-
if err != nil {
56-
return fmt.Errorf("unable to load ini: %w", err)
57-
}
54+
addFunc := func(source []byte) error {
55+
if len(source) <= 0 {
56+
return nil
57+
}
5858

59-
for _, section := range iniFile.Sections() {
60-
for _, key := range section.Keys() {
61-
var trKey string
62-
if section.Name() == "" || section.Name() == "DEFAULT" {
63-
trKey = key.Name()
64-
} else {
65-
trKey = section.Name() + "." + key.Name()
66-
}
67-
idx, ok := store.trKeyToIdxMap[trKey]
68-
if !ok {
69-
idx = len(store.trKeyToIdxMap)
70-
store.trKeyToIdxMap[trKey] = idx
59+
values := make(map[string]any)
60+
if err := json.Unmarshal(source, &values); err != nil {
61+
return fmt.Errorf("unable to load json: %w", err)
62+
}
63+
for trKey, value := range values {
64+
switch v := value.(type) {
65+
case string:
66+
idx, ok := store.trKeyToIdxMap[trKey]
67+
if !ok {
68+
idx = len(store.trKeyToIdxMap)
69+
store.trKeyToIdxMap[trKey] = idx
70+
}
71+
l.idxToMsgMap[idx] = v
72+
case map[string]any:
73+
for key, val := range v {
74+
idx, ok := store.trKeyToIdxMap[trKey+"."+key]
75+
if !ok {
76+
idx = len(store.trKeyToIdxMap)
77+
store.trKeyToIdxMap[trKey+"."+key] = idx
78+
}
79+
l.idxToMsgMap[idx] = val.(string)
80+
}
81+
default:
82+
return fmt.Errorf("unsupported value type %T for key %q", v, trKey)
7183
}
72-
l.idxToMsgMap[idx] = key.Value()
7384
}
85+
return nil
7486
}
7587

88+
if err := addFunc(source); err != nil {
89+
return fmt.Errorf("unable to load json: %w", err)
90+
}
91+
if err := addFunc(moreSource); err != nil {
92+
return fmt.Errorf("unable to load json: %w", err)
93+
}
7694
return nil
7795
}
7896

modules/translation/translation.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func InitLocales(ctx context.Context) {
7474

7575
localeData := make(map[string][]byte, len(localeNames))
7676
for _, name := range localeNames {
77+
if !strings.HasPrefix(name, "locale_") || !strings.HasSuffix(name, ".json") {
78+
continue
79+
}
7780
localeData[name], err = options.Locale(name)
7881
if err != nil {
7982
log.Fatal("Failed to load %s locale file. %v", name, err)
@@ -90,14 +93,14 @@ func InitLocales(ctx context.Context) {
9093
var localeDataBase []byte
9194
if i == 0 && setting.Langs[0] != "en-US" {
9295
// Only en-US has complete translations. When use other language as default, the en-US should still be used as fallback.
93-
localeDataBase = localeData["locale_en-US.ini"]
96+
localeDataBase = localeData["locale_en-US.json"]
9497
if localeDataBase == nil {
95-
log.Fatal("Failed to load locale_en-US.ini file.")
98+
log.Fatal("Failed to load locale_en-US.json file.")
9699
}
97100
}
98101

99-
key := "locale_" + setting.Langs[i] + ".ini"
100-
if err = i18n.DefaultLocales.AddLocaleByIni(setting.Langs[i], setting.Names[i], localeDataBase, localeData[key]); err != nil {
102+
key := "locale_" + setting.Langs[i] + ".json"
103+
if err = i18n.DefaultLocales.AddLocaleByJSON(setting.Langs[i], setting.Names[i], localeDataBase, localeData[key]); err != nil {
101104
log.Error("Failed to set messages to %s: %v", setting.Langs[i], err)
102105
}
103106
}

0 commit comments

Comments
 (0)