Nsdateformatter and Current Language in iOS11

NSDateFormatter and current language in iOS11

This isn't a problem with NSDateFormatter, it's a change in how iOS 11 supports localization.

Under iOS 11, [NSLocale currentLocale] only returns languages supported by your app's localizations. If your app only supports English (as the base localization), then no matter what language the user selects on the device, currentLocale will always return English.

Under iOS 10 and earlier, currentLocale would directly represent the user's chosen language and region, regardless of what localizations your app supports.

Classes such as NSDateFormatter default to using NSLocale currentLocale. So no matter what language your app actually supported through its localization, classes like NSDateFormatter would show text in the language set on the device, even it was different from the language being used by your app.

iOS 11 fixes this inconsistency. While one could argue that this change breaks lots of apps that only support one (or just a few) language, it actually makes the app more consistent.

To make all of this clear, consider an example. You create a simple test app with a base localization in English. If you run your app with iOS 10 and the device's language is set to English, you obviously see English text and you see dates formatted for English. If you now change the device's language to French and restart the app, the user now sees English text in the app (since that is its only localization) but dates now show with French month and weekday names.

Now run the same app under iOS 11. As with iOS 10, if the device's language is English you see everything in English. If you then change the device's language to French and run the app, iOS 11 sees that your app only supports English and currentLocale returns English, not French. So now the user sees English text (due to the app's localization) and dates are now also still in English.

DateFormatter returning string with incorrect language

Thank you very much for your help!

I just found out that I only had Localized strings for English and as rmaddy pointed out in this link with iOS 11 I also need to have a Localized string file for the language I want to support (in my case Portuguese).

So I just added support for Portuguese at Language localization section and now it's working correctly! :D

Localization

NSLocale returning wrong value in iOS 11

Found it!

It's a change behaviour starting from iOS 11.

Under iOS 11,
[NSLocale currentLocale] only returns languages supported by your app’s localizations. If your app only supports English (as the base localization), then no matter what language the user selects on the device, currentLocale will always return English.

Under iOS 10 and earlier, currentLocale would directly represent the
user’s chosen language and region, regardless of what localizations
your app supports.

More information here:

  • https://jaanus.com/ios-11-changes-localized-date-handling/

  • Maddy's stack overflow answer: https://stackoverflow.com/a/46206511/1226963

NSDateFormatter - set device language as locale?

I had forgotten to set the Region Format to my local region format :)



Related Topics



Leave a reply



Submit