gpt4 book ai didi

ios - Bundle.preferredLocalizations 困惑

转载 作者:行者123 更新时间:2023-11-29 11:47:18 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com