【发布时间】:2013-02-23 12:12:13
【问题描述】:
我在为用户找到正确的 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 自动用于资源本地化)?
【问题讨论】:
-
ToString(String)使用“当前”文化。你是说如果你改成 en-us 以外的东西,ToString仍然格式化为 en-us? -
嗯,我没有改变它,但我希望文化是 de-DE,因为它是一台没有任何额外语言的德国电脑。但你的权利,文化是我们。那么为什么winrt不尊重这一点并将其设置为文化呢?所以我需要以其他方式获得用户文化?
标签: c# windows-runtime globalization culture