我最终得到了这个解决方案:
在 UIViewController 中,我可以访问我的 UITextView 对象并强制使用 Storyboard.strings 中的本地化文本。
func fixTextViewStoryboardLocalization() {
guard storyboard != nil else {
return // No need to fix it if storyboard is not presented
}
let storyboardName = storyboard!.value(forKey: "name") as! String
for case let textView as UITextView in view.subviews {
if let ident = textView.restorationIdentifier {
textView.text = NSLocalizedString("\(ident).text", tableName: storyboardName, bundle: Bundle.main, value: "", comment: "")
}
}
}
创建一个自定义的 MyViewController(选择你想要的任何名称),在 viewDidLoad 中调用这个函数:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
fixTextViewStoryboardLocalization()
}
}
确保故事板视图控制器来自 MyViewController 类:
MyViewController custom class in Storyboard
为了使上面的代码正常工作,我需要为我的 Storyboard 中的每个 UITextView 设置恢复 ID:
UITextView Restoration ID
最后一步 - 在 Storyboard.strings 中本地化文本视图:
/* Class = "UITextView"; text = "Base text"; ObjectID = "MyTextView-Restoration-ID"; */
"MyTextView-Restoration-ID.text" = "Localized text comes here";
这适用于我所有故事板中的所有 UITextView。