mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-04 13:20:19 +08:00
feat: refactor backend i18n (#1373)
* fix: handle the dataSourceName when DB changes * reduce duplication of code * feat: refactor translation error message * feat: use json intsead of ini file * remove useless translation * fix translate problems * remove useless addition * fix pr problems * fix pr problems * fix split problem * use gofumpt to fmt code * use crowdin to execute backend translation * fix pr problems * refactor: change translation file structure same as frontend * delete useless output * update go.mod
This commit is contained in:
@ -15,13 +15,11 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/Unknwon/goconfig"
|
||||
"github.com/casdoor/casdoor/util"
|
||||
)
|
||||
|
||||
@ -43,7 +41,7 @@ func GetAllI18nStrings(fileContent string, path string) []string {
|
||||
return res
|
||||
}
|
||||
for _, match := range matches {
|
||||
match := strings.Split(match[1], ",")
|
||||
match := strings.SplitN(match[1], ",", 2)
|
||||
res = append(res, match[1][2:])
|
||||
}
|
||||
} else {
|
||||
@ -84,39 +82,34 @@ func getAllGoFilePaths() []string {
|
||||
return res
|
||||
}
|
||||
|
||||
func getErrName(paths []string) map[string]bool {
|
||||
ErrName := make(map[string]bool)
|
||||
func getErrName(paths []string) map[string]string {
|
||||
ErrName := make(map[string]string)
|
||||
for i := 0; i < len(paths); i++ {
|
||||
content := util.ReadStringFromPath(paths[i])
|
||||
words := GetAllI18nStrings(content, paths[i])
|
||||
for i := 0; i < len(words); i++ {
|
||||
ErrName[words[i]] = true
|
||||
for j := 0; j < len(words); j++ {
|
||||
ErrName[words[j]] = paths[i]
|
||||
}
|
||||
}
|
||||
return ErrName
|
||||
}
|
||||
|
||||
func writeToAllLanguageFiles(errName map[string]bool) {
|
||||
languages := "en,zh,es,fr,de,ja,ko,ru"
|
||||
languageArr := strings.Split(languages, ",")
|
||||
var c [10]*goconfig.ConfigFile
|
||||
for i := 0; i < len(languageArr); i++ {
|
||||
var err error
|
||||
c[i], err = goconfig.LoadConfigFile("../i18n/languages/" + "locale_" + languageArr[i] + ".ini")
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
func getI18nJSONData(errName map[string]string) *I18nData {
|
||||
data := I18nData{}
|
||||
for k, v := range errName {
|
||||
var index int
|
||||
if strings.Contains(v, "/") {
|
||||
index = strings.LastIndex(v, "/")
|
||||
} else {
|
||||
index = strings.LastIndex(v, "\\")
|
||||
}
|
||||
for j := range errName {
|
||||
parts := strings.Split(j, ".")
|
||||
_, err := c[i].GetValue(parts[0], parts[1])
|
||||
if err != nil {
|
||||
c[i].SetValue(parts[0], parts[1], parts[1])
|
||||
}
|
||||
}
|
||||
c[i].SetPrettyFormat(true)
|
||||
err = goconfig.SaveConfigFile(c[i], "../i18n/languages/"+"locale_"+languageArr[i]+".ini")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
namespace := v[index+1 : len(v)-3]
|
||||
key := k[len(namespace)+1:]
|
||||
// fmt.Printf("k=%s,v=%s,namespace=%s,key=%s\n", k, v, namespace, key)
|
||||
if _, ok := data[namespace]; !ok {
|
||||
data[namespace] = map[string]string{}
|
||||
}
|
||||
data[namespace][key] = key
|
||||
}
|
||||
return &data
|
||||
}
|
||||
|
Reference in New Issue
Block a user