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

translate.go 2.9 KiB

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package utils
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/utils/cache"
  5. "encoding/json"
  6. "github.com/gin-gonic/gin"
  7. "github.com/go-creed/sat"
  8. "strings"
  9. )
  10. func ReadReverse(c *gin.Context, str string) string {
  11. if c.GetString("translate_open") == "zh_Hant_" { //繁体先不改
  12. sat.InitDefaultDict(sat.SetPath(cfg.WxappletFilepath.URL + "/" + "sat.txt")) //使用自定义词库
  13. sat := sat.DefaultDict()
  14. res := sat.ReadReverse(str)
  15. list := strings.Split(res, "http")
  16. imgList := []string{".png", ".jpg", ".jpeg", ".gif"}
  17. for _, v := range list {
  18. for _, v1 := range imgList {
  19. if strings.Contains(v, v1) { //判断是不是有图片 有图片就截取 替换简繁体
  20. strs := strings.Split(v, v1)
  21. if len(strs) > 0 {
  22. oldStr := strs[0]
  23. newStr := sat.Read(oldStr)
  24. res = strings.ReplaceAll(res, oldStr, newStr)
  25. }
  26. }
  27. }
  28. }
  29. return res
  30. }
  31. if c.GetString("translate_open") != "zh_Hant_" { //除了繁体,其他都走这里
  32. //简体---其他语言
  33. cTouString, err := cache.GetString("multi_language_c_to_" + c.GetString("translate_open"))
  34. if err != nil {
  35. return str
  36. }
  37. var cTou = make(map[string]string)
  38. json.Unmarshal([]byte(cTouString), &cTou)
  39. if len(cTou) == 0 {
  40. return str
  41. }
  42. //其他语言--简体
  43. getString1, err1 := cache.GetString("multi_language_" + c.GetString("translate_open") + "_to_c")
  44. if err1 != nil {
  45. return str
  46. }
  47. var uToc = make(map[string]string)
  48. json.Unmarshal([]byte(getString1), &uToc)
  49. if len(uToc) == 0 {
  50. return str
  51. }
  52. res := str
  53. for k, v := range cTou {
  54. res = strings.ReplaceAll(res, k, v)
  55. }
  56. list := strings.Split(res, "http")
  57. imgList := []string{".png", ".jpg", ".jpeg", ".gif"}
  58. for _, v := range list {
  59. for _, v1 := range imgList {
  60. if strings.Contains(v, v1) { //判断是不是有图片 有图片就截取 替换简繁体
  61. strs := strings.Split(v, v1)
  62. if len(strs) > 0 {
  63. oldStr := strs[0]
  64. newStr := oldStr
  65. for k2, v2 := range uToc {
  66. newStr = strings.ReplaceAll(oldStr, k2, v2)
  67. }
  68. res = strings.ReplaceAll(res, oldStr, newStr)
  69. }
  70. }
  71. }
  72. }
  73. return res
  74. }
  75. return str
  76. }
  77. func ReadReverse1(str, types string) string {
  78. res := map[string]map[string]string{}
  79. err := cache.GetJson("multi_language", &res)
  80. if err != nil {
  81. return str
  82. }
  83. for k, v := range res {
  84. str = strings.ReplaceAll(str, k, v[types])
  85. }
  86. resStr := str
  87. list := strings.Split(resStr, "http")
  88. imgList := []string{".png", ".jpg", ".jpeg", ".gif"}
  89. for _, v := range list {
  90. for _, v1 := range imgList {
  91. if strings.Contains(v, v1) { //判断是不是有图片 有图片就截取 替换简繁体
  92. strs := strings.Split(v, v1)
  93. if len(strs) > 0 {
  94. oldStr := strs[0]
  95. for k2, v2 := range res {
  96. if v2[types] == oldStr {
  97. resStr = strings.ReplaceAll(resStr, oldStr, k2)
  98. }
  99. }
  100. //res = strings.ReplaceAll(res, oldStr, newStr)
  101. }
  102. }
  103. }
  104. }
  105. return resStr
  106. }