Files
4566704 2b7de69403 refactor(logger): 日志改用 logrus 全局包级调用,移除 Logger 注入接口
为什么:并入宿主项目后须与宿主日志体系一致——宿主统一用 pkg/logger 初始化
logrus 全局实例并直接 logrus.Xxx 包级调用;包一层 Logger 接口会让 logrus 的
caller 定位(报告调用文件:行号)失准。

改动:
- 删除 logger 包(Logger 接口 / Default / SetDefault,默认本就是 logrus.StandardLogger)
- 11 个结构体移除 lgr logger.Logger 字段、SetLogger 方法及构造中的 lgr 初始化
- 192 处 lgr.Xxxf 调用改为 logrus.Xxxf,日志消息文本保持不变
- xiaomi.Client 移除 Logger() 访问器,air_conditioner 回读日志去掉判空调用直连 logrus
- miot_i18n.go 两处标准库 log.Printf 改为 logrus.Errorf,统一日志出口
- go.mod:logrus 从 indirect 提升为直接依赖
- goimports/goformat 全树规范化:此前部分文件未 gofmt(单行 if、对齐),本次顺带
  格式化,纯空白/换行变化,无逻辑改动

验证:go build / go vet / go test ./... 全部通过(bridge、miot、xiaomi、devices、specs)
2026-09-15 20:16:26 +08:00

86 lines
2.1 KiB
Go

// Package miot provides MIoT core client for Xiaomi Home devices.
// This file is a direct port of py-miot/miot_i18n.py.
package miot
import (
"strings"
"github.com/sirupsen/logrus"
)
// MIoTI18n provides internationalization translation.
// Translate by Copilot, which does not guarantee the accuracy of the
// translation. If there is a problem with the translation, please submit
// the ISSUE feedback. After the review, we will modify it as soon as possible.
type MIoTI18n struct {
lang string
data map[string]interface{}
}
// NewMIoTI18n creates a new MIoTI18n instance.
func NewMIoTI18n(lang string) *MIoTI18n {
return &MIoTI18n{
lang: lang,
data: make(map[string]interface{}),
}
}
// Init loads the i18n JSON file for the configured language.
func (i *MIoTI18n) Init() {
if len(i.data) > 0 {
return
}
i.data = make(map[string]interface{})
data, err := LoadJSONFile(GenAbsolutePath("i18n/" + i.lang + ".json"))
if err != nil {
logrus.Errorf("load i18n file error: %v", err)
return
}
if data == nil {
logrus.Errorf("invalid i18n file: nil data")
return
}
i.data = data
}
// Deinit clears the loaded translation data.
func (i *MIoTI18n) Deinit() {
i.data = make(map[string]interface{})
}
// Translate looks up a translation by dot-separated key.
// It traverses nested JSON objects, e.g. "device.light.name"
// -> data["device"]["light"]["name"].
// If the key is not found, returns an empty string.
// Optional replace map performs placeholder substitution: {key} -> value.
func (i *MIoTI18n) Translate(key string, replace map[string]string) string {
result := i.lookup(strings.Split(key, "."))
s, ok := result.(string)
if !ok {
return ""
}
if replace != nil {
for k, v := range replace {
s = strings.ReplaceAll(s, "{"+k+"}", v)
}
}
return s
}
// lookup traverses the nested map using the given key segments.
func (i *MIoTI18n) lookup(keys []string) interface{} {
var cur interface{} = i.data
for _, k := range keys {
m, ok := cur.(map[string]interface{})
if !ok {
return nil
}
cur = m[k]
if cur == nil {
return nil
}
}
return cur
}