Use the current locale to sort country names (#259)

This commit is contained in:
Rudy Huyn
2019-07-16 11:44:13 -07:00
committed by Pepe Rivera
parent c3d3581240
commit a35a030683
6 changed files with 204 additions and 19 deletions

View File

@@ -75,9 +75,24 @@ LocalizationService::LocalizationService(_In_ const wchar_t * const overridedLan
{
m_isLanguageOverrided = overridedLanguage != nullptr;
m_language = m_isLanguageOverrided ? ref new Platform::String(overridedLanguage) : ApplicationLanguages::Languages->GetAt(0);
m_flowDirection = ResourceContext::GetForCurrentView()->QualifierValues->Lookup(L"LayoutDirection")
m_flowDirection = ResourceContext::GetForViewIndependentUse()->QualifierValues->Lookup(L"LayoutDirection")
!= L"LTR" ? FlowDirection::RightToLeft : FlowDirection::LeftToRight;
wstring localeName = wstring(m_language->Data());
localeName += L".UTF8";
try
{
// Convert wstring to string for locale
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &localeName[0], (int)localeName.size(), NULL, 0, NULL, NULL);
string localeNameStr(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &localeName[0], (int)localeName.size(), &localeNameStr[0], size_needed, NULL, NULL);
m_locale = locale(localeNameStr.data());
}
catch (...)
{
m_locale = locale("");
}
auto resourceLoader = AppResourceProvider::GetInstance();
m_fontFamilyOverride = resourceLoader.GetResourceString(L"LocalizedFontFamilyOverride");
@@ -371,7 +386,7 @@ DecimalFormatter ^ LocalizationService::GetRegionalSettingsAwareDecimalFormatter
// as configured by running intl.cpl.
//
// This helper function creates a DateTimeFormatter with a TwentyFour hour clock
DateTimeFormatter ^ LocalizationService::GetRegionalSettingsAwareDateTimeFormatter(_In_ String^ format) const
DateTimeFormatter ^ LocalizationService::GetRegionalSettingsAwareDateTimeFormatter(_In_ String ^ format) const
{
IIterable<String ^> ^ languageIdentifiers = LocalizationService::GetLanguageIdentifiers();
if (languageIdentifiers == nullptr)
@@ -384,7 +399,7 @@ DateTimeFormatter ^ LocalizationService::GetRegionalSettingsAwareDateTimeFormatt
// If successful, returns a formatter that respects the user's regional format settings,
// as configured by running intl.cpl.
DateTimeFormatter^ LocalizationService::GetRegionalSettingsAwareDateTimeFormatter(_In_ String ^ format, _In_ String ^ calendarIdentifier, _In_ String ^ clockIdentifier) const
DateTimeFormatter ^ LocalizationService::GetRegionalSettingsAwareDateTimeFormatter(_In_ String ^ format, _In_ String ^ calendarIdentifier, _In_ String ^ clockIdentifier) const
{
IIterable<String ^> ^ languageIdentifiers = LocalizationService::GetLanguageIdentifiers();
if (languageIdentifiers == nullptr)
@@ -421,7 +436,7 @@ IIterable<String ^> ^ LocalizationService::GetLanguageIdentifiers() const
if (m_isLanguageOverrided)
{
auto overridedLanguageList = ref new Vector<String^>();
auto overridedLanguageList = ref new Vector<String ^>();
overridedLanguageList->Append(m_language);
return overridedLanguageList;
}
@@ -562,3 +577,11 @@ 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

@@ -30,9 +30,9 @@ namespace CalculatorApp
DEPENDENCY_PROPERTY_ATTACHED_WITH_DEFAULT_AND_CALLBACK(LanguageFontType, FontType, LanguageFontType::UIText);
DEPENDENCY_PROPERTY_ATTACHED_WITH_CALLBACK(double, FontSize);
internal:
static LocalizationService^ GetInstance();
static void OverrideWithLanguage(_In_ const wchar_t * const language);
internal:
static LocalizationService ^ GetInstance();
static void OverrideWithLanguage(_In_ const wchar_t* const language);
Windows::UI::Xaml::FlowDirection GetFlowDirection();
bool IsRtlLayout();
@@ -43,6 +43,19 @@ namespace CalculatorApp
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;
});
}
Windows::Globalization::NumberFormatting::DecimalFormatter ^ GetRegionalSettingsAwareDecimalFormatter() const;
Windows::Globalization::DateTimeFormatting::DateTimeFormatter ^ GetRegionalSettingsAwareDateTimeFormatter(_In_ Platform::String ^ format) const;
Windows::Globalization::DateTimeFormatting::DateTimeFormatter ^ GetRegionalSettingsAwareDateTimeFormatter(
@@ -76,15 +89,16 @@ namespace CalculatorApp
static LocalizationService ^ s_singletonInstance;
Windows::Globalization::Fonts::LanguageFontGroup ^ m_fontGroup;
Platform::String ^ m_language;
Windows::UI::Xaml::FlowDirection m_flowDirection;
bool m_overrideFontApiValues;
Platform::String ^ m_fontFamilyOverride;
bool m_isLanguageOverrided;
Windows::UI::Text::FontWeight m_fontWeightOverride;
double m_uiTextFontScaleFactorOverride;
double m_uiCaptionFontScaleFactorOverride;
Windows::Globalization::Fonts::LanguageFontGroup ^ m_fontGroup;
Platform::String ^ m_language;
Windows::UI::Xaml::FlowDirection m_flowDirection;
bool m_overrideFontApiValues;
Platform::String ^ m_fontFamilyOverride;
bool m_isLanguageOverrided;
Windows::UI::Text::FontWeight m_fontWeightOverride;
double m_uiTextFontScaleFactorOverride;
double m_uiCaptionFontScaleFactorOverride;
std::locale m_locale;
};
}