DateFormatter 返回语言不正确的字符串

Ana*_*ula 7 iphone nsdateformatter ios swift

我正在尝试以我的设备语言显示格式化的日期,但它一直以英语显示(即使设备设置为葡萄牙语)。

在我的 ViewController 我创建了一个日期:

let todaysDate: Date = Date()
Run Code Online (Sandbox Code Playgroud)

我调用这个函数(它是 Date 的扩展):

func getFullDate() -> String {
    let dateFormatter  = DateFormatter()
    dateFormatter.dateFormat = "dd MMMM yyyy"
    dateFormatter.calendar = Calendar.current
    dateFormatter.locale = Locale.current
    return dateFormatter.string(from: self)
}
Run Code Online (Sandbox Code Playgroud)

输出始终为英文:

08 March 2019
Run Code Online (Sandbox Code Playgroud)

我还检查了终端中的 Locale.current 是否正确,我得到:

? en_BR (current)
Run Code Online (Sandbox Code Playgroud)

我不想定义硬编码的语言,因为我想支持其他语言,但它不起作用。你能给我一些关于如何解决这个问题的提示吗?

Dev*_*ami 5

func portugueseFormattedEn_US -> String {
        Formatter.date.calendar = Calendar(identifier: .iso8601)
        Formatter.date.locale   = Locale(identifier: “pt-PT”)
        Formatter.date.timeZone = .current
        Formatter.date.dateFormat = "dd/M/yyyy, H:mm"
        return Formatter.date.string(from: self)
    }
Run Code Online (Sandbox Code Playgroud)

在“Locale(identntfier:“pt-PT”)”中,您必须传递您想要的确切标识符,并且还需要设置语言。 在信息中,您还必须设置您的确切语言


Ana*_*ula 1

非常感谢您的帮助!

我刚刚发现我只有英语的本地化字符串,正如 rmaddy 在此链接中指出的那样我还需要有一个我想要支持的语言的本地化字符串文件(在我的例子中是葡萄牙语)。

所以我刚刚在语言本地化部分添加了对葡萄牙语的支持,现在它可以正常工作了!:D

本土化