From 572f34fcb6e7c83e0119a3b59861c70cb8c6cd9b Mon Sep 17 00:00:00 2001 From: "Dr.Rx" Date: Fri, 24 May 2019 08:50:06 -0400 Subject: [PATCH] Mixed workaround to get Date calc and Converters to work with Uno --- .../Common/LocalizationService.cs | 33 +++++++++++-------- .../Common/NetworkManager.cs | 33 +++++++++++-------- .../DataLoaders/CurrencyDataLoader.cs | 26 +++++++++------ .../DataLoaders/UnitConverterDataLoader.cs | 5 ++- .../ViewModels/DateCalculatorViewModel.cs | 10 +++--- .../ViewModels/UnitConverterViewModel.cs | 19 ++++++++--- .../Views/DateCalculator.xaml.cs | 8 +++-- 7 files changed, 84 insertions(+), 50 deletions(-) diff --git a/src/Calculator.Shared/Common/LocalizationService.cs b/src/Calculator.Shared/Common/LocalizationService.cs index 4b2e74e1..3ddd46f9 100644 --- a/src/Calculator.Shared/Common/LocalizationService.cs +++ b/src/Calculator.Shared/Common/LocalizationService.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.Text; using Windows.ApplicationModel.Resources.Core; using Windows.Globalization; @@ -366,14 +367,15 @@ namespace CalculatorApp.Common // as configured by running intl.cpl. public static DecimalFormatter GetRegionalSettingsAwareDecimalFormatter() { - IEnumerable languageIdentifiers = GetLanguageIdentifiers(); - if (languageIdentifiers != null) - { - return new DecimalFormatter(languageIdentifiers, GlobalizationPreferences.HomeGeographicRegion); - } + IEnumerable languageIdentifiers = GetLanguageIdentifiers(); + // TODO UNO + //if (languageIdentifiers != null) + //{ + // return new DecimalFormatter(languageIdentifiers, GlobalizationPreferences.HomeGeographicRegion); + //} - return new DecimalFormatter(); - } + return new DecimalFormatter(); + } // If successful, returns a formatter that respects the user's regional format settings, // as configured by running intl.cpl. @@ -400,13 +402,16 @@ namespace CalculatorApp.Common languageIdentifiers = ApplicationLanguages.Languages; } - return new DateTimeFormatter(format, languageIdentifiers, GlobalizationPreferences.HomeGeographicRegion, calendarIdentifier, clockIdentifier); - } + //TODO UNO: return new DateTimeFormatter(format, languageIdentifiers, GlobalizationPreferences.HomeGeographicRegion, calendarIdentifier, clockIdentifier); + return new DateTimeFormatter(format, languageIdentifiers); + } public static CurrencyFormatter GetRegionalSettingsAwareCurrencyFormatter() { - string userCurrency = - (GlobalizationPreferences.Currencies.Count > 0) ? GlobalizationPreferences.Currencies[0] : DefaultCurrencyCode; + // TOOD UNO + //string userCurrency = + // (GlobalizationPreferences.Currencies.Count > 0) ? GlobalizationPreferences.Currencies[0] : DefaultCurrencyCode; + string userCurrency = DefaultCurrencyCode; IEnumerable languageIdentifiers = GetLanguageIdentifiers(); if (languageIdentifiers == null) @@ -414,7 +419,8 @@ namespace CalculatorApp.Common languageIdentifiers = ApplicationLanguages.Languages; } - var currencyFormatter = new CurrencyFormatter(userCurrency, languageIdentifiers, GlobalizationPreferences.HomeGeographicRegion); + // TODO UNO: var currencyFormatter = new CurrencyFormatter(userCurrency, languageIdentifiers, GlobalizationPreferences.HomeGeographicRegion); + var currencyFormatter = new CurrencyFormatter(userCurrency); int fractionDigits = LocalizationSettings.GetInstance().GetCurrencyTrailingDigits(); currencyFormatter.FractionDigits = fractionDigits; @@ -424,7 +430,8 @@ namespace CalculatorApp.Common static IEnumerable GetLanguageIdentifiers() { - return GlobalizationPreferences.Languages; + // TODO UNO: return GlobalizationPreferences.Languages; + return new[] { CultureInfo.CurrentCulture.IetfLanguageTag }; } // Resources for the engine use numbers as keys. It's inconvenient, but also difficult to diff --git a/src/Calculator.Shared/Common/NetworkManager.cs b/src/Calculator.Shared/Common/NetworkManager.cs index 8361c09e..83a6c2a5 100644 --- a/src/Calculator.Shared/Common/NetworkManager.cs +++ b/src/Calculator.Shared/Common/NetworkManager.cs @@ -23,29 +23,34 @@ namespace CalculatorApp public NetworkManager() { - NetworkInformation.NetworkStatusChanged += new NetworkStatusChangedEventHandler(OnNetworkStatusChange); + // TODO: UNO (Not implemented) + //NetworkInformation.NetworkStatusChanged += new NetworkStatusChangedEventHandler(OnNetworkStatusChange); } ~NetworkManager() { - NetworkInformation.NetworkStatusChanged -= OnNetworkStatusChange; + // TODO: UNO (Not implemented) + //NetworkInformation.NetworkStatusChanged -= OnNetworkStatusChange; } public static NetworkAccessBehavior GetNetworkAccessBehavior() { - NetworkAccessBehavior behavior = NetworkAccessBehavior.Offline; - ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile(); - if (connectionProfile != null) - { - NetworkConnectivityLevel connectivityLevel = connectionProfile.GetNetworkConnectivityLevel(); - if (connectivityLevel == NetworkConnectivityLevel.InternetAccess || connectivityLevel == NetworkConnectivityLevel.ConstrainedInternetAccess) - { - ConnectionCost connectionCost = connectionProfile.GetConnectionCost(); - behavior = ConvertCostInfoToBehavior(connectionCost); - } - } + // TODO: UNO (Not implemented) + return NetworkAccessBehavior.Normal; - return behavior; + //NetworkAccessBehavior behavior = NetworkAccessBehavior.Offline; + //ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile(); + //if (connectionProfile != null) + //{ + // NetworkConnectivityLevel connectivityLevel = connectionProfile.GetNetworkConnectivityLevel(); + // if (connectivityLevel == NetworkConnectivityLevel.InternetAccess || connectivityLevel == NetworkConnectivityLevel.ConstrainedInternetAccess) + // { + // ConnectionCost connectionCost = connectionProfile.GetConnectionCost(); + // behavior = ConvertCostInfoToBehavior(connectionCost); + // } + //} + + //return behavior; } void OnNetworkStatusChange(object sender) diff --git a/src/Calculator.Shared/DataLoaders/CurrencyDataLoader.cs b/src/Calculator.Shared/DataLoaders/CurrencyDataLoader.cs index 25795257..933743bd 100644 --- a/src/Calculator.Shared/DataLoaders/CurrencyDataLoader.cs +++ b/src/Calculator.Shared/DataLoaders/CurrencyDataLoader.cs @@ -21,6 +21,7 @@ using SelectedUnits = System.Collections.Generic.KeyValuePair; using CategorySelectionInitializer = System.Tuple, UnitConversionManager.Unit, UnitConversionManager.Unit>; using UnitToUnitToConversionDataMap = System.Collections.Generic.Dictionary>; using CategoryToUnitVectorMap = System.Collections.Generic.Dictionary>; +using System.Globalization; namespace CalculatorApp.ViewModel { @@ -140,10 +141,12 @@ namespace CalculatorApp.ViewModel this.m_meteredOverrideSet = false; - if (GlobalizationPreferences.Languages.Count > 0) - { - m_responseLanguage = GlobalizationPreferences.Languages[0]; - } + // TODO UNO + //if (GlobalizationPreferences.Languages.Count > 0) + //{ + // m_responseLanguage = GlobalizationPreferences.Languages[0]; + //} + m_responseLanguage = CultureInfo.CurrentCulture.IetfLanguageTag; if (m_client != null) { @@ -151,11 +154,13 @@ namespace CalculatorApp.ViewModel m_client.SetResponseLanguage(m_responseLanguage); } - if (CoreWindow.GetForCurrentThread() != null) - { - // Must have a CoreWindow to access the resource context. - m_isRtlLanguage = LocalizationService.GetInstance().IsRtlLayout(); - } + // TODO UNO + //if (CoreWindow.GetForCurrentThread() != null) + //{ + // // Must have a CoreWindow to access the resource context. + // m_isRtlLanguage = LocalizationService.GetInstance().IsRtlLayout(); + //} + m_isRtlLanguage = true; m_ratioFormatter = LocalizationService.GetRegionalSettingsAwareDecimalFormatter(); m_ratioFormatter.IsGrouped = true; @@ -311,7 +316,8 @@ namespace CalculatorApp.ViewModel double rounded = (int)(ratio * (int)(scale)) / scale; string digitSymbol = LocalizationSettings.GetInstance().GetDigitSymbolFromEnUsDigit('1').ToString(); - string roundedFormat = m_ratioFormatter.Format(rounded); + // TODO UNO: string roundedFormat = m_ratioFormatter.Format(rounded); + string roundedFormat = rounded.ToString(CultureInfo.CurrentCulture); string ratioString = LocalizationStringUtil.GetLocalizedString( m_ratioFormat, diff --git a/src/Calculator.Shared/DataLoaders/UnitConverterDataLoader.cs b/src/Calculator.Shared/DataLoaders/UnitConverterDataLoader.cs index 3f50eb12..ef3ae72b 100644 --- a/src/Calculator.Shared/DataLoaders/UnitConverterDataLoader.cs +++ b/src/Calculator.Shared/DataLoaders/UnitConverterDataLoader.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.Linq; using System.Text; using Windows.Globalization; @@ -114,7 +115,9 @@ namespace CalculatorApp.ViewModel public UnitConverterDataLoader(GeographicRegion region) { - m_currentRegionCode = region.CodeTwoLetter; + // TODO UNO + //m_currentRegionCode = region.CodeTwoLetter; + m_currentRegionCode = new RegionInfo(CultureInfo.CurrentCulture.LCID).TwoLetterISORegionName; m_categoryList = new CalculatorList(); m_categoryToUnits = new CategoryToUnitVectorMap(EqualityComparer.Default); diff --git a/src/Calculator.Shared/ViewModels/DateCalculatorViewModel.cs b/src/Calculator.Shared/ViewModels/DateCalculatorViewModel.cs index 11c385a2..73eb293d 100644 --- a/src/Calculator.Shared/ViewModels/DateCalculatorViewModel.cs +++ b/src/Calculator.Shared/ViewModels/DateCalculatorViewModel.cs @@ -13,6 +13,7 @@ using CalculatorApp.Common.DateCalculation; using Windows.Foundation; using System.Numerics; using CalculatorApp; +using System.Globalization; namespace CalculatorApp.ViewModel { @@ -70,7 +71,7 @@ namespace CalculatorApp.ViewModel m_dateCalcEngine = new DateCalculationEngine(localizationSettings.GetCalendarIdentifier()); // Initialize dates of DatePicker controls to today's date - var calendar = new Calendar(); + var calendar = new Windows.Globalization.Calendar(); var today = calendar.GetDateTime().DateTime; // FromDate and ToDate should be clipped (adjusted to a consistent hour in UTC) @@ -126,7 +127,7 @@ namespace CalculatorApp.ViewModel PropertyChanged += (snd, e) => ((DateCalculatorViewModel)snd).OnPropertyChanged(e.PropertyName); } - static void CheckClipTimeSameDay(Calendar reference) + static void CheckClipTimeSameDay(Windows.Globalization.Calendar reference) { } @@ -473,7 +474,8 @@ namespace CalculatorApp.ViewModel else { // Display the resulting date in long format - StrDateResult = m_dateTimeFormatter.Format(DateResult); + //TODO UNO: StrDateResult = m_dateTimeFormatter.Format(DateResult); + StrDateResult = DateResult.ToString(CultureInfo.CurrentCulture); } } } @@ -637,7 +639,7 @@ namespace CalculatorApp.ViewModel private static DateTime ClipTime(DateTime dateTime) { - var calendar = new Calendar(); + var calendar = new Windows.Globalization.Calendar(); calendar.SetDateTime(dateTime); calendar.Period = 1; calendar.Hour = 12; diff --git a/src/Calculator.Shared/ViewModels/UnitConverterViewModel.cs b/src/Calculator.Shared/ViewModels/UnitConverterViewModel.cs index 2c7eb3da..fd6ebee9 100644 --- a/src/Calculator.Shared/ViewModels/UnitConverterViewModel.cs +++ b/src/Calculator.Shared/ViewModels/UnitConverterViewModel.cs @@ -29,6 +29,7 @@ using UCM = UnitConversionManager; using CategorySelectionInitializer = System.Tuple, UnitConversionManager.Unit, UnitConversionManager.Unit>; using UnitToUnitToConversionDataMap = System.Collections.Generic.Dictionary>; using CategoryToUnitVectorMap = System.Collections.Generic.Dictionary>; +using System.Globalization; namespace CalculatorApp.ViewModel { @@ -885,7 +886,8 @@ namespace CalculatorApp.ViewModel m_currencyFormatter.IsGrouped = true; m_currencyFormatter.Mode = CurrencyFormatterMode.UseCurrencyCode; m_currencyFormatter.ApplyRoundingForCurrency(RoundingAlgorithm.RoundHalfDown); - m_currencyMaxFractionDigits = m_currencyFormatter.FractionDigits; + //TODO UNO: m_currencyMaxFractionDigits = m_currencyFormatter.FractionDigits; + m_currencyMaxFractionDigits = 2; var resourceLoader = AppResourceProvider.GetInstance(); m_localizedValueFromFormat = resourceLoader.GetResourceString(UnitConverterResourceKeys.ValueFromFormat); @@ -1086,8 +1088,11 @@ namespace CalculatorApp.ViewModel if (IsCurrencyCurrentCategory) { - string currencyResult = m_currencyFormatter.Format(System.Convert.ToDouble(stringToLocalize)); - string currencyCode = m_currencyFormatter.Currency; + // TODO UNO: + // string currencyResult = m_currencyFormatter.Format(System.Convert.ToDouble(stringToLocalize)); + // string currencyCode = m_currencyFormatter.Currency; + string currencyResult = System.Convert.ToDouble(stringToLocalize).ToString(CultureInfo.CurrentCulture); + string currencyCode = "TODO UNO"; // CurrencyFormatter always includes LangCode or Symbol. Make it include LangCode // because this includes a non-breaking space. Remove the LangCode. @@ -1105,7 +1110,9 @@ namespace CalculatorApp.ViewModel { // Convert the input string to double using stod // Then use the decimalFormatter to reformat the double to Platform String - result = m_decimalFormatter.Format(System.Convert.ToDouble(stringToLocalize)); + // TODO UNO + //result = m_decimalFormatter.Format(System.Convert.ToDouble(stringToLocalize)); + result = System.Convert.ToDouble(stringToLocalize).ToString(CultureInfo.CurrentCulture); } if (hasDecimal) @@ -1113,7 +1120,9 @@ namespace CalculatorApp.ViewModel // Since the output from GetLocaleInfoEx() and DecimalFormatter are differing for decimal string // we are adding the below work-around of editing the string returned by DecimalFormatter // and replacing the decimal separator with the one returned by GetLocaleInfoEx() - String formattedSampleString = m_decimalFormatter.Format(System.Convert.ToDouble("1.1")); + // TODO UNO + //String formattedSampleString = m_decimalFormatter.Format(System.Convert.ToDouble("1.1")); + String formattedSampleString = 1.1.ToString(CultureInfo.CurrentCulture); string formattedSampleWString = formattedSampleString; string resultWithDecimal = result; diff --git a/src/Calculator.Shared/Views/DateCalculator.xaml.cs b/src/Calculator.Shared/Views/DateCalculator.xaml.cs index 272ced89..edbb7944 100644 --- a/src/Calculator.Shared/Views/DateCalculator.xaml.cs +++ b/src/Calculator.Shared/Views/DateCalculator.xaml.cs @@ -14,6 +14,7 @@ using Windows.UI.Xaml.Automation.Peers; using Windows.UI.Xaml.Controls; using CalculatorApp.Common; using CalculatorApp.ViewModel; +using System.Globalization; // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 @@ -50,7 +51,7 @@ namespace CalculatorApp DateDiff_ToDate.Language = localizationSettings.GetLocaleName(); // Set Min and Max Dates according to the Gregorian Calendar(1601 & 9999) - var calendar = new Calendar(); + var calendar = new Windows.Globalization.Calendar(); var today = calendar.GetDateTime(); calendar.ChangeCalendarSystem(CalendarIdentifiers.Gregorian); @@ -77,9 +78,10 @@ namespace CalculatorApp DateDiff_FromDate.DateFormat = "day month year"; DateDiff_ToDate.DateFormat = "day month year"; - var placeholderText = dateTimeFormatter.Format(today); + //TODO UNO: var placeholderText = dateTimeFormatter.Format(today); + var placeholderText = today.ToString("day month year", CultureInfo.CurrentCulture); - DateDiff_FromDate.PlaceholderText = placeholderText; + DateDiff_FromDate.PlaceholderText = placeholderText; DateDiff_ToDate.PlaceholderText = placeholderText; // TODO UNO