3 preferredLocalizations
的 Bundle
太困惑了,我有很多问题:
为什么 2
preferredLocalizations
方法是类方法,但一直在谈论一些未指定特定的包,就好像它们是在某个个体上调用的一样Bundle
实例?preferredLocalizations(from:)
: 到底是什么a bundle object
和the bundle
?-
Returns one or more localizations from the specified list that a bundle object would use to locate resources for the current user.
-
An array of NSString objects, each of which specifies the language ID for a localization that the bundle supports.
-
preferredLocalizations(from:forPreferences:)
: 到底是什么the specified bundle
和the receiver’s bundle
?-
Returns the localizations that a bundle object would prefer, given the specified bundle and user’s language preferences.
-
An array of NSString objects, each of which identifies a localization in the receiver’s bundle. These strings are ordered in the array according to the specified preferences and are taken from the strings in the localizationsArray parameter. If none of the user-preferred localizations are available in the bundle, this method returns one of the bundle localizations.
-
为什么
preferredLocalizations(from: localizations)
不会返回与preferredLocalizations(from: localizations, forPreferences: nil)
相同的结果?如上所述,阅读他们的文档并没有真正帮助。当然,它们看起来是这样设计的,但实际上不是这样设计的:// Locale.preferredLanguages: ["es-CN", "ja-CN", "zh-Hans-CN", "en-CN", "he-IL"]. let localizations = ["de", "en", "es", "fr", "it"] print(Bundle.preferredLocalizations(from: localizations)) // ["en"] print(Bundle.preferredLocalizations(from: localizations, forPreferences: nil)) // ["es"]
最佳答案
引用文档不是很好; Technical Note 2418更好地解释了它。
具体是说
Note that Bundle.preferredLocalizations(from:) will restrict the results to localizations supported by Bundle.mainBundle(), or the return value of Bundle.mainBundle().localizations(). If you would like to match against a different set of language identifiers, use Bundle.preferredLocalizations(from:forPreferences:) which does not rely on mainBundle’s localizations and instead solely relies on the two arguments passed in.
所以,
- 如果您使用
Bundle.preferredLocalizations(from:)
,您提供的本地化列表首先会根据为主包定义的本地化进行过滤 (Bundle.main.localizations
). - 如果您使用
Bundle.preferredLocalizations(from:forPreferences:)
,它会与您提供的列表一起工作。
在您提供的示例中,Bundle.preferredLocalizations(from: localizations)
返回 ["en"]
。这可能是因为您的应用没有定义任何版本的西类牙语(“es”)。如果您向项目添加西类牙语本地化,它应该返回 ["es"]
,因为“es-CN”列在“en-CN”之前。
关于ios - Bundle.preferredLocalizations 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43004252/