mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 06:13:14 -07:00
Add 2 sort methods to LocalizationService
This commit is contained in:
parent
3a02f6e1d4
commit
0dd6aa0612
3 changed files with 58 additions and 23 deletions
|
@ -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<TextBlock^>(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<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;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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<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::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;
|
||||
};
|
||||
|
||||
}}
|
||||
|
|
|
@ -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 <locale>
|
||||
|
||||
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<bool> 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<UCM::CurrencyStaticData>& 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<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;
|
||||
});
|
||||
LocalizationService::GetInstance()->Sort<UCM::CurrencyStaticData>(staticData, sortCurrencyNames);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -585,7 +584,7 @@ bool CurrencyDataLoader::TryParseAllRatiosData(_In_ String^ rawJson, _Inout_ Cur
|
|||
relativeRatio,
|
||||
sourceCurrencyCode,
|
||||
targetCurrencyCode
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue