Neelam Verma
Neelam Verma

Reputation: 3274

How to change localized images at run time

I have created two localized directories named with en.lproj and fr.lproj. I put two different images with same name like.png in those localized directories.

Now i have created a table View Controller with two rows "English" and "French" text respectively.

After selecting first or second row i am fetching the bundle path and changing the whole localized text of my app at run time.

Code:

-(NSString*) languageSelectedStringForKey:(NSString*) key
{

    NSString *path;
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if([[userDefault valueForKey:@"language_Selected"] intValue] == 0)
      path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
    else if([[userDefault valueForKey:@"language_Selected"] intValue] == 1)
      path = [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];

    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString* str=[[languageBundle localizedStringForKey:key value:@"" table:nil] retain];
    return str;
}

Through above code dynamically i can change the language of running app. But how to get images from that bundle?

Any Idea?

Thanks

Upvotes: 0

Views: 912

Answers (1)

Clafou
Clafou

Reputation: 15400

Presumably, you don't want to change every image, only the images that contain text or other culture-dependent content. For these images, you could add an entry to the localizable.strings file that contains the image path for a give language, and use this path instead of a hard-coded path.

That said, in many cases you'll have images included in the UI as part of a .xib. All you need to do in these cases is localize the xib (i.e. make a different version of the .xib per language--you can do this in XCode through the Languages control in the properties panel) and simply change the image in each language version of the .xib using Interface Builder.

Upvotes: 1

Related Questions