简体   繁体   English

从文件路径加载笔尖

[英]Load nib from file path

We might have a need for loading a nib using the results of 我们可能需要使用以下结果加载笔尖

[NSBundle -pathForResource:ofType:inDirectory:forLocalization:]

I thought this would be possible, but all the methods I can find only want the name not leading path info. 我以为这是有可能的,但是我能找到的所有方法只想要名称而不是前导路径信息。

We are exploring a way to provide both fr-Fr and fr-CA through one code base. 我们正在探索一种通过一个代码库同时提供fr-Fr和fr-CA的方法。 Our client wants a fr-CA translation and we're afraid we might have another customer that wants a fr-Fr translation. 我们的客户想要fr-CA翻译,而我们担心可能会有另一个客户想要fr-Fr翻译。 I was thinking we could path to correct nib based on the selected language. 我在想我们可以根据所选的语言来纠正笔尖。

Thanks 谢谢

The path lookup takes into account the user's locale preferences. 路径查找考虑了用户的语言环境首选项。 If you provide the proper localization directories ( .lproj ), the right nib should be loaded by the system. 如果提供正确的本地化目录( .lproj ),则系统应加载正确的笔尖。

Per Apple's documentation : 根据Apple的文档

The method first looks for a matching resource file in the non-localized resource directory of the specified bundle. 该方法首先在指定捆绑软件的非本地化资源目录中查找匹配的资源文件。 (In Mac OS X, this directory is typically called Resources but in iOS, it is the main bundle directory.) If a matching resource file is not found, it then looks in the top level of any available language-specific “.lproj” directories. (在Mac OS X中,此目录通常称为资源,而在iOS中,它是主捆绑包目录。)如果找不到匹配的资源文件,则它将在任何可用的语言特定的“ .lproj”的顶层查找目录。 (The search order for the language-specific directories corresponds to the user's preferences.) It does not recurse through other subdirectories at any of these locations. (特定于语言的目录的搜索顺序与用户的首选项相对应。)它不会在任何这些位置的其他子目录中递归。 For more details see Internationalization Programming Topics. 有关更多详细信息,请参见国际化编程主题。

Unfortunately, Apple's documentation is self-contradictory and incomplete. 不幸的是,Apple的文档是自相矛盾且不完整的。 If you look at "Internationalization and Programming Topics", section "Language and Locale Designations", you will find under "Language and Locale IDs" this gem: 如果查看“国际化和编程主题”的“语言和语言环境名称”部分,则可以在“语言和语言环境ID”下找到该gem:

Important: In iOS, the bundle interfaces do not take dialect or script information into account when looking for localized resources; 重要提示:在iOS中,捆绑接口在查找本地化资源时不会考虑方言或脚本信息;因此,捆绑接口不会考虑方言或脚本信息。 only the language designator code is considered. 仅考虑语言指示符代码。 Therefore if your project includes language-specific project directories with both a language and region designator, those directories are ignored. 因此,如果您的项目包括具有语言和地区指示符的特定于语言的项目目录,则这些目录将被忽略。 The bundle interfaces in Mac OS X do support region designators in language-specific project directories. Mac OS X中的捆绑软件接口确实支持语言特定的项目目录中的区域标识。

This is not entirely true. 这并非完全正确。 If you look at Apple's own applications, you will see that iOS does support some region designations, but only the ones Apple cares about. 如果查看Apple自己的应用程序,您会看到iOS确实支持某些地区名称,但只有Apple关心的名称。 See for example the list of .lproj directories in a recent Mobile Safari: 例如,请参阅最近的Mobile Safari中的.lproj目录列表:

$ ls -d *.lproj
Dutch.lproj/    el.lproj/       pt_PT.lproj/
English.lproj/  en_GB.lproj/    ro.lproj/
French.lproj/   fi.lproj/       ru.lproj/
German.lproj/   he.lproj/       sk.lproj/
Italian.lproj/  hr.lproj/       sv.lproj/
Japanese.lproj/ hu.lproj/       th.lproj/
Spanish.lproj/  id.lproj/       tr.lproj/
ar.lproj/       ko.lproj/       uk.lproj/
ca.lproj/       no.lproj/       vi.lproj/
cs.lproj/       pl.lproj/       zh_CN.lproj/
da.lproj/       pt.lproj/       zh_TW.lproj/

Notice that two Chinese regions, a British English region, and a Portugal Portuguese region are supported. 请注意,支持两个中文区域,一个英式英语区域和一个葡萄牙葡萄牙语区域​​。 But none of these solves the problem of fr_CA versus fr_FR . 但是这些都fr_CAfr_FR的问题。

For that, let's look at MobileSafari again. 为此,让我们再次看一下MobileSafari。 It must implement its own smart lookup, because it has plenty of more finely distinguished StaticBookmarks-xx_YY.plist files. 它必须实现自己的智能查找,因为它具有许多更精细的StaticBookmarks-xx_YY.plist文件。

So, that's the solution: Use the locale functions that are still available, and do your own lookup accordingly. 因此,这就是解决方案:使用仍然可用的语言环境功能,并进行相应的查找。

I don't understand the problem. 我不明白这个问题。 You can perfectly have two different NIBs, for France and Canada (french), as it's supported by Xcode. Xcode支持您完美地为法国和加拿大(法国)提供两种不同的NIB。

Simply add new localization for your file(s): 只需为您的文件添加新的本地化:

在此处输入图片说明

Then use a view controller to load the NIB, with the initWithNibName method. 然后使用视图控制器通过initWithNibName方法加载NIB。 The correct one will be used, depending on the device language settings. 将使用正确的语言,具体取决于设备语言设置。

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

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