Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
DateFormatter returning string with incorrect language

I'm trying to show a formatted date at my device's language but it keeps showing in English (even though the device is set to Portuguese).

At my ViewController I created a date:

let todaysDate: Date = Date()

And I call this function (that it's an extension of 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)
}

The output is always in English:

08 March 2019

I also checked if the Locale.current is correct in the terminal and I got:

▿ en_BR (current)

I don't want to define the language hardcoded because I want to support other languages, but it's not working. Could you please give me any tips on how to solve this?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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)
    }

in "Locale(identntfier: “pt-PT”)" you have to pass exact identifier which you want and need to set language as well. in Info you have to set your exact languahe as well

over 4 years ago · Santiago Trujillo Relatório

0

As I Understand your question. You want to change the date format with the current device language.

Set Locale as your preferred language

let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.locale = Locale.init(identifier: Locale.preferredLanguages.first!)
formatter.setLocalizedDateFormatFromTemplate("ddMMMMyyyy HH:mm")
let stringDate = formatter.string(from: Date())

print(stringDate)

Output:- Show date with current device language.

08 de março de 2019 10:54

over 4 years ago · Santiago Trujillo Relatório

0

In order to have the app displaying the dates in Portuguese,

  1. Add an empty Strings.strings file
  2. In projects settings, add the Portuguese localization
  3. Although the strings file is empty, the app will become localized in Portuguese so by changing the language, in Settings, to Portuguese
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda