NSDateFormatter和iOS11中的当前语言 [英] NSDateFormatter and current language in iOS11

查看:108
本文介绍了NSDateFormatter和iOS11中的当前语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS11中,似乎已更改 NSDateFormatter 的默认行为。此代码用于根据iOS11之前当前选择的iPhone / iPad语言工作和生成日期格式化程序:

It appears that default behavior for NSDateFormatter has been changed in iOS11. This code used to work and produced date formatter according to currently selected iPhone/iPad language prior to iOS11:

 _dateFormatterInstance = [[NSDateFormatter alloc] init];
 _dateFormatterInstance.timeZone = [NSTimeZone systemTimeZone];

在iOS11中我们必须为它明确指定语言环境属性:

Looks like in iOS11 we have to explicitly specify locale property for it:

 _dateFormatterInstance = [[NSDateFormatter alloc] init];
 _dateFormatterInstance.timeZone = [NSTimeZone systemTimeZone];
 _dateFormatterInstance.locale = [NSLocale localeWithLocaleIdentifier:[[NSLocale preferredLanguages] firstObject]];

有人可以证实我的发现吗?

Can somebody confirm my findings?

推荐答案

这不是 NSDateFormatter 的问题,这是一个改变iOS 11如何支持本地化。

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

在iOS 11下, [NSLocale currentLocale] 仅返回应用程序支持的语言本地化。如果您的应用仅支持英语(作为基本本地化),则无论用户在设备上选择何种语言, currentLocale 将始终返回英语。

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.

在iOS 10及更早版本中, currentLocale 将直接代表用户选择的语言和区域,无论您的应用支持哪种本地化。

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

诸如 NSDateFormatter 之类的类默认使用 NSLocale currentLocale 。因此,无论您的应用通过其本地化实际支持哪种语言, NSDateFormatter 等类都会以设备上的语言显示文本,即使它与所使用的语言不同你的应用。

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解决了这种不一致的问题。虽然有人可能认为这种变化打破了许多仅支持一种(或几种)语言的应用程序,但它实际上使应用程序更加一致。

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.

使所有这个清楚,考虑一个例子。您可以使用英语基础本地化创建一个简单的测试应用程序。如果您使用iOS 10运行您的应用程序并且设备的语言设置为英语,您显然会看到英文文本,并且您会看到格式化为英语的日期。如果您现在将设备的语言更改为法语并重新启动应用程序,则用户现在可以在应用程序中看到英文文本(因为这是唯一的本地化),但现在日期显示法语月份和工作日名称。

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.

现在在iOS 11下运行相同的应用程序。与iOS 10一样,如果设备的语言是英语,您会看到所有的英语。如果您随后将设备的语言更改为法语并运行应用程序,则iOS 11会看到您的应用程序仅支持英语, currentLocale 返回英语,而不是法语。所以现在用户看到英文文本(由于应用程序的本地化),日期现在仍然是英文。

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.

这篇关于NSDateFormatter和iOS11中的当前语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