我在为用户找到合适的DateTimeFormatter时遇到了一些麻烦。
将日期转换为字符串时,例如使用
.ToString("D");
始终在WinRT中使用en-US文化。
我发现应该使用新的全球化apis。
例如
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自动用于资源本地化)?
答案 0 :(得分:3)
单个参数DateTimeFormatter constructor采用模板(类似“month.abbreviated day dayofweek”)。为此提供一个区域将失败,并且参数无效。
对于Windows应用商店应用,如果通过提供Windows.Globalization.ApplicationLanguages.Languages property的值来构造DateTimeFormatter,则不使用语言参数构造的DateTimeFormatters将等效。对于桌面应用程序,默认为用户区域设置。
请注意,应用程序语言是通过用户语言(可以在Windows.System.UserProfile.GlobalizationPreferences.Languages中查询)和声明的应用程序清单语言(可以在Windows.Globalization.ApplicationLanguages.ManifestLanguages查询)解析的。< / p>
最后,ResolvedLanguage property将让您确切了解DateTimeFormatter在内部使用的语言。
根据我的经验,当人们得到他们没有想到的结果时,通常是因为应用程序仅支持单一语言,在这种情况下,无论用户偏好是什么,应用程序语言总是如此。否则,请验证您期望的语言是否位于用户语言列表的顶部。