且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WinRT的应用程序和区域设置。正确的方法来格式化根据用户的区域设置日期和数字?

更新时间:2023-10-03 23:15:46

这已经有一段时间,但这个问题没有完全回答,所以让我分享我的研究很少。 Depechie主要是对的,但他只提供了一个链接,真的不知道。

It's been a while, but the question is not fully answered, so let me share my little research. Depechie is mostly right, but he provided only a link and wasn't really sure.

不错,这意想不到的变化是故意的。因为它包含旧的codeS和微软希望我们使用的API Windows.Globalization相反,我们不应该使用的CultureInfo了。

Yes, this unexpected change is intentional. We shouldn't use CultureInfo anymore as it contains legacy codes and Microsoft want us to use Windows.Globalization APIs instead.

要获得当前区域我们可以使用:

To obtain current region we can use:

GeographicRegion userRegion = new GeographicRegion();
string regionCode = userRegion.CodeTwoLetter;

但我注意到它仅包含区域的信息,没有语言code。为了获得语言,我们可以使用:

But as I noticed it contains only region information, there's no language code. To obtain language we can use:

string langRegionCode = Windows.Globalization.Language.CurrentInputMethodLanguageTag; // depends on keyboard settings
List<string> langs = Windows.System.UserProfile.GlobalizationPreferences.Languages; // all user  languages, like in languages control panel
List<string> applicationlangs = Windows.Globalization.ApplicationLanguages.Languages; // application languages (user languages resolved against languages declared as supported by application)

他们返回格式语言地区BCP47的语言标签,如EN-US,如果语言都有方言,或只是语言如PL,如果语言没有重大的方言。

They return BCP47 language tags in format language-REGION like "en-US" if language has dialects or just language like "pl" if the language doesn't have major dialects.

我们还可以设置一个主语言,这将覆盖所有的休息:

We can also set one primary language which will override all the rest:

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "en-US";

(这是一个持久设置,应该在用户请求中使用)

(This is a persisted setting and is supposed to be used at user request)

有也是日期,时间和电话号码的新的API:

There is also new API for date, time and numbers:

Windows.Globalization.DateTimeFormatting.DateTimeFormatter dtf = new DateTimeFormatter("longdate", new[] { "en-US" }, "US", CalendarIdentifiers.Gregorian, ClockIdentifiers.TwentyFourHour);
string longDate = dtf.Format(DateTime.Now);

Windows.Globalization.NumberFormatting.DecimalFormatter deciamlFormatter = new DecimalFormatter(new string[] { "PL" }, "PL");
double d1 = (double)deciamlFormatter.ParseDouble("2,5"); // ParseDouble returns double?, not double

有真的在Windows.Globalization API的多很多,但我认为,这给我们的总体思路。进一步阅读:

There's really a lot more in Windows.Globalization APIs, but I think that this gives us the general idea. For further reading:


  • 日期和放大器;时间格式示例:
    http://$c$c.msdn.microsoft.com/windowsapps/Date-and-time-formatting-2361f348/source$c$c?fileId=52070&pathId=561085805

  • 数字格式和放大器;分析示例:搜索
    http://$c$c.msdn.microsoft.com/windowsapps/Number-formatting-and-bb10ba3d/source$c$c?fileId=52249&pathId=1462911094

  • 也有一个很好的文章,题为如何使用模式来格式化日期和时间MSDN上,但我可以只添加2链接

您还可以找到有关该问题的一些议题上与一些微软员工回答的Windows 8开发中心的论坛,但他们主要是送你的文档。

You can also find some topics about the issue on windows 8 dev center forum with some Microsoft employee answers, but they mainly send you to the documentation.