lang.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package lang
  2. import (
  3. _ "embed"
  4. "gopkg.in/yaml.v3"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "runtime"
  9. )
  10. //go:embed zh.yaml
  11. var zhYaml []byte
  12. var zh map[string]string
  13. func init() {
  14. yaml.Unmarshal(zhYaml, &zh)
  15. }
  16. func Get(lang string) map[string]string {
  17. if lang == "zh" {
  18. if runtime.GOOS == "linux" && !IsTerminalSupportChinese() {
  19. return nil
  20. }
  21. return zh
  22. }
  23. return nil
  24. }
  25. func Update(lang string, key string, value string) {
  26. if lang == "zh" {
  27. zh[key] = value
  28. }
  29. }
  30. func Merge(lang string, data map[string]string) {
  31. if lang == "zh" {
  32. for k, v := range data {
  33. zh[k] = v
  34. }
  35. }
  36. }
  37. func IsTerminalSupportChinese() bool {
  38. // 获取终端的环境变量
  39. env := os.Environ()
  40. // 查找 LANG 环境变量
  41. isSupportUTF8 := false
  42. for _, v := range env {
  43. if strings.Index(v, "LANG") != -1 && strings.Index(v, "UTF-8") != -1 {
  44. isSupportUTF8 = true
  45. }
  46. }
  47. if isSupportUTF8 {
  48. // 在终端中打印中文字符
  49. cmd := exec.Command("echo", "你好!")
  50. _, err := cmd.CombinedOutput()
  51. if err == nil {
  52. return true
  53. }
  54. return false
  55. }
  56. return false
  57. }