我有一个简单的应用程序,其中包含两个XIB,每个文件都使用“文件检查器”窗格中的“XCode添加本地化”功能进行了本地化(针对两种语言,en和ar表示阿拉伯语)。
我在根视图控制器xib中放置了两个按钮,以切换应用程序语言并切换到其他本地化的XIB,该应用程序运行正常,除了一个小问题:阿拉伯语XIB包含一个imageView内的图像,以及英语XIB在imageView内包含一个图像,

1)当我将语言从英语切换为阿拉伯语时,阿拉伯语XIB到位但没有图像

2)反之亦然,当我从阿拉伯语切换到英语时

3)甚至在应用启动时也不会显示图像:
XCode在这3种情况下会在控制台中产生此错误,而不会导致应用程序崩溃:
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)"didFinishLaunchingWithOptions方法代码为:

     [[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];

lang选择器代码为:
      - (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];
}

here is the sample app包含我在上面描述的内容。如果你们能帮助解决这个问题,我将非常感谢您。

提前非常感谢您...

最佳答案

我认为无法以您的方式完成此操作,即使如此,您仍在使用API​​来使您的应用程序从App Store中退回-Changing language on the fly, in running iOS, programmatically

但是您可以尝试定位图像(通过属性检查器完成)。

与其尝试通过更改本地化来加载不同的笔尖,还不如手动更改笔尖。不必本地化相同的nib文件,而必须创建两个nib文件,并根据用户的选择加载特定于语言的文件-您还负责自己加载所有语言本地化的资源,这不是一件容易的事。

实际上,语言选择最好由用户在设备级别上完成,而不是由您自己通过尝试进行本地化更改来完成-正如我所说的那样,Apple不鼓励这样做。

09-10 00:36
查看更多