1

I have a simple app, which contains two XIBs, each one is localized (for two languages en and ar for arabic) using XCode Add Localization feature in File Inspector pane.. I have placed two buttons in the root view controller xib to switch the app language and switch to the other localized XIB, the app works perfectly, except for one small issue: the Arabic XIB contains an image inside an imageView, and also the english XIB contains an image inside an imageView,

1) when I switch the language from English to Arabic, the Arabic XIB comes in place but without the image

2) and vice versa when I switch from Arabic To English

3) and even at app launch it does not display the image: XCode produces this error in these 3 cases in Console without crashing the app:

2011-12-27 12:48:30.412 LangSwitch[4903:f803] Could not load the "image1.png" image referenced from a nib in the bundle with identifier "(null)"

the didFinishLaunchingWithOptions method code is:

     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(lang:) name:@"Lang" object:nil];
// Override point for customization after application launch.

// Set the navigation controller as the window's root view controller and display.
NSString* str = [[[NSUserDefaults standardUserDefaults]valueForKey:@"AppleLanguages"] objectAtIndex:0];
NSBundle* bnd = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:str ofType:@"lproj"]];
    RootViewController* cont = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:bnd];
self.window.rootViewController = cont;
[self.window makeKeyAndVisible];

and the lang selector code is:

      - (void)lang:(NSNotification*)sender{
//NSLog(@"%@",[sender object]);
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:[sender object],nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults]synchronize];

NSString* str = [[[NSUserDefaults standardUserDefaults]valueForKey:@"AppleLanguages"] objectAtIndex:0];
NSBundle* bnd = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:str ofType:@"lproj"]];
RootViewController* cont = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:bnd];
self.window.rootViewController = cont;
[self.window makeKeyAndVisible];
}

and here is the sample app that contains what I'm describing above. if you guys could help solving this issue i'd be so grateful to you.

thank you so much in advance...

JAHelia
  • 6,934
  • 17
  • 74
  • 134

5 Answers5

2

The problem is the xib file seeks the images in its own local bundle, but the images are not there and naturally you get an error. The solution is:

  • Remove the images from the xib file. That means you should only have empty imageviews, buttons without background images ..etc on your xib file.

  • Using the assistant window, connect all subviews to your header file and synthesize associated properties.

  • In your view did load method, manually set your images like:

    [_logoImageView setImage:[UIImage imageNamed:@"logo.png"]];
    
    [_button1 setBackgroundImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
    

Now your images are loaded from the main bundle, but your xib files are localized. The compiler will not give any error.

Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
1

Xib files load image from the main bundle and not from your custom language specific bundle.

You can't really change the language of a internationalized app programaticly. The user can change the language in his phone settings but you can't. If you want use more than one language in your app, put all the image resources in the main bundle (no internationalization) and set your own internationalization method (sample: rename pic.png to en-pic.png and ar-pic.png)

Mathieu Hausherr
  • 3,485
  • 23
  • 30
  • how to put the images in main bundle ? how can i do that ? if you look at the sample app, both images are placed on root level of the application on file system. – JAHelia Dec 27 '11 at 10:36
1

I don't think this can be done the way you are doing it, and even then, you are using an API that will get your app bounced from the App Store - Changing language on the fly, in running iOS, programmatically

But you could try localising the images (done through the property inspector).

Rather than try to load different nibs by changing the localization - you're going to have to change the nib manually. Rather than have the same nib file localized, you'll have to create two nib files, and load a language specific one based on the user's choice - you are also responsible for loading all the language localized resources yourself, which is not a small task.

The language choice really is better done at the level of the device by the user, rather than by yourself by trying to force a localization change - which as I've said is discouraged by Apple.

Community
  • 1
  • 1
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • done by creating 4 images (2 for arabic) and (2 for english) and localizing the four. thank you so much. – JAHelia Dec 27 '11 at 14:42
0

I simply browsed the XIB in the xml mode and added the relative folder path. For ex. I kept my images in img folder and image name is test.png. now I went in xml mode and changes test.png to img/test.png

It worked for me!

rohit

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
0

Check whether image "image1.png" is added to your project.

Also verify whether an IBOutlet is defined in the code and linked to the image or not.

Basheer
  • 1,207
  • 9
  • 10
  • BAsheer, there is no need for IBOutlet because I do not want to change any property of the image, and image1 and image2 are both added correctly to the project. and you can verify that by checking the sample app i posted – JAHelia Dec 27 '11 at 10:37
  • I'm unable to download your sample app from rapid share. Can you verify whether the image is added to the Target? Check in the IB whether any false reference is made in that XIB. Check the images are properly opening in Preview. If so, re-save the image with a different name and update your xib. Clean the targets and make the run. Try it :) – Basheer Dec 27 '11 at 11:00
  • Basheer, give me your email to send you the sample ... I've done all what you told me above, same result ... – JAHelia Dec 27 '11 at 11:10
  • Have you created the Arabic.lproj folder in the Finder manually? Remove the Arabic folder. Remove the current localizations you have added in Xcode in all files (RootViewController and TestLangView). – Basheer Dec 27 '11 at 12:30
  • 1
    Go to the RootViewController.xib, "Make file Localizable" and add the language . In my case, I use Xcode 4, so I added the localization by clicking on '+' button and selecting the language. A new folder will be created in the Finder with the language code. For Example, if you select Arabic, it will be "ar.lproj". And the file will be placed inside the folder. Do the above step for the TestLangView.xib and also for the images. The images also needs to be localized. But don't create any folder manually in Finder. The Xcode will create and organize the files based on language. – Basheer Dec 27 '11 at 12:31
  • thank you so much Basheer ... it has worked with me as I described above in the selected answer – JAHelia Dec 27 '11 at 20:34