Is it possible to localize image assets for different languages in Xcode and Swift?
Same image name but different language folders.
Or if it is not possible, could you propose a way?
Is it possible to localize image assets for different languages in Xcode and Swift?
Same image name but different language folders.
Or if it is not possible, could you propose a way?
XCode 10 does not support image localization.
The good news is that the Asset Catalog has this feature in Xcode 11.
There is another way for Xcode 10.
Add the main picture to Image Assets eg for English "Picture 1" and add other picture for eg German named "Picture 1_de".
Just put extension in any of your .swift file:
extension String {
func localized(bundle: Bundle = .main, tableName: String = "Localizable") -> String {
return NSLocalizedString(self, tableName: tableName, value: "**\(self)**", comment: "")
}
}
To the ViewController file add:
let btnImage = UIImage(named: "Picture 1.jpg".localized())
Make Localizable.strings file, if you dont have one yet. When you add a new language(in our case German), you will have a new file Localizable.strings(German).
Then simple add to this file:
"Picture 1.jpg" = "Picture 1_de.jpg";
To localize Assets in Assets.xcassets is apparently currently not possible, since the Xcode file inspector does not show a localization option.
However, it is possible to localise the name of the image file that you want to load, using something like
let img = UIImage.init(named: NSLocalizedString("LOCALIZATION_KEYWORD", comment:""))
see here (in Obj-c)