Convert locale files from ini to json format (#35489)

Migrate from the current INI format to JSON for translations. JSON is
widely supported, including by platforms such as Crowdin and Weblate.
This commit is contained in:
Lunny Xiao
2025-12-19 09:50:48 -08:00
committed by GitHub
parent 9764ae87d2
commit ffea9a27c3
86 changed files with 78280 additions and 83164 deletions
+40 -22
View File
@@ -10,8 +10,8 @@ import (
"html/template"
"slices"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)
// This file implements the static LocaleStore that will not watch for changes
@@ -40,8 +40,8 @@ func NewLocaleStore() LocaleStore {
return &localeStore{localeMap: make(map[string]*locale), trKeyToIdxMap: make(map[string]int)}
}
// AddLocaleByIni adds locale by ini into the store
func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error {
// AddLocaleByJSON adds locale by JSON into the store
func (store *localeStore) AddLocaleByJSON(langName, langDesc string, source, moreSource []byte) error {
if _, ok := store.localeMap[langName]; ok {
return errors.New("lang has already been added")
}
@@ -52,28 +52,46 @@ func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, more
l := &locale{store: store, langName: langName, idxToMsgMap: make(map[int]string)}
store.localeMap[l.langName] = l
iniFile, err := setting.NewConfigProviderForLocale(source, moreSource)
if err != nil {
return fmt.Errorf("unable to load ini: %w", err)
}
for _, section := range iniFile.Sections() {
for _, key := range section.Keys() {
var trKey string
if section.Name() == "" || section.Name() == "DEFAULT" {
trKey = key.Name()
} else {
trKey = section.Name() + "." + key.Name()
}
idx, ok := store.trKeyToIdxMap[trKey]
if !ok {
idx = len(store.trKeyToIdxMap)
store.trKeyToIdxMap[trKey] = idx
}
l.idxToMsgMap[idx] = key.Value()
addFunc := func(source []byte) error {
if len(source) == 0 {
return nil
}
values := make(map[string]any)
if err := json.Unmarshal(source, &values); err != nil {
return fmt.Errorf("unable to load json: %w", err)
}
for trKey, value := range values {
switch v := value.(type) {
case string:
idx, ok := store.trKeyToIdxMap[trKey]
if !ok {
idx = len(store.trKeyToIdxMap)
store.trKeyToIdxMap[trKey] = idx
}
l.idxToMsgMap[idx] = v
case map[string]any:
for key, val := range v {
idx, ok := store.trKeyToIdxMap[trKey+"."+key]
if !ok {
idx = len(store.trKeyToIdxMap)
store.trKeyToIdxMap[trKey+"."+key] = idx
}
l.idxToMsgMap[idx] = val.(string)
}
default:
return fmt.Errorf("unsupported value type %T for key %q", v, trKey)
}
}
return nil
}
if err := addFunc(source); err != nil {
return fmt.Errorf("unable to load json: %w", err)
}
if err := addFunc(moreSource); err != nil {
return fmt.Errorf("unable to load json: %w", err)
}
return nil
}