|
| 1 | +package lang |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "errors" |
| 6 | + "github.com/flyhope/kubetea/comm" |
| 7 | + "github.com/nicksnyder/go-i18n/v2/i18n" |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | + "golang.org/x/text/language" |
| 10 | + "gopkg.in/yaml.v3" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | + "sync" |
| 15 | +) |
| 16 | + |
| 17 | +//go:embed active.*.yaml |
| 18 | +var localeFS embed.FS |
| 19 | + |
| 20 | +// init bundle i18n function |
| 21 | +var bundle = sync.OnceValue(func() *i18n.Bundle { |
| 22 | + b := i18n.NewBundle(language.English) |
| 23 | + b.RegisterUnmarshalFunc("yaml", yaml.Unmarshal) |
| 24 | + |
| 25 | + // load code all language yaml file |
| 26 | + dir, err := localeFS.ReadDir(".") |
| 27 | + if err != nil { |
| 28 | + logrus.Fatal(err) |
| 29 | + } |
| 30 | + for _, file := range dir { |
| 31 | + _, err := b.LoadMessageFileFS(localeFS, file.Name()) |
| 32 | + if err != nil { |
| 33 | + logrus.WithFields(logrus.Fields{"file": file.Name()}).Fatal(err) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // scan dir, load user all language yaml file (active.*.yaml) |
| 38 | + userLangDir := filepath.Dir(comm.ShowConfigFilePath()) + "/lang" |
| 39 | + if _, err := os.Stat(userLangDir); err == nil { |
| 40 | + files, err := os.ReadDir(userLangDir) |
| 41 | + if err != nil { |
| 42 | + logrus.WithFields(logrus.Fields{"dir": userLangDir}).Fatal(err) |
| 43 | + } |
| 44 | + for _, file := range files { |
| 45 | + // filter active.*.yaml |
| 46 | + if !strings.HasPrefix(file.Name(), "active.") || !strings.HasSuffix(file.Name(), ".yaml") { |
| 47 | + continue |
| 48 | + } |
| 49 | + |
| 50 | + _, err := b.LoadMessageFile(userLangDir + "/" + file.Name()) |
| 51 | + if err != nil { |
| 52 | + logrus.WithFields(logrus.Fields{"file": file.Name()}).Fatal(err) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return b |
| 58 | +}) |
| 59 | + |
| 60 | +// DefaultLang 获取当前环境的语言 |
| 61 | +var DefaultLang = sync.OnceValue(func() string { |
| 62 | + lang := comm.ShowKubeteaConfig().Language |
| 63 | + if lang != "" && lang != "auto" { |
| 64 | + return lang |
| 65 | + } |
| 66 | + |
| 67 | + // 获取 LANG 环境变量 |
| 68 | + lang = os.Getenv("LANG") |
| 69 | + |
| 70 | + // 如果 LANG 为空,尝试获取 LC_ALL |
| 71 | + if lang == "" { |
| 72 | + lang = os.Getenv("LC_ALL") |
| 73 | + } |
| 74 | + |
| 75 | + // 如果仍然为空,返回默认值 |
| 76 | + if lang == "" { |
| 77 | + return "" |
| 78 | + } |
| 79 | + |
| 80 | + // 移除编码后缀(如 ".UTF-8") |
| 81 | + if idx := strings.Index(lang, "."); idx != -1 { |
| 82 | + lang = lang[:idx] |
| 83 | + } |
| 84 | + |
| 85 | + // 将下划线替换为连字符(RFC 2616格式) |
| 86 | + lang = strings.Replace(lang, "_", "-", -1) |
| 87 | + |
| 88 | + return lang |
| 89 | +}) |
| 90 | + |
| 91 | +// Txt Render text with i18n |
| 92 | +func Txt(lc *i18n.LocalizeConfig) string { |
| 93 | + localizer := i18n.NewLocalizer(bundle(), DefaultLang()) |
| 94 | + str, err := localizer.Localize(lc) |
| 95 | + |
| 96 | + var messageNotFoundErr *i18n.MessageNotFoundErr |
| 97 | + if errors.As(err, &messageNotFoundErr) { |
| 98 | + localizer = i18n.NewLocalizer(bundle(), "en") |
| 99 | + str, err = localizer.Localize(lc) |
| 100 | + } |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + str = lc.DefaultMessage.ID |
| 104 | + logrus.WithFields(logrus.Fields{"id": lc.MessageID}).Errorln(err) |
| 105 | + } |
| 106 | + return str |
| 107 | +} |
| 108 | + |
| 109 | +// Map lang data |
| 110 | +type Map map[string]any |
| 111 | + |
| 112 | +// Data Render text with i18n (not multi number) |
| 113 | +func Data(defaultMesage *i18n.Message, data Map) string { |
| 114 | + return Txt(&i18n.LocalizeConfig{ |
| 115 | + DefaultMessage: defaultMesage, |
| 116 | + TemplateData: data, |
| 117 | + }) |
| 118 | +} |
0 commit comments