ios - Bundle.preferredLocalizations 困惑

标签 ios localization nsbundle

3 preferredLocalizationsBundle太困惑了,我有很多问题:

  1. 为什么 2 preferredLocalizations方法是类方法,但一直在谈论一些未指定特定的包,就好像它们是在某个个体上调用的一样Bundle实例?

    • preferredLocalizations(from:) : 到底是什么a bundle objectthe bundle

      1. Returns one or more localizations from the specified list that a bundle object would use to locate resources for the current user.

      2. An array of NSString objects, each of which specifies the language ID for a localization that the bundle supports.

    • preferred​Localizations(from:​for​Preferences:​) : 到底是什么the specified bundlethe receiver’s bundle

      1. Returns the localizations that a bundle object would prefer, given the specified bundle and user’s language preferences.

      2. 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 localizations​Array parameter. If none of the user-preferred localizations are available in the bundle, this method returns one of the bundle localizations.

  2. 为什么 preferredLocalizations(from: localizations)不会返回与 preferred​Localizations(from: localizations, ​for​Preferences:​ 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/

相关文章:

objective-c - NSUserDefaults 不保存

ios - 如何在不转换为 Swift 中的任何时区的情况下将字符串更改为日期?

ios - CKQueryOperation 可见性、取消和超时可能性

ios - 本地化 IOS 默认语言

visual-studio - 如何将附属程序集添加到 Visual Studio 中的项目

xcode - 使用 CFBundleGetVersionNumber

objective-c - 在包 NSBundle 中找不到名为 'Main' 的 Storyboard

ios - 让 Chrome 假装成 iPad

java - Spring LocaleResolver 链接

objective-c - loadNibNamed :owner:topLevelObjects: is called 时插件崩溃