iOS UITests NSLocalizedString 不工作

标签 ios swift localization xcode-ui-testing iqkeyboardmanager

我正在尝试本地化我的 UITests 以使用其他语言(目前使用 Snapshot 来自动截屏,所以这就是我需要它工作的原因)。

我现在的主要问题是 IQKeyboardManager 中的“完成”按钮.

在英语中,我有以下代码并且工作正常:

app.toolbars.buttons["Done"].tap()

输入文本后点击完成按钮。

在西类牙语中,该按钮称为“确定”。看起来它是从一些默认的 UIKit 本地化字符串或类似的东西中获取的。

我尝试在我的 UITest es.lproj 文件夹中添加一个 .strings 文件,并将 "UIBarButtonSystemItem.Done"= "OK"; 放入其中。

我也改成了:

app.toolbars.buttons[NSLocalizedString("UIBarButtonSystemItem.Done", bundle: Bundle.main, value: "Done", comment: "")].tap()

但这没有用。始终使用“完成”。

总是报错:

No matches found for "Done" Button.

我也试过:

app.toolbars.buttons[NSLocalizedString("UIBarButtonSystemItem.Done", comment: "")].tap()

这导致了一个错误:

No matches found for "UIBarButtonSystemItem.Done" Button.

看来我的 .strings 文件不适用于我的 UITest。关于如何让它发挥作用的任何想法?

最佳答案

我创建了这个支持英语和西类牙语的小项目。通过在方案的配置中切换,也可以使用两种不同的语言运行测试

enter image description here

这就是测试的构建方式

func testExample() {
    let greeting = localizedString(key: "Greetings.Hello")
    XCTAssert(XCUIApplication().staticTexts[greeting].exists, "Expected \"\(greeting)\" label to exist")
}

它使用以下函数来获取翻译

func localizedString(key:String) -> String {
    let bundle = Bundle(for: LocalizationUITests.self)
    let deviceLanguage = Locale.preferredLanguages[0]
    let localizationBundle = Bundle(path: bundle.path(forResource: deviceLanguage, ofType: "lproj")!)
    let result = NSLocalizedString(key, bundle:localizationBundle!, comment: "") //
    return result
}

这是您可以看到它工作的项目:https://github.com/TitouanVanBelle/LocalizationUITests

关于iOS UITests NSLocalizedString 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44959298/

相关文章:

Java 星期几,每个语言环境有 2 个字母

asp.net - 需要有关 ASP.NET 本地化的建议 - 字符串存储在数据库中并显示在 GridView 中

objective-c - iOS 应用程序显示最后显示的 View 而不是初始屏幕 - 但只是有时

iphone - 将 COCOnut 添加到 XCode 中的现有项目

json - 从不同端点下载特定数据的 JSON

iphone - 新的 Swift 应用程序列表器错误 : "No matching provisioning profiles found", "App ID with Identifier ... is not available"

c# - 在 Windows 上获取不同语言的本地化日期时间字符串和时区名称

iOS系统图标

ios - 在Air iOS中,.fla文件夹上方包含的文件夹的路径是什么?

ios - 当 inputAccessoryView 出现时,将 Collection View 聊天向上移动