import Foundation

// 模拟测试本地化回退机制
func testLocalizationFallback() {
    print("=== 测试本地化回退机制 ===")
    
    // 模拟LanguageManager的localizedString方法
    func localizedString(for key: String, language: String) -> String {
        let bundle = Bundle.main
        
        // 首先尝试从当前语言获取字符串
        if let path = bundle.path(forResource: language, ofType: "lproj"),
           let languageBundle = Bundle(path: path) {
            let localizedString = languageBundle.localizedString(forKey: key, value: nil, table: nil)
            
            // 如果返回的字符串不是key本身，说明找到了本地化条目
            if localizedString != key {
                print("✅ 在 \(language) 中找到 '\(key)': '\(localizedString)'")
                return localizedString
            } else {
                print("❌ 在 \(language) 中未找到 '\(key)'")
            }
        }
        
        // 如果当前语言没有找到条目，回退到英语
        if let path = bundle.path(forResource: "en", ofType: "lproj"),
           let languageBundle = Bundle(path: path) {
            let englishString = languageBundle.localizedString(forKey: key, value: nil, table: nil)
            
            // 如果英语也没有找到，返回key本身
            if englishString != key {
                print("🔄 回退到英语: '\(key)' -> '\(englishString)'")
                return englishString
            }
        }
        
        // 最后回退到key本身
        print("⚠️ 最终回退到key: '\(key)'")
        return key
    }
    
    // 测试用例
    let testKeys = ["settings", "scan", "create", "nonexistent_key"]
    let testLanguages = ["ja", "ko", "en"]
    
    for language in testLanguages {
        print("\n--- 测试语言: \(language) ---")
        for key in testKeys {
            let result = localizedString(for: key, language: language)
            print("结果: '\(key)' -> '\(result)'")
        }
    }
}

// 运行测试
testLocalizationFallback()
