From 853704c1c24f6f62e24e485b27a2208ee9606a97 Mon Sep 17 00:00:00 2001 From: Seulgi Kim Date: Tue, 16 Apr 2019 17:53:08 -0700 Subject: [PATCH] Add support for Pyeong, a Korean floorspace unit. (#444) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #382 Description of the changes: Add Pyeong as an Area conversion unit. Pyeong shows up only if the user's current region is Korea ( i.e. region is either KP or KR ). Added Korean translation for Pyeong (평). For other locales, we default to English ( Pyeong ). How changes were validated: Manually tested the below For non-Korean regions, Pyeong does not show up. Korean region with Korean locale => Pyeong shows up and Pyeong is correctly translated. pyeong_Korean Korean region with English locale => Pyeong shows up and Pyeong is in English. pyeong_English Korean region with simplified Chinese locale => Pyeong shows up and Pyeong is in English. pyeong_Chinese --- src/CalcManager/UnitConverter.cpp | 26 ++++++++++++++++--- .../DataLoaders/UnitConverterDataConstants.h | 5 ++-- .../DataLoaders/UnitConverterDataLoader.cpp | 24 ++++++++++++++--- src/Calculator/Resources/en-US/Resources.resw | 10 ++++++- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/CalcManager/UnitConverter.cpp b/src/CalcManager/UnitConverter.cpp index 718e47f4..c1e4cd16 100644 --- a/src/CalcManager/UnitConverter.cpp +++ b/src/CalcManager/UnitConverter.cpp @@ -404,12 +404,30 @@ void UnitConverter::RestoreUserPreferences(const wstring& userPreferences) } vector outerTokens = StringToVector(userPreferences, L"|"); - if (outerTokens.size() == 3) + if (outerTokens.size() != 3) { - m_fromType = StringToUnit(outerTokens[0]); - m_toType = StringToUnit(outerTokens[1]); - m_currentCategory = StringToCategory(outerTokens[2]); + return; } + + auto fromType = StringToUnit(outerTokens[0]); + auto toType = StringToUnit(outerTokens[1]); + m_currentCategory = StringToCategory(outerTokens[2]); + + // Only restore from the saved units if they are valid in the current available units. + auto itr = m_categoryToUnits.find(m_currentCategory); + if (itr != m_categoryToUnits.end()) + { + const auto& curUnits = itr->second; + if (find(curUnits.begin(), curUnits.end(), fromType) != curUnits.end()) + { + m_fromType = fromType; + } + if (find(curUnits.begin(), curUnits.end(), toType) != curUnits.end()) + { + m_toType = toType; + } + } + } /// diff --git a/src/CalcViewModel/DataLoaders/UnitConverterDataConstants.h b/src/CalcViewModel/DataLoaders/UnitConverterDataConstants.h index bd8cb169..7032b250 100644 --- a/src/CalcViewModel/DataLoaders/UnitConverterDataConstants.h +++ b/src/CalcViewModel/DataLoaders/UnitConverterDataConstants.h @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. namespace CalculatorApp @@ -162,7 +162,8 @@ namespace CalculatorApp Data_Zebibytes = UnitStart + 162, Data_Zetabits = UnitStart + 163, Data_Zetabytes = UnitStart + 164, - UnitEnd = Data_Zetabytes + Area_Pyeong = UnitStart + 165, + UnitEnd = Area_Pyeong }; } } diff --git a/src/CalcViewModel/DataLoaders/UnitConverterDataLoader.cpp b/src/CalcViewModel/DataLoaders/UnitConverterDataLoader.cpp index 45411c1d..c95fda5a 100644 --- a/src/CalcViewModel/DataLoaders/UnitConverterDataLoader.cpp +++ b/src/CalcViewModel/DataLoaders/UnitConverterDataLoader.cpp @@ -118,12 +118,19 @@ void UnitConverterDataLoader::LoadData() unordered_map unitConversions = categoryToUnitConversionDataMap.at(categoryViewMode); double unitFactor = unitConversions[unit.id]; - for (auto itr = unitConversions.begin(); itr != unitConversions.end(); ++itr) + for (const auto&[id, conversionFactor] : unitConversions) { + if (idToUnit.find(id) == idToUnit.end()) + { + // Optional units will not be in idToUnit but can be in unitConversions. + // For optional units that did not make it to the current set of units, just continue. + continue; + } + UCM::ConversionData parsedData = { 1.0, 0.0, false }; - assert(itr->second > 0); // divide by zero assert - parsedData.ratio = unitFactor / itr->second; - conversions.insert(pair(idToUnit.at(itr->first), parsedData)); + assert(conversionFactor > 0); // divide by zero assert + parsedData.ratio = unitFactor / conversionFactor; + conversions.insert(pair(idToUnit.at(id), parsedData)); } } else @@ -175,6 +182,10 @@ void UnitConverterDataLoader::GetUnits(_In_ unordered_map areaUnits; areaUnits.push_back(OrderedUnit{ UnitConverterUnits::Area_Acre, GetLocalizedStringName(L"UnitName_Acre"), GetLocalizedStringName(L"UnitAbbreviation_Acre"), 9 }); areaUnits.push_back(OrderedUnit{ UnitConverterUnits::Area_Hectare, GetLocalizedStringName(L"UnitName_Hectare"), GetLocalizedStringName(L"UnitAbbreviation_Hectare"), 4 }); @@ -190,6 +201,10 @@ void UnitConverterDataLoader::GetUnits(_In_ unordered_map dataUnits; @@ -384,6 +399,7 @@ void UnitConverterDataLoader::GetConversionData(_In_ unordered_mapMicrosoft Services Agreement Displayed on a link to the Microsoft Services Agreement in the about this app information - + + Pyeong + An abbreviation for a measurement unit of area. + + + Pyeong + A measurement unit for area. + + \ No newline at end of file