WinRT的应用程序和区域设置。正确的方法来格式化根据用户的区域设置日期和数字? [英] WinRT apps and Regional settings. The correct way to format dates and numbers based on the user's regional settings?

查看:179
本文介绍了WinRT的应用程序和区域设置。正确的方法来格式化根据用户的区域设置日期和数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在Windows 8地铁的应用程序的一些问题,对于用户的区域设置(XAML和放C#)。似乎,所以即使你的Windows 8被设置为显示日期和时间格式芬兰在应用程序将不尊重用户的区域设置,应用程序仍然会采用美国的格式显示出来。但是,这是一个大问题,一定有什么我失踪?

I'm having some problems in Windows 8 Metro apps (XAML & C#) regarding the user's regional settings. It seems that the apps won't respect user's regional settings, so even if your Windows 8 is set to display dates and times in Finnish format, the apps will still display them using US-formatting. But this is such a big problem that there must be something I'm missing?

要测试这个,我开始通过创建一个 WPF应用程序即可。该应用程序会打印出的CurrentCulture和格式化DateTime.Now:

To test this I started by creating a WPF-application. The application just prints out the CurrentCulture and the formatted DateTime.Now:

    private void Culture_Loaded_1(object sender, RoutedEventArgs e)
    {
        this.Culture.Text = System.Globalization.CultureInfo.CurrentCulture.DisplayName;
    }

    private void Date_Loaded_1(object sender, RoutedEventArgs e)
    {
        this.Date.Text = DateTime.Now.ToString();
    }

下面是我的默认区域设置:

Here's my default regional settings:

在运行时,应用程序显示在芬兰格式的日期:

When run, the app displayed the date in Finnish format:

然后,我改变了区域设置为美国:

Then I changed the regional settings to US:

和当应用程序被再次运行,文化和格式更改:

And when the app was run again, the culture and formatting changed:

这是<强>如我所料的一切工作,这也是我没有料到的WinRT应用工作。

This is as I expected everything to work and this is also how I expected WinRT apps to work.

因此​​,作为下一个步骤,我创建了一个的WinRT(XAML和C#)应用程序与同code和恢复的区域设置回芬兰。问题:

So as a next step, I created a WinRT (XAML & C#) app with the same code and reverted the regional settings back to Finnish. The problem:

甚至当我通过区域设置,格式应该是芬兰的定义,WinRT的应用程序显示与美国的格式化日期时间。然后我修改了应用程序的项目文件,并提出 FI-FI的默认语言

Even when I've defined through regional settings that the formatting should be "Finnish", the WinRT app displays the datetime with US-formatting. I then modified the app's project file and made fi-FI the default language:

这变化也修改了应用程序的文化:

This change also modified the app's culture:

奇怪。我改变了默认语言恢复到默认值和格式就恢复了美国。然后,我创建的文件夹字符串 - FI-FI内的项目,并增加了空Resources.resw到项目。这个空文件似乎是不够的,因为我现在是越来越芬兰格式:

Strange. I changed the Default Language back to its default value and the formatting was restored to US. I then created folders "Strings - fi-FI" inside the project and added an empty "Resources.resw" to the project. This empty file seems to be enough, as I was now getting the Finnish formatting:


当我删除空资源文件的打印格式回复到美国:

As soon as I remove the empty resource file, the formattings reverts back to US:


很奇怪。

这导致了一些问题,但主要是我想的是:?是不是故意的WinRT的,应用程序不按照用户的区域设置,如WPF应用执行

This leads to few questions, but the main one I think is: Is it intentional that the WinRT-apps don't follow the user's regional settings like the WPF apps do?

推荐答案

这已经有一段时间,但这个问题没有完全回答,所以让我分享我的研究很少。 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:


  • 日期和放大器;时间格式示例:
    <一href=\"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/Date-and-time-formatting-2361f348/source$c$c?fileId=52070&pathId=561085805

  • 数字格式和放大器;分析示例:搜索
    <一href=\"http://$c$c.msdn.microsoft.com/windowsapps/Number-formatting-and-bb10ba3d/source$c$c?fileId=52249&pathId=1462911094\">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.

这篇关于WinRT的应用程序和区域设置。正确的方法来格式化根据用户的区域设置日期和数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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