Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to localize the images in Images.xcassets?

We can localize an image in the File Inspector using Localize... like this:

enter image description here

Then we can get this:

enter image description here

But now, I use Images.xcassets to manage my images in my iOS project, how should I localize these images in Images.xcassets?

like image 322
SamirChen Avatar asked Jan 23 '14 14:01

SamirChen


People also ask

How do I localize Xib files?

strings files from the xib, which make localization pretty straight forward. Right click on the xib file in Xcode, and choose Get Info . Select the General tab and on the bottom click Make File Localizable . Then you will be able to add localizations by clicking Add Localization on that same tab.

How do you localize in Swift?

Click on iOS->App/Single View App->Next. Name your project and select the language as Swift. Select a desired location and click on Create. To start with Localization, click on your Project Name -> go to Info Tab -> Under Localizations, click on the '+' button.

How do you localize a storyboard?

Go to storyboard and click on UI Element that you want to localize. Select identity inspector, In Document portion you will see Object ID that we need to use for localize that element. Now goto localization file that created from step 3. In that string file you will see Object ID of elements.


2 Answers

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)]; 
like image 81
cc. Avatar answered Nov 06 '22 20:11

cc.


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

like image 36
Vladimir Grigorov Avatar answered Nov 06 '22 21:11

Vladimir Grigorov