简体   繁体   English

如何为本地化应用程序加载XIB?

[英]How are XIBs loaded for localized apps?

I found after a couple of days of successfully running and debugging localized apps on the Simulator it (or the XCode deployment process) got into a state where, if the Simulator was set to one of my supported local languages, it would crash on startup with the following stack: 我发现在Simulator上成功运行和调试本地化应用程序几天后,它(或XCode部署过程)进入了一种状态,如果模拟器设置为我支持的本地语言之一,它将在启动时崩溃以下堆栈:

3   CoreFoundation                      0x01780e6a +[NSException raise:format:] + 58
4   UIKit                               0x008050fa -[UINib instantiateWithOwner:options:] + 2024
5   UIKit                               0x00806ab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
6   UIKit                               0x0060c17a -[UIApplication _loadMainNibFile] + 172
7   UIKit                               0x0060ccf4 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 
8   UIKit                               0x00617617 -[UIApplication handleEvent:withNewEvent:] + 1533
9   UIKit                               0x0060fabf -[UIApplication sendEvent:] + 71
10  UIKit                               0x00614f2e _UIApplicationHandleEvent + 7576
11  GraphicsServices                    0x01e13992 PurpleEventCallback + 1550

The crash is happening because the UIApplication is failing to load the MainWindow.nib file: 发生崩溃是因为UIApplication无法加载MainWindow.nib文件:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle /.app> (loaded)' with name 'MainWindow'' 因未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在bundle中加载NIB:'NSBundle /.app>(已加载)',名称为'MainWindow''

When you localize an app you have a couple of options. 当您对应用进行本地化时,您有几个选择。 You can do one (or both) of the following: 您可以执行以下一项(或两项):

  • Localize each Xib file which gets loaded based on the language on the device. 根据设备上的语言本地化每个加载的Xib文件。 So your localized UI is completely configured from Interface Builder. 因此,您的本地化UI完全由Interface Builder配置。
  • You can set up a Localizable.strings file for each language target and access the strings therein from code. 您可以为每个语言目标设置Localizable.strings文件,并从代码中访问其中的字符串。

I chose the later because I wanted all my localized strings in single place (so it can be translated all at once). 我之所以选择后者是因为我希望我所有的本地化字符串都在一个地方(所以它可以一次翻译)。 What this means is that I don't have any xib files in language specific directories in my project (fr.lproj, zh-Hant.lproj etc...). 这意味着我的项目中的语言特定目录中没有任何xib文件(fr.lproj,zh-Hant.lproj等...)。 Instead I have en.lproj full of all my xib files (nib files once they are built) and language specific directories containing the corresponding translated Localizable.strings files. 相反,我有en.lproj充满了我的所有xib文件(构建它们后的nib文件)和包含相应翻译的Localizable.strings文件的语言特定目录。

As I mentioned, this has been working fine for a couple of days. 正如我所提到的,这已经好几天了。 Today (and I am not sure at exactly what point), the App on the Simulator started crashing on startup. 今天(我不确定到底是什么点),模拟器上的应用程序在启动时开始崩溃。 Installing the App on the device worked fine and running the App on the Simulator in English mode worked fine. 在设备上安装应用程序工作正常,在英语模式下在模拟器上运行应用程序运行正常。

After a great deal of stuffing around, I realised that the Simulator was crashing because it was looking for its nib files in the language specific directory (fr.lpro, sz-Hant.lproj, ja.lproj depending on the language settings). 经过大量的填充,我意识到模拟器崩溃了,因为它正在语言特定目录中查找其nib文件(fr.lpro,sz-Hant.lproj,ja.lproj,具体取决于语言设置)。

So it seems like the Simulator is in a state where it isn't looking the default language directory (en.lproj) after it fails to find the nib in the language specific directory. 因此,似乎模拟器处于一种状态,即在找不到语言特定目录中的nib后,它不会查看默认语言目录(en.lproj)。

I worked around the problem by going into the application directory for the simulator (based on the exception message above): 我通过进入模拟器的应用程序目录(基于上面的异常消息)解决了这个问题:

