Reputation: 31665
The problem arises when setup the "Language & Region" (Settings => General => Language & Region) as follows:
However, (as mentioned answer for: How to get detailed language of device in swift calling:
print(Locale.current.identifier)
Logs:
en_JO
which is an invalid local identifier (Logically speaking, Jordan is a Middle-East country which its native language is Arabic not English).
I also checked the availableIdentifiers:
print(Locale.availableIdentifiers)
and -obviously -it does not contains "en_JO".
Also, I tried:
if let regionCode = Locale.current.regionCode, let languageCode = Locale.current.languageCode {
print("\(languageCode)_\(regionCode)")
}
and the output was the same.
It seems that it has nothing to do with identifier validity, but how can I make sure to get a valid identifier? As an example, in my case the expected result should be:
en_CA
So what am I missing here?
Upvotes: 6
Views: 8060
Reputation: 9484
Every locale identifier includes a language code(e.g. en) and a region code(e.g. JO). This much is evident from Apple's documentation:
A locale ID identifies a specific region and its cultural conventions—such as the formatting of dates, times, and numbers. To specify a locale, use an underscore character to combine a language ID with a region designator
This means your statement that en_JO is an invalid identifier is incorrect. It is formed because you have selected english as a language and region to Jordan.
Now, if you want to get only langauge part, you can get it by preferredLangauges,
let langId = Locale.preferredLanguages.first
or by collatorIdentifier on current Locale
let langId = Locale.current.collatorIdentifier // returns optional string
Upvotes: 2
Reputation: 130172
There is nothing invalid about the identifier en_JO
. The identifier is completely valid per the Unicode standard as an identifier for English in Jordan region.
However, that does not mean data for that region has to be available. The system is not required to have data for every crazy combination of language and region.
See Language Matching in the Unicode standard. If there is no data for the requested locale, a fallback locale is then used, in this case probably the locale en
.
Upvotes: 3