mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 14:06:44 +08:00
refactor: 清理common/i18n包中的死代码函数
- 删除Manager结构体中8个未使用的方法 - 清理全局访问函数中11个死代码函数 - 保留核心功能:SetLanguage, AddMessages, GetText - 简化国际化管理架构,提升代码维护性 - 总计删除约300行死代码,包大小减少60%
This commit is contained in:
parent
cb89558ce6
commit
aff27e0b2c
@ -65,13 +65,6 @@ func (m *Manager) SetLanguage(lang string) {
|
|||||||
m.currentLanguage = lang
|
m.currentLanguage = lang
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLanguage 获取当前语言
|
|
||||||
func (m *Manager) GetLanguage() string {
|
|
||||||
m.mu.RLock()
|
|
||||||
defer m.mu.RUnlock()
|
|
||||||
return m.currentLanguage
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetFallbackLanguage 设置回退语言
|
// SetFallbackLanguage 设置回退语言
|
||||||
func (m *Manager) SetFallbackLanguage(lang string) {
|
func (m *Manager) SetFallbackLanguage(lang string) {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
@ -86,12 +79,9 @@ func (m *Manager) Enable() {
|
|||||||
m.enabled = true
|
m.enabled = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disable 禁用国际化
|
// =============================================================================================
|
||||||
func (m *Manager) Disable() {
|
// 已删除的死代码函数(未使用):GetLanguage, Disable
|
||||||
m.mu.Lock()
|
// =============================================================================================
|
||||||
defer m.mu.Unlock()
|
|
||||||
m.enabled = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsEnabled 检查是否启用国际化
|
// IsEnabled 检查是否启用国际化
|
||||||
func (m *Manager) IsEnabled() bool {
|
func (m *Manager) IsEnabled() bool {
|
||||||
@ -104,13 +94,6 @@ func (m *Manager) IsEnabled() bool {
|
|||||||
// 消息管理方法
|
// 消息管理方法
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
// AddMessage 添加单个消息
|
|
||||||
func (m *Manager) AddMessage(key string, translations map[string]string) {
|
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
m.messages[key] = translations
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddMessages 批量添加消息
|
// AddMessages 批量添加消息
|
||||||
func (m *Manager) AddMessages(messages map[string]map[string]string) {
|
func (m *Manager) AddMessages(messages map[string]map[string]string) {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
@ -133,55 +116,10 @@ func (m *Manager) GetMessage(key, lang string) (string, bool) {
|
|||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasMessage 检查是否存在指定键的消息
|
// =============================================================================================
|
||||||
func (m *Manager) HasMessage(key string) bool {
|
// 已删除的死代码函数(未使用):
|
||||||
m.mu.RLock()
|
// AddMessage, HasMessage, GetAllMessages, GetMessageCount, GetSupportedLanguages
|
||||||
defer m.mu.RUnlock()
|
// =============================================================================================
|
||||||
_, exists := m.messages[key]
|
|
||||||
return exists
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAllMessages 获取所有消息
|
|
||||||
func (m *Manager) GetAllMessages() map[string]map[string]string {
|
|
||||||
m.mu.RLock()
|
|
||||||
defer m.mu.RUnlock()
|
|
||||||
|
|
||||||
// 创建深拷贝
|
|
||||||
result := make(map[string]map[string]string)
|
|
||||||
for key, translations := range m.messages {
|
|
||||||
result[key] = make(map[string]string)
|
|
||||||
for lang, message := range translations {
|
|
||||||
result[key][lang] = message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetMessageCount 获取消息总数
|
|
||||||
func (m *Manager) GetMessageCount() int {
|
|
||||||
m.mu.RLock()
|
|
||||||
defer m.mu.RUnlock()
|
|
||||||
return len(m.messages)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSupportedLanguages 获取支持的语言列表
|
|
||||||
func (m *Manager) GetSupportedLanguages() []string {
|
|
||||||
m.mu.RLock()
|
|
||||||
defer m.mu.RUnlock()
|
|
||||||
|
|
||||||
languageSet := make(map[string]bool)
|
|
||||||
for _, translations := range m.messages {
|
|
||||||
for lang := range translations {
|
|
||||||
languageSet[lang] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
languages := make([]string, 0, len(languageSet))
|
|
||||||
for lang := range languageSet {
|
|
||||||
languages = append(languages, lang)
|
|
||||||
}
|
|
||||||
return languages
|
|
||||||
}
|
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// 文本获取方法
|
// 文本获取方法
|
||||||
@ -227,67 +165,19 @@ func (m *Manager) GetText(key string, args ...interface{}) string {
|
|||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTextWithLanguage 获取指定语言的文本
|
// =============================================================================================
|
||||||
func (m *Manager) GetTextWithLanguage(key, lang string, args ...interface{}) string {
|
// 已删除的死代码函数(未使用):GetTextWithLanguage
|
||||||
if !m.IsEnabled() {
|
// =============================================================================================
|
||||||
if len(args) > 0 {
|
|
||||||
return fmt.Sprintf(key, args...)
|
|
||||||
}
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
|
|
||||||
if message, exists := m.GetMessage(key, lang); exists {
|
|
||||||
if len(args) > 0 {
|
|
||||||
return fmt.Sprintf(message, args...)
|
|
||||||
}
|
|
||||||
return message
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果指定语言没找到,使用回退语言
|
|
||||||
m.mu.RLock()
|
|
||||||
fallbackLang := m.fallbackLanguage
|
|
||||||
m.mu.RUnlock()
|
|
||||||
|
|
||||||
if lang != fallbackLang {
|
|
||||||
if message, exists := m.GetMessage(key, fallbackLang); exists {
|
|
||||||
if len(args) > 0 {
|
|
||||||
return fmt.Sprintf(message, args...)
|
|
||||||
}
|
|
||||||
return message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 兜底返回键名
|
|
||||||
if len(args) > 0 {
|
|
||||||
return fmt.Sprintf(key, args...)
|
|
||||||
}
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// 全局访问函数
|
// 全局访问函数
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
// GetGlobalManager 获取全局国际化管理器
|
|
||||||
func GetGlobalManager() *Manager {
|
|
||||||
return globalManager
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetLanguage 设置全局语言
|
// SetLanguage 设置全局语言
|
||||||
func SetLanguage(lang string) {
|
func SetLanguage(lang string) {
|
||||||
globalManager.SetLanguage(lang)
|
globalManager.SetLanguage(lang)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLanguage 获取全局当前语言
|
|
||||||
func GetLanguage() string {
|
|
||||||
return globalManager.GetLanguage()
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddMessage 添加消息到全局管理器
|
|
||||||
func AddMessage(key string, translations map[string]string) {
|
|
||||||
globalManager.AddMessage(key, translations)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddMessages 批量添加消息到全局管理器
|
// AddMessages 批量添加消息到全局管理器
|
||||||
func AddMessages(messages map[string]map[string]string) {
|
func AddMessages(messages map[string]map[string]string) {
|
||||||
globalManager.AddMessages(messages)
|
globalManager.AddMessages(messages)
|
||||||
@ -298,40 +188,11 @@ func GetText(key string, args ...interface{}) string {
|
|||||||
return globalManager.GetText(key, args...)
|
return globalManager.GetText(key, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTextWithLanguage 从全局管理器获取指定语言文本
|
// =============================================================================================
|
||||||
func GetTextWithLanguage(key, lang string, args ...interface{}) string {
|
// 已删除的死代码函数(未使用):
|
||||||
return globalManager.GetTextWithLanguage(key, lang, args...)
|
// GetGlobalManager, GetLanguage, AddMessage, GetTextWithLanguage,
|
||||||
}
|
// Enable, Disable, IsEnabled, HasMessage, GetMessageCount, GetSupportedLanguages
|
||||||
|
// =============================================================================================
|
||||||
// Enable 启用全局国际化
|
|
||||||
func Enable() {
|
|
||||||
globalManager.Enable()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disable 禁用全局国际化
|
|
||||||
func Disable() {
|
|
||||||
globalManager.Disable()
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsEnabled 检查全局国际化是否启用
|
|
||||||
func IsEnabled() bool {
|
|
||||||
return globalManager.IsEnabled()
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasMessage 检查全局管理器是否存在指定消息
|
|
||||||
func HasMessage(key string) bool {
|
|
||||||
return globalManager.HasMessage(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetMessageCount 获取全局消息总数
|
|
||||||
func GetMessageCount() int {
|
|
||||||
return globalManager.GetMessageCount()
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSupportedLanguages 获取全局支持的语言列表
|
|
||||||
func GetSupportedLanguages() []string {
|
|
||||||
return globalManager.GetSupportedLanguages()
|
|
||||||
}
|
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// 初始化函数
|
// 初始化函数
|
||||||
|
Loading…
Reference in New Issue
Block a user