/Users/.../Library/Application Support/iPhone Simulator/4.3/Applications//.app /Users/.../Library/Application Support / iPhone模拟器/ 4.3 /应用程序//。应用程序

Then going into the language specific subdirectory and copying all the nib files from en.lproj into that directory. 然后进入特定于语言的子目录,并将en.lproj中的所有nib文件复制到该目录中。

Having copied the files by hand, the simulator loads the language specific nib files (which are actually just copies of the files in the en.lproj directory) and everything works fine. 手动复制文件后,模拟器加载特定于语言的nib文件(实际上只是en.lproj目录中文件的副本),一切正常。

So my questions: 所以我的问题:

  • Is this just a bug in the Simulator? 这只是模拟器中的一个错误吗? So a big NO to that 所以这是一个很大的NO
  • Has anyone else had this problem? 有没有其他人有这个问题? Yep 是的
  • Is this a result of some obscure XCode setting that I have accidentally toggled? 这是我不小心切换的一些模糊的XCode设置的结果吗? Nope, doesn't look like it 不,看起来不像

Update 更新

Discovered today that this issue not confined to the simulator, this happens on the device. 今天发现这个问题不仅限于模拟器,这种情况发生在设备上。 So the workaround I described above (copying the nibs from the default language directory to the target language directory), obviously doesn't work on the phone. 因此,上面描述的解决方法(将nib从默认语言目录复制到目标语言目录)显然无法在手机上运行。

I tried McCygnus's suggested fix (remove all localizations for my xibs, which moves them back into the root directory) and that sorted it for me. 我尝试了McCygnus的建议修复(删除我的xib的所有本地化,将它们移回根目录)并为我排序。 So I guess the localization system checks the current language directory and then the root, it won't fall back to your default language. 所以我猜本地化系统会检查当前的语言目录,然后检查根目录,它不会回退到您的默认语言。

I had this issue recently when I had a english version of the nib but didn't localize it for any of the other languages (MyNib was in en.lproj). 我最近有这个问题,当时我有一个英文版的笔尖,但没有为任何其他语言本地化它(MyNib在en.lproj中)。 The simulator was only looking in the other language directory (da.lproj, de.lproj, etc.) for MyNib and not falling back to the english version if not found. 模拟器只在MyNib的另一个语言目录(da.lproj,de.lproj等)中查找,如果没有找到则不会回到英文版本。 I fixed it by removing the english localization for the nib. 我通过删除笔尖的英文本地化来修复它。 That caused it to be the in the app bundle's root with the other resources instead of in en.lproj. 这导致它成为app bundle的根与其他资源而不是en.lproj。 After that everything worked perfectly. 之后一切都很完美。

I was a bit confused by it since I thought the localization system checked through all of the languages in the user's preferred order with english usually being used by the dev as the catch all. 我有点困惑,因为我认为本地化系统通过用户首选顺序检查所有语言,而英语通常被dev用作全部。 I'm not sure if this is a change in loading behavior or a simulator bug (I never tried on device). 我不确定这是否是加载行为或模拟器错误的变化(我从未尝试过设备)。 I'm guessing it's the latter, but I don't know for sure. 我猜它是后者,但我不确定。 This reminds me though that I need to submit a bug report on it. 这提醒我,虽然我需要提交一份错误报告。

if you are using 如果你正在使用

[[Class alloc] initWithNibName:@"aNibName" bundle:nil];  

change it to 改为

[[Class alloc] initWithNibName:@"aNibName" bundle:[NSBundle mainBundle]];

Had the same symptom but different root cause I'd like to share. 有相同的症状,但不同的根本原因我想分享。 I removed the localization as McCygnus hinted at (thanks for that) but still got the same exception on iPads, only. 我删除了McCygnus暗示的本地化(感谢你),但在iPad上仍然有相同的例外。 It turned out that my Info.plist file contained a link to an iPad specific main window nib file (MainWindow-iPad.nib). 事实证明,我的Info.plist文件包含指向iPad特定主窗口nib文件(MainWindow-iPad.nib)的链接。 After removing that, everything worked. 删除后,一切正常。

I was able to solve this problem by simply restarting xcode. 我只需重启xcode即可解决这个问题。 =/ = /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM