Foundation - 什么是 Locale 的 collatorIdentifier?
Foundation - What is collatorIdentifier for Locale?
要实现的目标
在table中有一列日期。日期的格式应基于用户的区域设置。
示例:
- 24/01/2020 或 24/01/20(我不介意)法国用户
- 01/24/20 美国用户
注意:该应用(目前)只有英文版。
我有一个 Date
对象,我想以正确的格式向用户显示它。为此,我想我必须使用 DateFormatter,它有一个 locale
参数。但是我遇到了后者的问题。
问题
在 Xcode 操场上,在 Mac 法语(Regio = BE)(但 Xcode 英语)中,以下代码 returns 奇怪的东西.
Locale.current.regionCode // returns "BE"
Locale.current.languageCode // returns "en"
Locale.preferredLanguages[0] // returns "en"
Locale.current.identifier // returns "en_BE"
Locale.current.collatorIdentifier // returns "fr-BE"
Locale.current.collationIdentifier // returns nil
注意:法语是比利时的语言之一。
collatorIdentifier
让我感兴趣,因为它是唯一检测到我的 mac 是法语的。但它是什么?它的目标是什么?为什么它 return 与其他的明显不同?
首先,针对您的问题:区域设置在 Xcode 中很棘手,尤其是在 Playgrounds 中。如果您的 playground 的目标是 iOS,则它基于模拟器的区域设置。如果它针对 macOS,我觉得它有点奇怪,因为 Xcode 覆盖了一些东西。请注意,区域设置更改通常需要重新启动才能完全生效(模拟器或 Mac),但我仍然发现 Playgrounds 不可靠,因为 Xcode 未正确本地化(或者我猜本地化在全部....) 如果您还没有重新启动,则可能会发生不匹配。如果您在 Playgrounds 中 运行,则所有赌注都将取消,即使您重新启动也是如此。我建议创建 Mac 命令行工具。他们更可靠。
如果您打算使用 Playgrounds,请使用您初始化的特定语言环境,而不是 current
。
关于它是什么的问题,归类通常是应该如何考虑字母 "sorted." 这在语言之间、共享语言的文化之间,甚至在单一文化中的用法之间都是不同的。一些示例 from the ICU(还有很多):
The letters A-Z can be sorted in a different order than in English. For example, in Lithuanian, "y" is sorted between "i" and "k".
Combinations of letters can be treated as if they were one letter. For example, in traditional Spanish "ch" is treated as a single
letter, and sorted between "c" and "d".
Accented letters can be treated as minor variants of the unaccented letter. For example, "é" can be treated equivalent to "e".
Accented letters can be treated as distinct letters. For example, "Å" in Danish is treated as a separate letter that sorts just after
"Z".
Unaccented letters that are considered distinct in one language can be indistinct in another. For example, the letters "v" and "w" are two
different letters according to English. However, "v" and "w" are
traditionally considered variant forms of the same letter in Swedish.
collatorIdentifier
是整个标识符,collationIdentifier
是具体用法
let l = Locale(identifier: "de@collation=phonebook")
l.collatorIdentifier // "de@collation=phonebook"
l.collationIdentifier // "phonebook"
要实现的目标
在table中有一列日期。日期的格式应基于用户的区域设置。 示例:
- 24/01/2020 或 24/01/20(我不介意)法国用户
- 01/24/20 美国用户
注意:该应用(目前)只有英文版。
我有一个 Date
对象,我想以正确的格式向用户显示它。为此,我想我必须使用 DateFormatter,它有一个 locale
参数。但是我遇到了后者的问题。
问题
在 Xcode 操场上,在 Mac 法语(Regio = BE)(但 Xcode 英语)中,以下代码 returns 奇怪的东西.
Locale.current.regionCode // returns "BE"
Locale.current.languageCode // returns "en"
Locale.preferredLanguages[0] // returns "en"
Locale.current.identifier // returns "en_BE"
Locale.current.collatorIdentifier // returns "fr-BE"
Locale.current.collationIdentifier // returns nil
注意:法语是比利时的语言之一。
collatorIdentifier
让我感兴趣,因为它是唯一检测到我的 mac 是法语的。但它是什么?它的目标是什么?为什么它 return 与其他的明显不同?
首先,针对您的问题:区域设置在 Xcode 中很棘手,尤其是在 Playgrounds 中。如果您的 playground 的目标是 iOS,则它基于模拟器的区域设置。如果它针对 macOS,我觉得它有点奇怪,因为 Xcode 覆盖了一些东西。请注意,区域设置更改通常需要重新启动才能完全生效(模拟器或 Mac),但我仍然发现 Playgrounds 不可靠,因为 Xcode 未正确本地化(或者我猜本地化在全部....) 如果您还没有重新启动,则可能会发生不匹配。如果您在 Playgrounds 中 运行,则所有赌注都将取消,即使您重新启动也是如此。我建议创建 Mac 命令行工具。他们更可靠。
如果您打算使用 Playgrounds,请使用您初始化的特定语言环境,而不是 current
。
关于它是什么的问题,归类通常是应该如何考虑字母 "sorted." 这在语言之间、共享语言的文化之间,甚至在单一文化中的用法之间都是不同的。一些示例 from the ICU(还有很多):
The letters A-Z can be sorted in a different order than in English. For example, in Lithuanian, "y" is sorted between "i" and "k".
Combinations of letters can be treated as if they were one letter. For example, in traditional Spanish "ch" is treated as a single letter, and sorted between "c" and "d".
Accented letters can be treated as minor variants of the unaccented letter. For example, "é" can be treated equivalent to "e".
Accented letters can be treated as distinct letters. For example, "Å" in Danish is treated as a separate letter that sorts just after "Z".
Unaccented letters that are considered distinct in one language can be indistinct in another. For example, the letters "v" and "w" are two different letters according to English. However, "v" and "w" are traditionally considered variant forms of the same letter in Swedish.
collatorIdentifier
是整个标识符,collationIdentifier
是具体用法
let l = Locale(identifier: "de@collation=phonebook")
l.collatorIdentifier // "de@collation=phonebook"
l.collationIdentifier // "phonebook"