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 2.1 kB

3 years ago
3 years ago
3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package i18n
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "regexp"
  20. "strings"
  21. "github.com/casibase/casibase/util"
  22. )
  23. type I18nData map[string]map[string]string
  24. var reI18n *regexp.Regexp
  25. func init() {
  26. reI18n, _ = regexp.Compile("i18next.t\\(\"(.*?)\"\\)")
  27. }
  28. func getAllI18nStrings(fileContent string) []string {
  29. res := []string{}
  30. matches := reI18n.FindAllStringSubmatch(fileContent, -1)
  31. if matches == nil {
  32. return res
  33. }
  34. for _, match := range matches {
  35. res = append(res, match[1])
  36. }
  37. return res
  38. }
  39. func getAllJsFilePaths() []string {
  40. path := "../web/src"
  41. res := []string{}
  42. err := filepath.Walk(path,
  43. func(path string, info os.FileInfo, err error) error {
  44. if err != nil {
  45. return err
  46. }
  47. if !strings.HasSuffix(info.Name(), ".js") {
  48. return nil
  49. }
  50. res = append(res, path)
  51. fmt.Println(path, info.Name())
  52. return nil
  53. })
  54. if err != nil {
  55. panic(err)
  56. }
  57. return res
  58. }
  59. func parseToData() *I18nData {
  60. allWords := []string{}
  61. paths := getAllJsFilePaths()
  62. for _, path := range paths {
  63. fileContent := util.ReadStringFromPath(path)
  64. words := getAllI18nStrings(fileContent)
  65. allWords = append(allWords, words...)
  66. }
  67. fmt.Printf("%v\n", allWords)
  68. data := I18nData{}
  69. for _, word := range allWords {
  70. tokens := strings.Split(word, ":")
  71. namespace := tokens[0]
  72. key := tokens[1]
  73. if _, ok := data[namespace]; !ok {
  74. data[namespace] = map[string]string{}
  75. }
  76. data[namespace][key] = key
  77. }
  78. return &data
  79. }