refactor: 删除冗余常量文件并整合常量定义
This commit is contained in:
@@ -5,110 +5,54 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
systemService "be.ems/src/modules/system/service"
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/database/redis"
|
||||
)
|
||||
|
||||
// localeItem 国际化数据项
|
||||
type localeItem struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
// localeMap 国际化数据组
|
||||
var localeMap = make(map[string][]localeItem)
|
||||
|
||||
// ClearLocaleData 清空国际化数据
|
||||
func ClearLocaleData() {
|
||||
localeMap = make(map[string][]localeItem)
|
||||
}
|
||||
|
||||
// LoadLocaleData 加载国际化数据
|
||||
func LoadLocaleData(language string) []localeItem {
|
||||
dictType := fmt.Sprintf("i18n_%s", language)
|
||||
dictTypeList := systemService.NewSysDictType.DictDataCache(dictType)
|
||||
localeData := []localeItem{}
|
||||
for _, v := range dictTypeList {
|
||||
localeData = append(localeData, localeItem{
|
||||
Key: v.DictLabel,
|
||||
Value: v.DictValue,
|
||||
Code: v.DictCode,
|
||||
})
|
||||
}
|
||||
localeMap[language] = localeData
|
||||
return localeData
|
||||
}
|
||||
|
||||
// UpdateKeyValue 更新键对应的值
|
||||
func UpdateKeyValue(language, key, value string) bool {
|
||||
arr, ok := localeMap[language]
|
||||
if !ok || len(arr) == 0 {
|
||||
arr = LoadLocaleData(language)
|
||||
}
|
||||
|
||||
code := ""
|
||||
if key == "" {
|
||||
return false
|
||||
}
|
||||
for _, v := range arr {
|
||||
if v.Key == key {
|
||||
code = v.Code
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 更新字典数据
|
||||
sysDictDataService := systemService.NewSysDictData
|
||||
item := sysDictDataService.SelectDictDataByCode(code)
|
||||
if item.DictCode == code && item.DictLabel == key {
|
||||
item.DictValue = value
|
||||
row := sysDictDataService.UpdateDictData(item)
|
||||
if row > 0 {
|
||||
delete(localeMap, language)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TFindKeyPrefix 翻译值查找键 值前缀匹配
|
||||
func TFindKeyPrefix(language, keyPrefix, value string) string {
|
||||
key := value
|
||||
if value == "" {
|
||||
if key == "" {
|
||||
return key
|
||||
}
|
||||
arr, ok := localeMap[language]
|
||||
if !ok || len(arr) == 0 {
|
||||
arr = LoadLocaleData(language)
|
||||
|
||||
langKey := constants.CACHE_I18N + ":" + keyPrefix + "*"
|
||||
prefixKeys, err := redis.GetKeys("", langKey)
|
||||
if err != nil {
|
||||
return key
|
||||
}
|
||||
mkv, err := redis.GetHashBatch("", prefixKeys)
|
||||
if err != nil {
|
||||
return key
|
||||
}
|
||||
|
||||
for _, v := range arr {
|
||||
if strings.HasPrefix(v.Key, keyPrefix) && strings.HasPrefix(v.Value, value) {
|
||||
key = v.Key
|
||||
break
|
||||
for k, m := range mkv {
|
||||
// 跳过-号数据 i18n:menu.system.menu
|
||||
|
||||
if v, ok := m[language]; ok {
|
||||
if strings.HasPrefix(v, value) {
|
||||
key = k[len(constants.CACHE_I18N)+1:]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
// TKey 翻译键
|
||||
// language: zh-中文 en-英文
|
||||
func TKey(language, key string) string {
|
||||
value := key
|
||||
if key == "" {
|
||||
return value
|
||||
}
|
||||
arr, ok := localeMap[language]
|
||||
if !ok || len(arr) == 0 {
|
||||
arr = LoadLocaleData(language)
|
||||
}
|
||||
|
||||
for _, v := range arr {
|
||||
if v.Key == key {
|
||||
value = v.Value
|
||||
break
|
||||
}
|
||||
langKey := constants.CACHE_I18N + ":" + key
|
||||
output, err := redis.GetHash("", langKey, language)
|
||||
if err != nil {
|
||||
return value
|
||||
}
|
||||
return value
|
||||
return output
|
||||
}
|
||||
|
||||
// TTemplate 翻译模板字符串
|
||||
|
||||
Reference in New Issue
Block a user