如何为本地化应用加载

如何为本地化应用加载

本文介绍了如何为本地化应用加载 XIB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模拟器上成功运行和调试本地化应用程序几天后,我发现它(或 XCode 部署过程)进入一种状态,如果模拟器设置为我支持的本地语言之一,它会崩溃在启动时使用以下堆栈:

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:

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

发生崩溃是因为 UIApplication 无法加载 MainWindow.nib 文件:

The crash is happening because the UIApplication is failing to load the MainWindow.nib file:

由于未捕获而终止应用例外'NSInternalInconsistencyException',原因:'无法在捆绑包中加载 NIB:'NSBundle/.app>(已加载)',名称为 'MainWindow''

当您本地化应用程序时,您有几个选择.您可以执行以下一项(或两项)操作:

When you localize an app you have a couple of options. You can do one (or both) of the following:

  • 根据设备上的语言对每个加载的 Xib 文件进行本地化.因此,您的本地化 UI 完全是从 Interface Builder 配置的.
  • 您可以为每个语言目标设置一个 Localizable.strings 文件,并从代码中访问其中的字符串.

我选择了后者,因为我希望将所有本地化字符串放在一个位置(因此可以一次全部翻译).这意味着我的项目(fr.lproj、zh-Hant.lproj 等)的语言特定目录中没有任何 xib 文件.相反,我的 en.lproj 充满了我所有的 xib 文件(构建后的 nib 文件)和包含相应翻译的 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...). 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.

正如我所提到的,这已经好几天了.今天(我不确定究竟是什么时候),模拟器上的应用程序在启动时开始崩溃.在设备上安装应用程序运行良好,在模拟器上以英文模式运行应用程序运行良好.

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.

经过大量的填充后,我意识到模拟器正在崩溃,因为它正在语言特定目录(fr.lpro、sz-Hant.lproj、ja.lproj,具体取决于语言设置)中查找其 nib 文件).

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 后,它似乎处于未查看默认语言目录 (en.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.

我通过进入模拟器的应用程序目录解决了这个问题(基于上面的异常消息):

I worked around the problem by going into the application directory for the simulator (based on the exception message above):

/用户/.../库/应用程序支持/iPhoneSimulator/4.3/Applications//.app

然后进入特定语言的子目录并将所有 nib 文件从 en.lproj 复制到该目录中.

Then going into the language specific subdirectory and copying all the nib files from en.lproj into that directory.

手动复制文件后,模拟器加载特定语言的 nib 文件(实际上只是 en.lproj 目录中文件的副本),一切正常.

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.

所以我的问题:

  • 这只是模拟器中的错误吗?对此表示反对
  • 还有其他人遇到过这个问题吗?是的
  • 这是因为我不小心切换了一些晦涩的 XCode 设置吗?不,看起来不像

更新

今天发现这个问题不仅限于模拟器,设备上也会出现这种情况.所以我上面描述的解决方法(将笔尖从默认语言目录复制到目标语言目录)显然不适用于手机.

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.

我尝试了 McCygnus 的建议修复(删除我的 xib 的所有本地化,这会将它们移回根目录)并为我排序.所以我猜本地化系统会检查当前语言目录,然后是根目录,它不会回退到您的默认语言.

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. So I guess the localization system checks the current language directory and then the root, it won't fall back to your default language.

推荐答案

我最近遇到了这个问题,当时我有一个英文版的 nib,但没有将它本地化为任何其他语言(MyNib 在 en.lproj).模拟器仅在其他语言目录(da.lproj、de.lproj 等)中查找 MyNib,如果未找到则不会退回到英文版本.我通过删除笔尖的英文本地化来修复它.这导致它与其他资源一起位于应用程序包的根目录中,而不是在 en.lproj 中.之后,一切都完美无缺.

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). 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. 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. 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. 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.

这篇关于如何为本地化应用加载 XIB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:30