Locale.current reporting wrong language on device Locale.current reporting wrong language on device swift swift

Locale.current reporting wrong language on device


Based on @Romain's answer the forced unwrapping of first! could be avoided by using the Locale.current.identifier as fallback.

func getPreferredLocale() -> Locale {    guard let preferredIdentifier = Locale.preferredLanguages.first else {        return Locale.current    }    return Locale(identifier: preferredIdentifier)}


@RomainThe problem here is that you have not localized your app for French but only for English.

Go to the upper left blue file with the name of your app, select Project > Info and in the Localization area you will see "English - Development Language".Press + and choose French (fr).

That's it.Now you can use Locale.current and if the first language of a device (or simulator) is French, your app will show French currency format.


@florieger's answer in the form of extension:

import Foundationextension Locale {    static func preferredLocale() -> Locale {        guard let preferredIdentifier = Locale.preferredLanguages.first else {            return Locale.current        }        return Locale(identifier: preferredIdentifier)    }}

Then use it like this:

dateFormatter.locale = Locale.preferredLocale()datePicker.locale = Locale.preferredLocale()