Reputation: 1444
I have an app and I want to support 2 languages. English and Greek. I have already localised the whole app and everything works perfect except the UI. I want to change images based on the system language user has. It works fine on simulator but when I use my real device the app changes its language except the images.
This is my code for the change.
let language = NSLocale.current.identifier
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
if language == "el_US" {
print(language)
scanBtn.setBackgroundImage(UIImage(named: "scanBtnGr"), for: .normal)
detoxBtn.setBackgroundImage(UIImage(named: "detoxGr"), for: .normal)
bioBtn.setBackgroundImage(UIImage(named: "bioBtn"), for: .normal)
searchBtn.setBackgroundImage(UIImage(named: "browseGr"), for: .normal)
captureBtn.setBackgroundImage(UIImage(named: "scanGr"), for: .normal)
uploadBtn.setBackgroundImage(UIImage(named: "uploadGr"), for: .normal)
}else{
print(language)
scanBtn.setBackgroundImage(UIImage(named: "scanBtn"), for: .normal)
detoxBtn.setBackgroundImage(UIImage(named: "detoxBtn"), for: .normal)
bioBtn.setBackgroundImage(UIImage(named: "bioBtn"), for: .normal)
searchBtn.setBackgroundImage(UIImage(named: "browseBtn"), for: .normal)
captureBtn.setBackgroundImage(UIImage(named: "scan"), for: .normal)
uploadBtn.setBackgroundImage(UIImage(named: "upload"), for: .normal)
}
}
I really don't know where is the problem because on the Simulator works perfect.
Upvotes: 0
Views: 276
Reputation: 781
Changing the way you set those images as below can help.
scanBtn.setBackgroundImage(UIImage(named: NSLocalizedString(key: "scanBtnGr", comment: ""), for: .normal)
In this way, you won't have to worry about the target language code as well. You can remove if statement and define corresponding names in Localizable.strings
file.
Besides, if you change the language of your app without terminating the app and changing the device language, please make sure that setupUI()
function gets invoked one more time after the language switch. viewWillAppear
may be a suitable place for it.
Upvotes: 1