Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locale.current reporting wrong language on device

I'm trying to format currency values in an iOS app, and I'm using the current Locale settings on the device to use the appropriate currency formatting.

In the simulator, everything seems to run fine: when using currencyFormatter.locale = Locale.current, it takes the right locale settings and prints numbers with the right currency format.

On my iPhone however, which is configured in French with French regional settings, I would expect another format to be used (e.g.: 1 234,56 €). But it does not work, and seems to use an English formatting style (e.g.: €1 234,56).

In fact, if I print the current Locale from my app on the device, it does not return fr_FR as I would expect:

NSLog(Locale.current.identifier)
>>> en_FR

The region is good but the language is not, though iOS on that device is clearly in French.

Has anyone an idea about this?

Thanks!

like image 406
Romain Avatar asked Jan 07 '18 10:01

Romain


People also ask

What is locale current?

A locale representing the user's region settings at the time the property is read.

What is device locale on iPhone?

The locale is formed from the settings for the current user's chosen system locale overlaid with any custom settings the user has specified. Use this property when you need to rely on a consistent locale. A locale instance obtained this way does not change even when the user changes region settings.

How do I get the Swift language on my phone?

This can be done by navigating to the device Settings, select an app, select language, and change the chosen language.

How do I find my iPhone language?

iPhone: Go to iPhone Settings > General > Language & Region > iPhone Language. Select a language, then tap Change to {language}.


2 Answers

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)
}
like image 134
florieger Avatar answered Sep 28 '22 20:09

florieger


@Romain The 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.

like image 38
Nikos Polychronakis Avatar answered Sep 28 '22 20:09

Nikos Polychronakis