diff --git a/i18n/generate.go b/i18n/generate.go index 3388ef88..05933f9c 100644 --- a/i18n/generate.go +++ b/i18n/generate.go @@ -26,16 +26,22 @@ import ( type I18nData map[string]map[string]string -var reI18n *regexp.Regexp +var ( + reI18nFrontend *regexp.Regexp + reI18nBackendObject *regexp.Regexp + reI18nBackendController *regexp.Regexp +) func init() { - reI18n, _ = regexp.Compile("i18next.t\\(\"(.*?)\"\\)") + reI18nFrontend, _ = regexp.Compile("i18next.t\\(\"(.*?)\"\\)") + reI18nBackendObject, _ = regexp.Compile("i18n.Translate\\((.*?)\"\\)") + reI18nBackendController, _ = regexp.Compile("c.T\\((.*?)\"\\)") } -func getAllI18nStrings(fileContent string) []string { +func getAllI18nStringsFrontend(fileContent string) []string { res := []string{} - matches := reI18n.FindAllStringSubmatch(fileContent, -1) + matches := reI18nFrontend.FindAllStringSubmatch(fileContent, -1) if matches == nil { return res } @@ -46,6 +52,30 @@ func getAllI18nStrings(fileContent string) []string { return res } +func getAllI18nStringsBackend(fileContent string, isObjectPackage bool) []string { + res := []string{} + if isObjectPackage { + matches := reI18nBackendObject.FindAllStringSubmatch(fileContent, -1) + if matches == nil { + return res + } + for _, match := range matches { + match := strings.SplitN(match[1], ",", 2) + res = append(res, match[1][2:]) + } + } else { + matches := reI18nBackendController.FindAllStringSubmatch(fileContent, -1) + if matches == nil { + return res + } + for _, match := range matches { + res = append(res, match[1][1:]) + } + } + + return res +} + func getAllFilePathsInFolder(folder string, fileSuffix string) []string { res := []string{} err := filepath.Walk(folder, @@ -69,12 +99,25 @@ func getAllFilePathsInFolder(folder string, fileSuffix string) []string { return res } -func parseToData() *I18nData { +func parseEnData(category string) *I18nData { + var paths []string + if category == "backend" { + paths = getAllFilePathsInFolder("../", ".go") + } else { + paths = getAllFilePathsInFolder("../web/src", ".js") + } + allWords := []string{} - paths := getAllFilePathsInFolder("../web/src", ".js") for _, path := range paths { fileContent := util.ReadStringFromPath(path) - words := getAllI18nStrings(fileContent) + + var words []string + if category == "backend" { + isObjectPackage := strings.Contains(path, "object") + words = getAllI18nStringsBackend(fileContent, isObjectPackage) + } else { + words = getAllI18nStringsFrontend(fileContent) + } allWords = append(allWords, words...) } fmt.Printf("%v\n", allWords) diff --git a/i18n/generate_backend.go b/i18n/generate_backend.go deleted file mode 100644 index 8332f12e..00000000 --- a/i18n/generate_backend.go +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2022 The Casdoor Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package i18n - -import ( - "regexp" - "strings" - - "github.com/casdoor/casdoor/util" -) - -var ( - reI18nBackendObject *regexp.Regexp - re18nBackendController *regexp.Regexp -) - -func init() { - reI18nBackendObject, _ = regexp.Compile("i18n.Translate\\((.*?)\"\\)") - re18nBackendController, _ = regexp.Compile("c.T\\((.*?)\"\\)") -} - -func GetAllI18nStrings(fileContent string, path string) []string { - res := []string{} - if strings.Contains(path, "object") { - matches := reI18nBackendObject.FindAllStringSubmatch(fileContent, -1) - if matches == nil { - return res - } - for _, match := range matches { - match := strings.SplitN(match[1], ",", 2) - res = append(res, match[1][2:]) - } - } else { - matches := re18nBackendController.FindAllStringSubmatch(fileContent, -1) - if matches == nil { - return res - } - for _, match := range matches { - res = append(res, match[1][1:]) - } - } - - return res -} - -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 j := 0; j < len(words); j++ { - ErrName[words[j]] = paths[i] - } - } - return ErrName -} - -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, "\\") - } - 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 -} diff --git a/i18n/generate_test.go b/i18n/generate_test.go index 9a15f68a..f37b452b 100644 --- a/i18n/generate_test.go +++ b/i18n/generate_test.go @@ -26,8 +26,8 @@ func applyToOtherLanguage(category string, language string, i18nData *I18nData) writeI18nFile(category, language, i18nData) } -func TestGenerateI18nStringsForFrontend(t *testing.T) { - enData := parseToData() +func TestGenerateI18nFrontend(t *testing.T) { + enData := parseEnData("frontend") writeI18nFile("frontend", "en", enData) applyToOtherLanguage("frontend", "de", enData) @@ -39,13 +39,8 @@ func TestGenerateI18nStringsForFrontend(t *testing.T) { applyToOtherLanguage("frontend", "zh", enData) } -func TestGenerateI18nStringsForBackend(t *testing.T) { - paths := getAllFilePathsInFolder("../", ".go") - - errName := getErrName(paths) - - enData := getI18nJSONData(errName) - +func TestGenerateI18nBackend(t *testing.T) { + enData := parseEnData("backend") writeI18nFile("backend", "en", enData) applyToOtherLanguage("backend", "de", enData)