0

我在为用户找到正确的 DateTimeFormatter 时遇到了一些麻烦。

例如,将日期转换为字符串时

.ToString("D");

WinRT 中始终使用 en-US 区域性。

我发现应该使用新的全球化 API。

例如

       var langs = Windows.System.UserProfile.GlobalizationPreferences.Languages;

       var homeregion = Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion;


           Windows.Globalization.DateTimeFormatting.DateTimeFormatter dtf = new DateTimeFormatter(homeregion);

但是 HomeGeographicRegion 的结果不是具有新 DateTimeformatter 要求的格式

我也试过这个

 var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(Windows.Globalization.DateTimeFormatting.YearFormat.Default,
                Windows.Globalization.DateTimeFormatting.MonthFormat.Abbreviated,
                Windows.Globalization.DateTimeFormatting.DayFormat.Default,
                Windows.Globalization.DateTimeFormatting.DayOfWeekFormat.Default);

                string result = formatter.Format(Date);

但这也只返回 en-Us 格式的日期字符串。

谁能告诉我根据用户文化获取 DateTimeFormatter 的正确方法是什么(这也通过 uid 自动用于资源本地化)?

4

1 回答 1

4

单参数DateTimeFormatter 构造函数采用模板(类似于“month.abbreviated day dayofweek”)。为此提供区域将失败,并出现无效参数。

对于 Windows 应用商店应用程序,不带语言参数构造的 DateTimeFormatter 将等效于通过提供Windows.Globalization.ApplicationLanguages.Languages 属性的值来构造 DateTimeFormatter 。对于桌面应用程序,默认设置是用户区域设置。

Note that the application languages are resolved from the user languages (which you can query at Windows.System.UserProfile.GlobalizationPreferences.Languages) and the declared application manifest languages (which you can query at Windows.Globalization.ApplicationLanguages.ManifestLanguages).

Finally, the ResolvedLanguage property will let you see exactly what language is being used internally by the DateTimeFormatter.

In my experience, when people get results that they didn't expect, it is generally because the application only supports a single language, in which case, the application language will always be that no matter what the user preference is. Otherwise, verify that the language you expect is at the top of your user language list.

于 2013-02-26T21:52:42.327 回答