Issue in Dateformatter with language

Hello! How can we change the locale in DateFormatter auotmatic or dynamically? I need to set in English or Spanish depends on the user language...

let formatter = DateFormatter() formatter.dateStyle = .long formatter.locale = Locale(identifier: "es_ES") return formatter.string(from: date)

I’m trying in the simulator in spanish and without the Locale identifier keeps in english, also in real devices.

Thanks!

Answered by DavidManso in 732551022

The problem was resolved when I create the Localizable string file, with the two Languages. Automatically swift recognized the Dates preferred language.

Thanks!

Try:

  • setting the locale immediately after creating the formatter (before applying the format)
  • using Locale.autoupdatingCurrent
    let formatter = DateFormatter()
    formatter.locale = Locale.autoupdatingCurrent /// automatically updates with the user's configuration settings:
    formatter.dateStyle = .long
    return formatter.string(from: date)
Accepted Answer

The problem was resolved when I create the Localizable string file, with the two Languages. Automatically swift recognized the Dates preferred language.

Thanks!

Issue in Dateformatter with language
 
 
Q