You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import Foundation
// 退
func testLocalizationFallback() {
print("=== 测试本地化回退机制 ===")
// LanguageManagerlocalizedString
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()