How to localize the images in Images.xcassets? How to localize the images in Images.xcassets? ios ios

How to localize the images in Images.xcassets?


If you are using an Asset Catalog:

Asset catalog elements are now localizable. In the information panel for an asset, there is a "Localization" section that lists all the languages you've set up for your project in the project settings. If you don't select any of them, your images will be deemed "universal" (i.e., they will adopt the default behavior). If you select one or more of them, you will be able to select different images for "universal" and any alternate language.

For example, if your base language is English, and you want Spanish-language alternates, select only "Spanish" and drag in the alternate versions in the Spanish wells, and leave the English versions as the Universal. Then, at run-time, if the chosen language is Spanish, then the Spanish-language image will be pulled. Otherwise, it will pull the English version. (Or, if you want specific English and Spanish versions that are both different from "everything else", also check English and drag in the corresponding English and Universal images.)

If you need to determine localized images at run time without using the Asset Catalog:

While there's (apparently) not currently a way to explicitly localize the xcassets file directly, you could instead localize the name of the asset to pull using your Localizable.strings file. For instance, say you have a graphic logo treatment for the main menu of a game that needs to be localized. You could put the localized logo treatments in the assets file with different names, add the names to your Localizable.strings file, and then fetch the appropriate image with code like this:

UIImage *img = [UIImage imageNamed:NSLocalizedString(@"MAIN_MENU_IMAGE", nil)];


That "Localize..." button is back in Xcode 11! So now you can localize your images and assets in the Asset catalog as you expect:

enter image description here


When Apple gives you lemons, make lemonade, or in this case, lemonade_en, lemonade_es or whatever suits your needs.

First, I create an entry for each desired language in my assets file like so:

enter image description here

Next, you will need to get a language code for the device. The important thing to remember here is that the user may have an unsupported language, so if that is the case, return your default (I use English).

The following extension of UIApplication will handle all of this for you:

extension UIApplication {    var languageCode: String {        let supportedLanguageCodes = ["en", "es", "fr"]        let languageCode = Locale.current.languageCode ?? "en"        return supportedLanguageCodes.contains(languageCode) ? languageCode : "en"    }}

This extension does the following:

  • Creates an array of the languages my app supports
  • Obtains the current device's language code, or returns a default value if this is nil
  • Finally, it checks to see if the current code is in my supported list. If it is, it returns it, otherwise it returns my default code

Now, we combine the two to get the proper image:

let languageCode = UIApplication.shared.languageCodelet image = UIImage(named: "access_\(languageCode)")