Goal to achieve
In a table, there is a column Date. The date should have a format based on the Locale of the user. Example:
Note: the app is (currently) only in English.
I have a Date
object which I would like to display to the user in the right format. For that, I suppose I have to use a DateFormatter, which has a locale
parameter. But I encounter an issue with the latter.
Problem
In a Xcode playground, on a Mac in French (Regio = BE) (but Xcode in English), the following code returns weird things.
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
Note: french is one of the language in Belgium.
collatorIdentifier
interests me because it is the only one that detects that my mac is in French. But what is it? What is the goal of it and why does it return something significantly different than the others?
First, to your problem: Locale is tricky in Xcode, particularly in Playgrounds. If your playground is targeting iOS, then it's based on the Simulator's Locale. If it's targeting macOS, I find it to be just kind of weird because Xcode overrides things. Note that Locale changes generally require a reboot to fully take effect (either the Simulator or the Mac), but I still find that Playgrounds is unreliable because Xcode isn't properly localized (or I guess localized at all....) If you haven't rebooted, then mismatches can happen. If you're running in Playgrounds, all bets are off, even if you do reboot. I recommend creating Mac command-line tools. They're much more reliable.
If you are going to use Playgrounds, work with a specific Locale that you initialize, not current
.
To the question of what it is, collation generally is how letters should be considered "sorted." That's different between languages, between cultures that share a language, and even between usages within a single culture. A few of the examples from the ICU (there are many more):
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.
The collatorIdentifier
is the whole identifier, and the collationIdentifier
is the usage-specific piece.
let l = Locale(identifier: "de@collation=phonebook")
l.collatorIdentifier // "de@collation=phonebook"
l.collationIdentifier // "phonebook"