I have two different Apps that I run from XCode on the same device.
In AppDelegate application didFinishLaunchingWithOptions I print out the following debugging message:
print( Locale.current )
In one App it prints out sv_SE
(as I would expect), but on the other App it prints out en_SE
!!
As a result, dateFormatter.string
will produce english names instead of the Swedish names that I where expecting.
func dayOfWeek(date:Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
dateFormatter.locale = Locale.current // locale is en_SE not sv_SE ???
let dayname = dateFormatter.string(from: date).capitalized
return dayname // returns SATURDAY not LÖRDAG
}
Question: Why do I get different locales in different Apps when ran on the same device?
I found the answer here: Locale.current reporting wrong language on device
The answer was that Locale.current
is not the locale set on the device, but a "compromise" between that locale what the App supports. My first App did support swedish but my second did not. To get the locale on the device one should instead use Locale.preferredLanguages.first
, as is done in the answer to Locale.current reporting wrong language on device.