为什么我在不同的应用程序中得到不同的Locale.current?
Why do I get different Locale.current in different Apps?
我在 同一设备 上 XCode 运行 有两个不同的应用程序。
在 AppDelegate 应用程序 didFinishLaunchingWithOptions 中,我打印出以下调试消息:
print( Locale.current )
在一个应用程序中,它打印出 sv_SE
(如我所料),但在另一个应用程序中,它打印出 en_SE
!
因此,dateFormatter.string
将生成英文名称,而不是我期望的瑞典名称。
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
}
问题:为什么我在同一台设备上运行时在不同的应用程序中得到不同的语言环境?
我在这里找到了答案:
答案是 Locale.current
不是 设备上设置的语言环境,而是应用程序支持的语言环境之间的 "compromise"。我的第一个应用程序确实支持瑞典语,但我的第二个应用程序不支持。要在设备上获取语言环境,应该使用 Locale.preferredLanguages.first
,就像在对 .
的回答中所做的那样
我在 同一设备 上 XCode 运行 有两个不同的应用程序。
在 AppDelegate 应用程序 didFinishLaunchingWithOptions 中,我打印出以下调试消息:
print( Locale.current )
在一个应用程序中,它打印出 sv_SE
(如我所料),但在另一个应用程序中,它打印出 en_SE
!
因此,dateFormatter.string
将生成英文名称,而不是我期望的瑞典名称。
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
}
问题:为什么我在同一台设备上运行时在不同的应用程序中得到不同的语言环境?
我在这里找到了答案:
答案是 Locale.current
不是 设备上设置的语言环境,而是应用程序支持的语言环境之间的 "compromise"。我的第一个应用程序确实支持瑞典语,但我的第二个应用程序不支持。要在设备上获取语言环境,应该使用 Locale.preferredLanguages.first
,就像在对