ios 中 DateFormatters 的“区域”设置的目的
Purpose of `region` settings in ios for DateFormatters
我知道 DateFormatter
使用语言和地区来生成所需的 strings/content。
尤其是阅读 documentation 时,我似乎正确地理解了它们应该如何工作。
Locales represent the formatting choices for a particular user, not the user’s preferred language. These are often the same but can be different. For example, a native English speaker who lives in Germany might select English as the language and Germany as the region. Text appears in English but dates, times, and numbers follow German formatting rules. The day precedes the month and a 24-hour clock represents times, as shown in Table 4-1
那为什么我把手机设置成
首选系统语言:[西班牙语、意大利语]
地区:美国
我的应用仅支持 德语
一个简单的(伪代码)
let df = DateFormatter()
df.string(from: date)`
returns miércoles, 26. sept 2018
? (德语区格式的西班牙语)
考虑到包(以及因此从 NSLocalizedString(..)
和相关资源返回的所有字符串)已经正确选择了语言(de
,德语),为什么基础对象不使用捆绑包支持和识别相同的语言(而不是区域)以生成所需的输出,而是坚持使用系统语言?
我是否缺少某些应用程序方面的配置,或者我真的必须为我需要使用的每个日历、DateFormatter、NumberFormatter、ecc 覆盖语言、区域设置和区域?特别是考虑到这些 类 及其实例化效率低下。
我在评论中列出了几个问题,答案可能会改变我对问题的理解,但假设问题只是如何使用德国所说的德语来制作格式化程序格式,那只是设置一个区域设置。
let locale = Locale(identifier: "de-DE")
let df = DateFormatter()
df.dateStyle = .full
df.locale = locale
df.string(from: Date()) // "Freitag, 28. September 2018"
我当前的语言环境是 en-US 这一事实不会影响这一点。
创建 DateFormatter 并分配其区域设置并不比仅创建 DateFormatter 花费更多时间。这是一行额外的代码,但如果你愿意,你可以将它包装在一个扩展中(此外,如果你想避免重复创建它们,你可能想要创建共享的不可变实例,但无论你是否设置显式 Locale 都是如此或不)。
像dateStyle
这样的事情的全部意义在于它适应用户的配置。如果您不想适应用户的配置,只需配置格式化程序以按照您希望的方式运行。
我知道 DateFormatter
使用语言和地区来生成所需的 strings/content。
尤其是阅读 documentation 时,我似乎正确地理解了它们应该如何工作。
Locales represent the formatting choices for a particular user, not the user’s preferred language. These are often the same but can be different. For example, a native English speaker who lives in Germany might select English as the language and Germany as the region. Text appears in English but dates, times, and numbers follow German formatting rules. The day precedes the month and a 24-hour clock represents times, as shown in Table 4-1
那为什么我把手机设置成
首选系统语言:[西班牙语、意大利语]
地区:美国
我的应用仅支持 德语
一个简单的(伪代码)
let df = DateFormatter()
df.string(from: date)`
returns miércoles, 26. sept 2018
? (德语区格式的西班牙语)
考虑到包(以及因此从 NSLocalizedString(..)
和相关资源返回的所有字符串)已经正确选择了语言(de
,德语),为什么基础对象不使用捆绑包支持和识别相同的语言(而不是区域)以生成所需的输出,而是坚持使用系统语言?
我是否缺少某些应用程序方面的配置,或者我真的必须为我需要使用的每个日历、DateFormatter、NumberFormatter、ecc 覆盖语言、区域设置和区域?特别是考虑到这些 类 及其实例化效率低下。
我在评论中列出了几个问题,答案可能会改变我对问题的理解,但假设问题只是如何使用德国所说的德语来制作格式化程序格式,那只是设置一个区域设置。
let locale = Locale(identifier: "de-DE")
let df = DateFormatter()
df.dateStyle = .full
df.locale = locale
df.string(from: Date()) // "Freitag, 28. September 2018"
我当前的语言环境是 en-US 这一事实不会影响这一点。
创建 DateFormatter 并分配其区域设置并不比仅创建 DateFormatter 花费更多时间。这是一行额外的代码,但如果你愿意,你可以将它包装在一个扩展中(此外,如果你想避免重复创建它们,你可能想要创建共享的不可变实例,但无论你是否设置显式 Locale 都是如此或不)。
像dateStyle
这样的事情的全部意义在于它适应用户的配置。如果您不想适应用户的配置,只需配置格式化程序以按照您希望的方式运行。