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")
!= 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));
@ -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;
});
}

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.
#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;
};
}}

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.
#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;
@ -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;
}