diff --git a/frameworks/intl/include/phone_number_format.h b/frameworks/intl/include/phone_number_format.h index 6d9017a929a06e5bb037a61a673ed338db7e6a94..1b85f9f680bebecbeb52456600bbe7b5a74f36af 100644 --- a/frameworks/intl/include/phone_number_format.h +++ b/frameworks/intl/include/phone_number_format.h @@ -43,7 +43,7 @@ public: const std::map &options); std::string getLocationName(const std::string &number, const std::string &locale); std::string getPhoneLocationName(const std::string& number, const std::string& phoneLocale, - const std::string& displayLocale, const std::string& regionCode); + const std::string& displayLocale); virtual bool getBlockedRegionName(const std::string& regionCode, const std::string& language); virtual std::string getCityName(const std::string& language, const std::string& phonenumber, const std::string& locationName); @@ -54,6 +54,7 @@ private: void OpenHandler(); std::string GetAsYouTypeFormatResult(const std::string &number); std::string FormatAllInputNumber(const std::string &originalNumber, std::string &replacedNumber); + std::string formatNumber(i18n::phonenumbers::PhoneNumber &phoneNumber); PhoneNumberUtil *util; std::unique_ptr formatter = nullptr; std::string country; @@ -63,7 +64,7 @@ private: static std::mutex AS_YOU_TYPE_FORMAT_MUTEX; static size_t MAX_NUMBER_LENGTH; std::string lastFormatNumber; - bool withOptions = false; + bool withFormatType = false; static std::map VALID_PHONE_NUMBER_CHARS; }; } // namespace I18n diff --git a/frameworks/intl/src/phone_number_format.cpp b/frameworks/intl/src/phone_number_format.cpp index 560232a3d114b8ae4ce28569f9b56488bb9f8650..7b0edc432a899f28bb5652894b457bceade18ad5 100644 --- a/frameworks/intl/src/phone_number_format.cpp +++ b/frameworks/intl/src/phone_number_format.cpp @@ -34,10 +34,13 @@ namespace Global { namespace I18n { const int RECV_CHAR_LEN = 128; using i18n::phonenumbers::PhoneNumberUtil; +using i18n::phonenumbers::PhoneNumber_CountryCodeSource::PhoneNumber_CountryCodeSource_FROM_NUMBER_WITH_PLUS_SIGN; void* PhoneNumberFormat::dynamicHandler = nullptr; std::mutex PhoneNumberFormat::phoneMutex; std::mutex PhoneNumberFormat::AS_YOU_TYPE_FORMAT_MUTEX; size_t PhoneNumberFormat::MAX_NUMBER_LENGTH = 30; +static const std::string KOREA_ISO_COUNTRY_CODE = "KR"; +static const std::string JAPAN_ISO_COUNTRY_CODE = "JP"; std::map PhoneNumberFormat::VALID_PHONE_NUMBER_CHARS { { '+', '+' }, { ' ', ' ' }, @@ -127,10 +130,8 @@ PhoneNumberFormat::PhoneNumberFormat(const std::string &countryTag, std::set validType = {"E164", "RFC3966", "INTERNATIONAL", "NATIONAL"}; if (validType.find(type) != validType.end()) { - withOptions = true; + withFormatType = true; phoneNumberFormat = type2PhoneNumberFormat[type]; - } else { - phoneNumberFormat = PhoneNumberUtil::PhoneNumberFormat::NATIONAL; } if (type.compare("TYPING") == 0) { formatter = std::unique_ptr(util->GetAsYouTypeFormatter(country)); @@ -182,20 +183,48 @@ std::string PhoneNumberFormat::format(const std::string &number) formatted_number = GetAsYouTypeFormatResult(number); return PseudoLocalizationProcessor(formatted_number); } + if (number.empty() || number.at(0) == '#' || number.at(0) == '*') { + return PseudoLocalizationProcessor(number); + } i18n::phonenumbers::PhoneNumber phoneNumber; PhoneNumberUtil::ErrorType type = util->ParseAndKeepRawInput(number, country, &phoneNumber); if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) { return PseudoLocalizationProcessor(""); } - const std::string prefix = "106"; - if (number.compare(0, prefix.length(), prefix) == 0) { - util->FormatInOriginalFormat(phoneNumber, country, &formatted_number); - } else { + if (withFormatType) { util->Format(phoneNumber, phoneNumberFormat, &formatted_number); + } else { + formatted_number = formatNumber(phoneNumber); } return PseudoLocalizationProcessor(formatted_number); } +std::string PhoneNumberFormat::formatNumber(i18n::phonenumbers::PhoneNumber &phoneNumber) +{ + std::string formattedNumber; + if (KOREA_ISO_COUNTRY_CODE.compare(country) == 0 && + phoneNumber.country_code() == util->GetCountryCodeForRegion(KOREA_ISO_COUNTRY_CODE) && + phoneNumber.country_code_source() == PhoneNumber_CountryCodeSource_FROM_NUMBER_WITH_PLUS_SIGN) { + /** + * Need to reformat any local Korean phone numbers (when the user is in Korea) with + * country code to corresponding national format which would replace the leading + * +82 with 0. + */ + util->Format(phoneNumber, PhoneNumberUtil::PhoneNumberFormat::NATIONAL, &formattedNumber); + } else if (JAPAN_ISO_COUNTRY_CODE.compare(country) == 0 && + phoneNumber.country_code() == util->GetCountryCodeForRegion(JAPAN_ISO_COUNTRY_CODE) && + phoneNumber.country_code_source() == PhoneNumber_CountryCodeSource_FROM_NUMBER_WITH_PLUS_SIGN) { + /** + * Need to reformat Japanese phone numbers (when the user is in Japan) with the national + * dialing format. + */ + util->Format(phoneNumber, PhoneNumberUtil::PhoneNumberFormat::NATIONAL, &formattedNumber); + } else { + util->FormatInOriginalFormat(phoneNumber, country, &formattedNumber); + } + return formattedNumber; +} + std::string PhoneNumberFormat::GetAsYouTypeFormatResult(const std::string &number) { std::lock_guard formatLock(AS_YOU_TYPE_FORMAT_MUTEX); @@ -257,7 +286,7 @@ std::string PhoneNumberFormat::getLocationName( std::string regionCode; util->GetRegionCodeForNumber(phoneNumber, ®ionCode); - std::string locName = getPhoneLocationName(number, phoneLocale.getName(), locale, regionCode); + std::string locName = getPhoneLocationName(number, phoneLocale.getName(), locale); icu::LocaleBuilder regionbuilder = icu::LocaleBuilder().setRegion(regionCode); icu::Locale regionLocale = builder.build(status); @@ -284,7 +313,7 @@ std::string PhoneNumberFormat::getLocationName( std::string PhoneNumberFormat::getPhoneLocationName( const std::string& number, const std::string& phoneLocale, - const std::string& displayLocale, const std::string& regionCode) + const std::string& displayLocale) { OpenHandler(); std::lock_guard phoneLock(phoneMutex); diff --git a/frameworks/intl/test/fuzztest/phonenumberformat_fuzzer/phonenumberformat_fuzzer.cpp b/frameworks/intl/test/fuzztest/phonenumberformat_fuzzer/phonenumberformat_fuzzer.cpp index 6105fea9a80a771c8ebfd103a1dca225da6d0160..3fc42235cc9aa657afcea24f07d3c1cc714436cf 100644 --- a/frameworks/intl/test/fuzztest/phonenumberformat_fuzzer/phonenumberformat_fuzzer.cpp +++ b/frameworks/intl/test/fuzztest/phonenumberformat_fuzzer/phonenumberformat_fuzzer.cpp @@ -34,7 +34,7 @@ namespace OHOS { formatter.isValidPhoneNumber(input); formatter.format(input); formatter.getLocationName(input, input); - formatter.getPhoneLocationName(input, input, input, input); + formatter.getPhoneLocationName(input, input, input); formatter.getBlockedRegionName(input, input); formatter.getCityName(input, input, input); PhoneNumberFormat::CreateInstance(input, options); diff --git a/frameworks/intl/test/unittest/intl_test_extent.cpp b/frameworks/intl/test/unittest/intl_test_extent.cpp index d7dbc2037cf021cbf45af0efaee18c4260ae1bdd..283b33e635110212adb1d3226419ce4e33eecd4c 100644 --- a/frameworks/intl/test/unittest/intl_test_extent.cpp +++ b/frameworks/intl/test/unittest/intl_test_extent.cpp @@ -255,8 +255,11 @@ HWTEST_F(IntlTest, IntlFuncTest0053, TestSize.Level1) EXPECT_EQ(formatResult, "186 2235 0085"); std::string location4 = phoneNumberFormat->getLocationName("fake-number", "zh-CN"); EXPECT_EQ(location4, ""); + map options3 = {}; + std::unique_ptr phoneNumberFormat3 = + std::make_unique("zh-CN", options3); std::string number3 = "1068195561"; - std::string formatedStr = phoneNumberFormat->format(number3); + std::string formatedStr = phoneNumberFormat3->format(number3); EXPECT_EQ(formatedStr, "10 6819 5561"); }