Xcode 7 UITests with localized UI

后端 未结 11 916
既然无缘
既然无缘 2020-12-07 17:38

In my App I\'m using NSLocalizedString to localize my app. Now I want to switch to UITests and habe Testcode like this:

[tabBarsQue         


        
11条回答
  •  时光说笑
    2020-12-07 17:51

    The answer of SeanR is great (+1), but there is a minor improvement:

    If you use base localization, then your Localizable.strings might not be localized in your base language. This is not necessary because the base language would be used in this case. If so, SeanR’s function localizedString would return „?“.

    The extended version below checks additionally for the base language, and returns the localized string in the base language:

    func localizedString(_ key: String) -> String {
        let testBundle = Bundle(for: ShopEasyUITests.self)
        guard let currentLanguage = currentLanguage else { return "?" }
        if let testBundlePath = testBundle.path(forResource: currentLanguage.localeCode, ofType: "lproj"),
            let localizedBundle = Bundle(path: testBundlePath) {
            return NSLocalizedString(key, bundle: localizedBundle, comment: "")
        }
        if let testBundlePath = testBundle.path(forResource: currentLanguage.langCode, ofType: "lproj"),
            let localizedBundle = Bundle(path: testBundlePath) {
            return NSLocalizedString(key, bundle: localizedBundle, comment: "")
        }
        if let testBundlePath = testBundle.path(forResource: "Base", ofType: "lproj"),
            let localizedBundle = Bundle(path: testBundlePath) {
            return NSLocalizedString(key, bundle: localizedBundle, comment: "")
        }
        return "?"
    }
    

提交回复
热议问题