我如何从主 Bundle 进入 UITest 目标本地化?
func localizedText(_ key: String) -> String {
return Bundle.main.localizedString(forKey: key, value: nil, table: nil)
}
我尝试访问 Bundle.main 但没有本地化字符串。而且我似乎无法导入应用程序的主要目标来执行 Bundle(for: ClassName.self).localized...
有什么提示吗?
最佳答案
要在 UITest 中使用本地化字符串,您必须做两件事:
1) 将您的 Localizable.strings 文件添加到您的 UITest 目标
2) 通过 UITest 包访问字符串:
func localizedText(_ key: String) -> String {
let uiTestBundle = Bundle(for: AClassFromYourUITests.self)
return NSLocalizedString(key, bundle: uiTestBundle, comment: "")
}
只要确保您使用的类是您的 UITest 目标的一部分即可访问该包。
使用 Bundle.main
在运行 UITests 时不起作用,因为它为您提供了 UITest Runner App 的 bundle 而不是 UITest bundle
关于ios - 从主包中获取本地化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41700302/