From 0dd6aa0612fcbae6e3791751728f73fef4ad6f79 Mon Sep 17 00:00:00 2001 From: Rudy Huyn Date: Thu, 14 Mar 2019 02:10:30 -0700 Subject: [PATCH] Add 2 sort methods to LocalizationService --- .../Common/LocalizationService.cpp | 33 +++++++++++++++---- .../Common/LocalizationService.h | 19 +++++++++-- .../DataLoaders/CurrencyDataLoader.cpp | 29 ++++++++-------- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/CalcViewModel/Common/LocalizationService.cpp b/src/CalcViewModel/Common/LocalizationService.cpp index df1d85b3..fb482a86 100644 --- a/src/CalcViewModel/Common/LocalizationService.cpp +++ b/src/CalcViewModel/Common/LocalizationService.cpp @@ -62,9 +62,20 @@ LocalizationService::LocalizationService() m_flowDirection = ResourceContext::GetForCurrentView()->QualifierValues->Lookup(L"LayoutDirection") != L"LTR" ? FlowDirection::RightToLeft : FlowDirection::LeftToRight; + auto localeName = std::string(m_language->Begin(), m_language->End()); + localeName += ".UTF8"; + try + { + m_locale = locale(localeName); + } + catch(...) + { + m_locale = locale(""); + } auto resourceLoader = AppResourceProvider::GetInstance(); m_fontFamilyOverride = resourceLoader.GetResourceString(L"LocalizedFontFamilyOverride"); + String^ reserved = L"RESERVED_FOR_FONTLOC"; m_overrideFontApiValues = ((m_fontFamilyOverride != nullptr) && (m_fontFamilyOverride != reserved)); @@ -206,7 +217,7 @@ FontWeight LocalizationService::GetFontWeightOverride() double LocalizationService::GetFontScaleFactorOverride(LanguageFontType fontType) { assert(m_overrideFontApiValues); - + switch (fontType) { case LanguageFontType::UIText: @@ -271,12 +282,12 @@ void LocalizationService::UpdateFontFamilyAndSize(DependencyObject^ target) { control->FontSize = sizeToUse; } - else + else { control->ClearValue(Control::FontSizeProperty); } } - else + else { auto textBlock = dynamic_cast(target); if (textBlock) @@ -290,7 +301,7 @@ void LocalizationService::UpdateFontFamilyAndSize(DependencyObject^ target) { textBlock->FontSize = sizeToUse; } - else + else { textBlock->ClearValue(TextBlock::FontSizeProperty); } @@ -309,7 +320,7 @@ void LocalizationService::UpdateFontFamilyAndSize(DependencyObject^ target) { richTextBlock->FontSize = sizeToUse; } - else + else { richTextBlock->ClearValue(RichTextBlock::FontSizeProperty); } @@ -328,7 +339,7 @@ void LocalizationService::UpdateFontFamilyAndSize(DependencyObject^ target) { textElement->FontSize = sizeToUse; } - else + else { textElement->ClearValue(TextElement::FontSizeProperty); } @@ -552,3 +563,13 @@ String^ LocalizationService::GetNarratorReadableString(String^ rawString) return ref new String(readableString.str().c_str()); } + +void LocalizationService::Sort(std::vector& source) +{ + const collate& coll = use_facet>(m_locale); + sort(source.begin(), source.end(), [&coll](Platform::String^ str1, Platform::String^ str2) + { + return coll.compare(str1->Begin(), str1->End(), + str2->Begin(), str2->End()) < 0; + }); +} diff --git a/src/CalcViewModel/Common/LocalizationService.h b/src/CalcViewModel/Common/LocalizationService.h index 7560682d..8d11e0d0 100644 --- a/src/CalcViewModel/Common/LocalizationService.h +++ b/src/CalcViewModel/Common/LocalizationService.h @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once @@ -39,6 +39,21 @@ namespace CalculatorApp { namespace Common Windows::UI::Text::FontWeight GetFontWeightOverride(); double GetFontScaleFactorOverride(LanguageFontType fontType); + void Sort(std::vector& source); + + template + void Sort(std::vector& source, std::function func) + { + const collate& coll = use_facet>(m_locale); + sort(source.begin(), source.end(), [&coll, &func](T obj1, T obj2) + { + Platform::String^ str1 = func(obj1); + Platform::String^ str2 = func(obj2); + return coll.compare(str1->Begin(), str1->End(), + str2->Begin(), str2->End()) < 0; + }); + } + static Windows::Globalization::NumberFormatting::DecimalFormatter^ GetRegionalSettingsAwareDecimalFormatter(); static Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ GetRegionalSettingsAwareDateTimeFormatter(_In_ Platform::String^ format); static Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ GetRegionalSettingsAwareDateTimeFormatter( @@ -50,7 +65,6 @@ namespace CalculatorApp { namespace Common static Platform::String^ GetNarratorReadableToken(Platform::String^ rawToken); static Platform::String^ GetNarratorReadableString(Platform::String^ rawString); - private: Windows::Globalization::Fonts::LanguageFont^ GetLanguageFont(LanguageFontType fontType); Windows::UI::Text::FontWeight ParseFontWeight(Platform::String^ fontWeight); @@ -79,6 +93,7 @@ namespace CalculatorApp { namespace Common Windows::UI::Text::FontWeight m_fontWeightOverride; double m_uiTextFontScaleFactorOverride; double m_uiCaptionFontScaleFactorOverride; + std::locale m_locale; }; }} diff --git a/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp b/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp index f42e3a73..cd215aba 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp +++ b/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "pch.h" @@ -9,7 +9,6 @@ #include "Common/LocalizationSettings.h" #include "Common/TraceLogger.h" #include "UnitConverterDataConstants.h" -#include using namespace CalculatorApp; using namespace CalculatorApp::Common; @@ -204,7 +203,7 @@ void CurrencyDataLoader::LoadData() } } - co_return didLoad; + co_return didLoad; }).then([this](bool didLoad) { UpdateDisplayedTimestamp(); @@ -330,7 +329,7 @@ task CurrencyDataLoader::TryLoadDataFromCacheAsync() { loadComplete = co_await TryLoadDataFromWebAsync(); } - + if (!loadComplete) { loadComplete = co_await TryFinishLoadFromCacheAsync(); @@ -505,6 +504,12 @@ bool CurrencyDataLoader::TryParseWebResponses( && TryParseAllRatiosData(allRatiosJson, allRatiosData); } +Platform::String ^ te(const UCM::CurrencyStaticData& s) +{ + return ref new Platform::String(s.countryName.c_str()); +}; + + bool CurrencyDataLoader::TryParseStaticData(_In_ String^ rawJson, _Inout_ vector& staticData) { JsonArray^ data = nullptr; @@ -547,17 +552,11 @@ bool CurrencyDataLoader::TryParseStaticData(_In_ String^ rawJson, _Inout_ vector }; } - // TODO - MSFT 8533667: this sort will be replaced by a WinRT call to sort localized strings + auto sortCurrencyNames = [](UCM::CurrencyStaticData s) { + return ref new Platform::String(s.countryName.c_str()); + }; - auto loc = std::locale(""); //Use the user-preferred locale to sort country names - const std::collate& coll = std::use_facet >(loc); - sort(begin(staticData), end(staticData), [&coll](CurrencyStaticData unit1, CurrencyStaticData unit2) - { - auto country1 = unit1.countryName.data(); - auto country2 = unit2.countryName.data(); - return coll.compare(country1, country1 + unit1.countryName.length(), - country2, country2 + unit2.countryName.length()) < 0; - }); + LocalizationService::GetInstance()->Sort(staticData, sortCurrencyNames); return true; } @@ -585,7 +584,7 @@ bool CurrencyDataLoader::TryParseAllRatiosData(_In_ String^ rawJson, _Inout_ Cur relativeRatio, sourceCurrencyCode, targetCurrencyCode - }); + }); } return true;