You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate.go 1.5 kB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package i18n
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "regexp"
  7. "strings"
  8. "github.com/casbin/casbase/util"
  9. )
  10. type I18nData map[string]map[string]string
  11. var reI18n *regexp.Regexp
  12. func init() {
  13. reI18n, _ = regexp.Compile("i18next.t\\(\"(.*)\"\\)")
  14. }
  15. func getAllI18nStrings(fileContent string) []string {
  16. res := []string{}
  17. matches := reI18n.FindAllStringSubmatch(fileContent, -1)
  18. if matches == nil {
  19. return res
  20. }
  21. for _, match := range matches {
  22. res = append(res, match[1])
  23. }
  24. return res
  25. }
  26. func getAllJsFilePaths() []string {
  27. path := "../web/src"
  28. res := []string{}
  29. err := filepath.Walk(path,
  30. func(path string, info os.FileInfo, err error) error {
  31. if err != nil {
  32. return err
  33. }
  34. if !strings.HasSuffix(info.Name(), ".js") {
  35. return nil
  36. }
  37. res = append(res, path)
  38. fmt.Println(path, info.Name())
  39. return nil
  40. })
  41. if err != nil {
  42. panic(err)
  43. }
  44. return res
  45. }
  46. func parseToData() *I18nData {
  47. allWords := []string{}
  48. paths := getAllJsFilePaths()
  49. for _, path := range paths {
  50. fileContent := util.ReadStringFromPath(path)
  51. words := getAllI18nStrings(fileContent)
  52. allWords = append(allWords, words...)
  53. }
  54. fmt.Printf("%v\n", allWords)
  55. data := I18nData{}
  56. for _, word := range allWords {
  57. tokens := strings.Split(word, ":")
  58. namespace := tokens[0]
  59. key := tokens[1]
  60. if _, ok := data[namespace]; !ok {
  61. data[namespace] = map[string]string{}
  62. }
  63. data[namespace][key] = key
  64. }
  65. return &data
  66. }

基于Casbin的开源AI领域知识库平台

Contributors (1)