简体   繁体   English

自定义NumberFormat用于多语言UWP应用

[英]Custom NumberFormat for multi-language UWP app

I'm working on a multi-language app that should have the same NumberFormat and DateTimeFormat across all languages (4 in total). 我正在开发一种多语言应用程序,该应用程序在所有语言(总共4种)中应具有相同的NumberFormat和DateTimeFormat。 I feel the best way to achieve this would be to set those formats only once, and not every time I convert a value to a string, as this might be forgotten on some values and lead to errors. 我觉得实现此目标的最佳方法是仅将这些格式设置一次,而不是每次将值转换为字符串时都设置一次,因为这可能会遗忘某些值并导致错误。

In a Silverlight app, this was achievable in the following way: 在Silverlight应用程序中,可以通过以下方式实现:

var cultureInfo = new CultureInfo(languageCode);
cultureInfo.NumberFormat.NumberGroupSeparator = " ";
cultureInfo.NumberFormat.NumberDecimalDigits = 2;
cultureInfo.NumberFormat.NumberDecimalSeparator = ",";
cultureInfo.DateTimeFormat.ShortDatePattern = "dd'/'MM'/'yyyy";
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;

Since Thread.CurrentThread.CurrentCulture is no longer available, I use CultureInfo.CurrentCulture instead. 由于Thread.CurrentThread.CurrentCulture不再可用,因此我改用CultureInfo.CurrentCulture Unfortunately, the custom cultureInfo does not appear to be set across the entire app by using this method. 不幸的是,使用此方法似乎并未在整个应用程序中设置自定义的CultureInfo。 I set the culture in the OnLaunched method, so I would think that it is set on the correct thread. 我在OnLaunched方法中设置了区域性,因此我认为它是在正确的线程上设置的。

I know that since WinRT, apps only run in one of the cultures that the app has resources for, but does this mean that we can no longer override the NumberFormat? 我知道自WinRT以来,应用程序只能以其具有资源的一种文化来运行,但这是否意味着我们不能再覆盖NumberFormat了? Or is there a better way to achieve my desired result? 还是有更好的方法来达到我想要的结果?

The formats are not retained when replacing the entire CultureInfo with a custom one, however it is possible to instantiate the individual formats and set them on the current cultures. 用自定义格式替换整个CultureInfo时,不会保留格式,但是可以实例化各个格式并将其设置为当前的区域性。

var cultureInfo = new CultureInfo(languageCode);

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = " ";
nfi.NumberDecimalDigits = 2;
nfi.NumberDecimalSeparator = ",";

DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.ShortDatePattern = "dd'/'MM'/'yyyy";

CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentCulture.NumberFormat = nfi;
CultureInfo.CurrentCulture.DateTimeFormat = dtfi;

The DateTimeFormatter class provides a globally-aware method for formatting a date or time into a string for display to a user. DateTimeFormatter类提供了一种全球通用的方法,用于将日期或时间格式化为字符串以显示给用户。 It can either use the default preferences of the current user, or the caller can override these to specify other languages, geographic region, and clock and calendar systems. 它可以使用当前用户的默认首选项,或者调用者可以覆盖这些首选项以指定其他语言,地理区域以及时钟和日历系统。 The caller can request a format using the well-known constants (shorttime, longtime, shortdate or longdate) or define the specific elements required. 调用者可以使用众所周知的常量(短时间,长时间,短日期或长日期)请求格式,或定义所需的特定元素。

You can get the demo from Microsoft in GitHub. 您可以在GitHub上从Microsoft获得演示。 Date and time formatting sample 日期和时间格式化示例

 DateTimeFormatter[] timeFormatters = new[]
        {
            // Example formatters for times.
            new DateTimeFormatter(
                HourFormat.Default, 
                MinuteFormat.Default, 
                SecondFormat.Default),
            new DateTimeFormatter(
                HourFormat.Default, 
                MinuteFormat.Default, 
                SecondFormat.None),
            new DateTimeFormatter(
                HourFormat.Default, 
                MinuteFormat.None, 
                SecondFormat.None),
         };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM