Sunday, April 28, 2013

iOS Localize Image for Language of Applicaiton


How to localize images for iOS?
Put your images into localized folder, such as "en.lproj" , "zh.lproj"
This works for System Language.
But how about using language of application?

We can use string of app language by "NSLocalizedStringFromTable"
But there is NO such function for image.


Here is a solution.
Use the following code to get the path of the image of the app language.

NSString *path = [[NSBundle mainBundle] pathForResource:@"image_name" ofType:@"jpg" inDirectory:@"zh.lproj"]; 

or 

NSString *path = [[NSBundle mainBundle] pathForResource:@"image_name" ofType:@"jpg" inDirectory:@"" forLocalization:@"zh"];


UIImage *image = [UIImage imageWithContentsOfFile:path];


According to an stackoverflow answer, [1]
"imageWithContentsOfFile works properly (considering @2x images with correct scale) starting iOS 4.1 and onwards."

I tested on iPhone simulator iOS 6, imageWithContentsOfFile works fine with retina image.



Reference:

[1]http://stackoverflow.com/questions/3739812/how-to-get-uiimage-imagewithcontentsoffile-and-high-res-images-working
How to get [UIImage imageWithContentsOfFile:] and High Res Images working

[2]http://atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/
UIImage, resolution independence and the iPhone 4′s Retina display

[3]http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html

1 comment:

  1. You can try using this tool: https://poeditor.com/. It really well worked for me.

    ReplyDelete