工具包
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.

110 lines
2.9 KiB

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