1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
nihasmata
  • 652
  • 1
  • 8
  • 28

3 Answers3

1

XCode 10 does not support image localization.

The good news is that the Asset Catalog has this feature in Xcode 11.

Vladimir Grigorov
  • 10,903
  • 8
  • 60
  • 70
1

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";
lukas28277
  • 97
  • 1
  • 15
0

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)

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116