简体   繁体   中英

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). 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:

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. Unfortunately, the custom cultureInfo does not appear to be set across the entire app by using this method. I set the culture in the OnLaunched method, so I would think that it is set on the correct thread.

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? 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.

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. 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. 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),
         };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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