I have one more question based on my previous discussion thread of localized strings. Here is the link: How to do automated UI testing for system button on XCode7?
Now if I switch the iOS system language to another one, I can't get the localized string since it's in different bundle. I found another thread is talking about this: Can't get access to string localizations in UI Test (Xcode 7)
It works good if I copy all of localized strings from target app to UI tests. But for those strings in XIBs or storyboards they are using Object ID to represent a message, it's meaningless and not readable. How to make Object ID human-readable?
So I have to convert it first to know what it's originally before using NSLocalizedString:
NSLocalizedString("Help", bundle: NSBundle(forClass: AClassInYourUITests.self), comment: "")
Is there any easy way to resolve this issue within Xcode? If possible I wouldn't like to use these meaningless string in my code, it's difficult to know what it's. Or I have to write a script to handle this?
To be more precise, let's have an example. Some of my localized strings in storyboard of target app are:
"U2v-M1-HYu.text" = "ヘルプ";
"eTC-Zg-zHl.headerTitle" = "完了";
If I want to do the UI tests for different languages, I can't just copy it to the UI test bundle. I need to convert it first like this:
"Help" = "ヘルプ";
"Done" = "完了";
Any suggestions are welcome.
Open Info tab, and click “+” button under Localizations section. Then choose a language you want to support from the dropdown list shown. XCode opens a dialog showing resources to be added for the new language.
In your project settings, go to Build Settings and search for Use compiler to Extract Swift Strings. Make sure to search All settings, not just the Basic ones. Change the value of this setting to Yes. In Xcode, go to Product ▸ Export Localizations….
The key used to look up an entry in a strings file or strings dictionary file.
Use it in custom SwiftUI views to make them ready for localization. Enable the "Use Compiler to Extract Swift Strings" build setting to extract LocalizedStringKeys from code when exporting for localization in Xcode. Format your strings to internationalize your code, and style them with Markdown.
Step 1. Add your localizable.strings
file to your UI Test target
Step 2. Create these helper methods in your Test class (found here):
var currentLanguage: (langCode: String, localeCode: String)? {
let currentLocale = Locale(identifier: Locale.preferredLanguages.first!)
guard let langCode = currentLocale.languageCode else {
return nil
}
var localeCode = langCode
if let scriptCode = currentLocale.scriptCode {
localeCode = "\(langCode)-\(scriptCode)"
} else if let regionCode = currentLocale.regionCode {
localeCode = "\(langCode)-\(regionCode)"
}
return (langCode, localeCode)
}
func localizedString(_ key: String) -> String {
let testBundle = Bundle(for: /*YourTestClass*/.self)
if let currentLanguage = currentLanguage,
let testBundlePath = testBundle.path(forResource: currentLanguage.localeCode, ofType: "lproj") ?? testBundle.path(forResource: currentLanguage.langCode, ofType: "lproj"),
let localizedBundle = Bundle(path: testBundlePath)
{
return NSLocalizedString(key, bundle: localizedBundle, comment: "")
}
return "?"
}
Step 3. Change YourTestClass
in step 2 code to your test class
Step 4. Get your localized string by using localizedString("String")
NOTE: This doesn't seem to work for all localization strings. I have found it doesn't work for storyboard strings for me, only the localizable.strings
file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With