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

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

问题描述

我在 Windows 8 Metro 应用程序 (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#)应用程序,并将区域设置恢复为芬兰语.问题:

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:

奇怪.我将默认语言更改回其默认值,并将格式恢复为美国.然后我在项目中创建了文件夹Strings - 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.

是的,这种意外的变化是有意为之.我们不应再使用 CultureInfo,因为它包含遗留代码,而且 Microsoft 希望我们改用 Windows.Globalization API.

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;

但我注意到它只包含区域信息,没有语言代码.要获得我们可以使用的语言:

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)

如果语言有方言,它们会以 language-REGION 格式返回 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:

您还可以在 Windows 8 开发中心论坛上找到一些有关该问题的主题以及一些 Microsoft 员工的回答,但他们主要将您发送给文档.

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天全站免登陆