Add 2 sort methods to LocalizationService

This commit is contained in:
Rudy Huyn 2019-03-14 02:10:30 -07:00
commit 0dd6aa0612
3 changed files with 58 additions and 23 deletions

View file

@ -62,9 +62,20 @@ LocalizationService::LocalizationService()
m_flowDirection = ResourceContext::GetForCurrentView()->QualifierValues->Lookup(L"LayoutDirection") m_flowDirection = ResourceContext::GetForCurrentView()->QualifierValues->Lookup(L"LayoutDirection")
!= L"LTR" ? FlowDirection::RightToLeft : FlowDirection::LeftToRight; != 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(); auto resourceLoader = AppResourceProvider::GetInstance();
m_fontFamilyOverride = resourceLoader.GetResourceString(L"LocalizedFontFamilyOverride"); m_fontFamilyOverride = resourceLoader.GetResourceString(L"LocalizedFontFamilyOverride");
String^ reserved = L"RESERVED_FOR_FONTLOC"; String^ reserved = L"RESERVED_FOR_FONTLOC";
m_overrideFontApiValues = ((m_fontFamilyOverride != nullptr) && (m_fontFamilyOverride != reserved)); m_overrideFontApiValues = ((m_fontFamilyOverride != nullptr) && (m_fontFamilyOverride != reserved));
@ -206,7 +217,7 @@ FontWeight LocalizationService::GetFontWeightOverride()
double LocalizationService::GetFontScaleFactorOverride(LanguageFontType fontType) double LocalizationService::GetFontScaleFactorOverride(LanguageFontType fontType)
{ {
assert(m_overrideFontApiValues); assert(m_overrideFontApiValues);
switch (fontType) switch (fontType)
{ {
case LanguageFontType::UIText: case LanguageFontType::UIText:
@ -271,12 +282,12 @@ void LocalizationService::UpdateFontFamilyAndSize(DependencyObject^ target)
{ {
control->FontSize = sizeToUse; control->FontSize = sizeToUse;
} }
else else
{ {
control->ClearValue(Control::FontSizeProperty); control->ClearValue(Control::FontSizeProperty);
} }
} }
else else
{ {
auto textBlock = dynamic_cast<TextBlock^>(target); auto textBlock = dynamic_cast<TextBlock^>(target);
if (textBlock) if (textBlock)
@ -290,7 +301,7 @@ void LocalizationService::UpdateFontFamilyAndSize(DependencyObject^ target)
{ {
textBlock->FontSize = sizeToUse; textBlock->FontSize = sizeToUse;
} }
else else
{ {
textBlock->ClearValue(TextBlock::FontSizeProperty); textBlock->ClearValue(TextBlock::FontSizeProperty);
} }
@ -309,7 +320,7 @@ void LocalizationService::UpdateFontFamilyAndSize(DependencyObject^ target)
{ {
richTextBlock->FontSize = sizeToUse; richTextBlock->FontSize = sizeToUse;
} }
else else
{ {
richTextBlock->ClearValue(RichTextBlock::FontSizeProperty); richTextBlock->ClearValue(RichTextBlock::FontSizeProperty);
} }
@ -328,7 +339,7 @@ void LocalizationService::UpdateFontFamilyAndSize(DependencyObject^ target)
{ {
textElement->FontSize = sizeToUse; textElement->FontSize = sizeToUse;
} }
else else
{ {
textElement->ClearValue(TextElement::FontSizeProperty); textElement->ClearValue(TextElement::FontSizeProperty);
} }
@ -552,3 +563,13 @@ String^ LocalizationService::GetNarratorReadableString(String^ rawString)
return ref new String(readableString.str().c_str()); return ref new String(readableString.str().c_str());
} }
void LocalizationService::Sort(std::vector<Platform::String^>& source)
{
const collate<wchar_t>& coll = use_facet<collate<wchar_t>>(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;
});
}

View file

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
#pragma once #pragma once
@ -39,6 +39,21 @@ namespace CalculatorApp { namespace Common
Windows::UI::Text::FontWeight GetFontWeightOverride(); Windows::UI::Text::FontWeight GetFontWeightOverride();
double GetFontScaleFactorOverride(LanguageFontType fontType); double GetFontScaleFactorOverride(LanguageFontType fontType);
void Sort(std::vector<Platform::String^>& source);
template<typename T>
void Sort(std::vector<T>& source, std::function<Platform::String^(T)> func)
{
const collate<wchar_t>& coll = use_facet<collate<wchar_t>>(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::NumberFormatting::DecimalFormatter^ GetRegionalSettingsAwareDecimalFormatter();
static Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ GetRegionalSettingsAwareDateTimeFormatter(_In_ Platform::String^ format); static Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ GetRegionalSettingsAwareDateTimeFormatter(_In_ Platform::String^ format);
static Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ GetRegionalSettingsAwareDateTimeFormatter( static Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ GetRegionalSettingsAwareDateTimeFormatter(
@ -50,7 +65,6 @@ namespace CalculatorApp { namespace Common
static Platform::String^ GetNarratorReadableToken(Platform::String^ rawToken); static Platform::String^ GetNarratorReadableToken(Platform::String^ rawToken);
static Platform::String^ GetNarratorReadableString(Platform::String^ rawString); static Platform::String^ GetNarratorReadableString(Platform::String^ rawString);
private: private:
Windows::Globalization::Fonts::LanguageFont^ GetLanguageFont(LanguageFontType fontType); Windows::Globalization::Fonts::LanguageFont^ GetLanguageFont(LanguageFontType fontType);
Windows::UI::Text::FontWeight ParseFontWeight(Platform::String^ fontWeight); Windows::UI::Text::FontWeight ParseFontWeight(Platform::String^ fontWeight);
@ -79,6 +93,7 @@ namespace CalculatorApp { namespace Common
Windows::UI::Text::FontWeight m_fontWeightOverride; Windows::UI::Text::FontWeight m_fontWeightOverride;
double m_uiTextFontScaleFactorOverride; double m_uiTextFontScaleFactorOverride;
double m_uiCaptionFontScaleFactorOverride; double m_uiCaptionFontScaleFactorOverride;
std::locale m_locale;
}; };
}} }}

View file

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
#include "pch.h" #include "pch.h"
@ -9,7 +9,6 @@
#include "Common/LocalizationSettings.h" #include "Common/LocalizationSettings.h"
#include "Common/TraceLogger.h" #include "Common/TraceLogger.h"
#include "UnitConverterDataConstants.h" #include "UnitConverterDataConstants.h"
#include <locale>
using namespace CalculatorApp; using namespace CalculatorApp;
using namespace CalculatorApp::Common; using namespace CalculatorApp::Common;
@ -204,7 +203,7 @@ void CurrencyDataLoader::LoadData()
} }
} }
co_return didLoad; co_return didLoad;
}).then([this](bool didLoad) }).then([this](bool didLoad)
{ {
UpdateDisplayedTimestamp(); UpdateDisplayedTimestamp();
@ -330,7 +329,7 @@ task<bool> CurrencyDataLoader::TryLoadDataFromCacheAsync()
{ {
loadComplete = co_await TryLoadDataFromWebAsync(); loadComplete = co_await TryLoadDataFromWebAsync();
} }
if (!loadComplete) if (!loadComplete)
{ {
loadComplete = co_await TryFinishLoadFromCacheAsync(); loadComplete = co_await TryFinishLoadFromCacheAsync();
@ -505,6 +504,12 @@ bool CurrencyDataLoader::TryParseWebResponses(
&& TryParseAllRatiosData(allRatiosJson, allRatiosData); && 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<UCM::CurrencyStaticData>& staticData) bool CurrencyDataLoader::TryParseStaticData(_In_ String^ rawJson, _Inout_ vector<UCM::CurrencyStaticData>& staticData)
{ {
JsonArray^ data = nullptr; 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 LocalizationService::GetInstance()->Sort<UCM::CurrencyStaticData>(staticData, sortCurrencyNames);
const std::collate<wchar_t>& coll = std::use_facet<std::collate<wchar_t> >(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;
});
return true; return true;
} }
@ -585,7 +584,7 @@ bool CurrencyDataLoader::TryParseAllRatiosData(_In_ String^ rawJson, _Inout_ Cur
relativeRatio, relativeRatio,
sourceCurrencyCode, sourceCurrencyCode,
targetCurrencyCode targetCurrencyCode
}); });
} }
return true; return true;