From e2400cc55d452716ed6992b3f65f5de3a0725bda Mon Sep 17 00:00:00 2001 From: zhangdd_ewan Date: Fri, 1 Nov 2024 09:21:28 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=8F=B7=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangdd_ewan --- frameworks/intl/include/phone_number_format.h | 3 +- frameworks/intl/src/phone_number_format.cpp | 44 +++++++++++++++---- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/frameworks/intl/include/phone_number_format.h b/frameworks/intl/include/phone_number_format.h index 6d9017a9..ed34b158 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(const std::string &number); PhoneNumberUtil *util; std::unique_ptr formatter = nullptr; std::string country; diff --git a/frameworks/intl/src/phone_number_format.cpp b/frameworks/intl/src/phone_number_format.cpp index 560232a3..1d9aa029 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 { { '+', '+' }, { ' ', ' ' }, @@ -182,18 +185,43 @@ std::string PhoneNumberFormat::format(const std::string &number) formatted_number = GetAsYouTypeFormatResult(number); return PseudoLocalizationProcessor(formatted_number); } + formatted_number = formatNumber(number); + return PseudoLocalizationProcessor(formatted_number); +} + +std::string PhoneNumberFormat::formatNumber(const std::string &number) +{ + if (number.empty() || number.at(0) == '#' || number.at(0) == '*') { + return number; + } + std::string formattedNumber; i18n::phonenumbers::PhoneNumber phoneNumber; PhoneNumberUtil::ErrorType type = util->ParseAndKeepRawInput(number, country, &phoneNumber); if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) { - return PseudoLocalizationProcessor(""); + return ""; } - const std::string prefix = "106"; - if (number.compare(0, prefix.length(), prefix) == 0) { - util->FormatInOriginalFormat(phoneNumber, country, &formatted_number); + HILOG_ERROR_I18N("zdd ---%{public}s.", country.c_str()); + 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->Format(phoneNumber, phoneNumberFormat, &formatted_number); + util->FormatInOriginalFormat(phoneNumber, country, &formattedNumber); } - return PseudoLocalizationProcessor(formatted_number); + return formattedNumber; } std::string PhoneNumberFormat::GetAsYouTypeFormatResult(const std::string &number) @@ -257,7 +285,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 +312,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); -- Gitee From 52d45736e9bc2084c61892e38a72541436ef6244 Mon Sep 17 00:00:00 2001 From: zhangdd_ewan Date: Fri, 1 Nov 2024 09:23:29 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=8F=B7=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangdd_ewan --- frameworks/intl/src/phone_number_format.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/intl/src/phone_number_format.cpp b/frameworks/intl/src/phone_number_format.cpp index 1d9aa029..4837649a 100644 --- a/frameworks/intl/src/phone_number_format.cpp +++ b/frameworks/intl/src/phone_number_format.cpp @@ -200,7 +200,6 @@ std::string PhoneNumberFormat::formatNumber(const std::string &number) if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) { return ""; } - HILOG_ERROR_I18N("zdd ---%{public}s.", country.c_str()); 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) { -- Gitee From 3bc74302222d657a9179024c1a07dcca68bf8812 Mon Sep 17 00:00:00 2001 From: zhangdd_ewan Date: Fri, 1 Nov 2024 10:44:50 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=8F=B7=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangdd_ewan --- .../phonenumberformat_fuzzer/phonenumberformat_fuzzer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/intl/test/fuzztest/phonenumberformat_fuzzer/phonenumberformat_fuzzer.cpp b/frameworks/intl/test/fuzztest/phonenumberformat_fuzzer/phonenumberformat_fuzzer.cpp index 6105fea9..3fc42235 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); -- Gitee From 59f7ff46ca37908d714198f407a409676e7e8a6a Mon Sep 17 00:00:00 2001 From: zhangdd_ewan Date: Fri, 1 Nov 2024 16:26:18 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=8F=B7=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangdd_ewan --- frameworks/intl/test/unittest/i18n_test.cpp | 2 +- frameworks/intl/test/unittest/intl_test_extent.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/intl/test/unittest/i18n_test.cpp b/frameworks/intl/test/unittest/i18n_test.cpp index 7e50b69b..51ebaf9c 100644 --- a/frameworks/intl/test/unittest/i18n_test.cpp +++ b/frameworks/intl/test/unittest/i18n_test.cpp @@ -550,7 +550,7 @@ HWTEST_F(I18nTest, I18nFuncTest014, TestSize.Level1) EXPECT_EQ(replaceCity, "安徽省宣城市2"); std::string number192 = "19200707087"; std::string formattedNumber = formatter2->format(number192); - EXPECT_EQ(formattedNumber, "+86 192 0070 7087"); + EXPECT_EQ(formattedNumber, "192 0070 7087"); } /** diff --git a/frameworks/intl/test/unittest/intl_test_extent.cpp b/frameworks/intl/test/unittest/intl_test_extent.cpp index d7dbc203..e0b79818 100644 --- a/frameworks/intl/test/unittest/intl_test_extent.cpp +++ b/frameworks/intl/test/unittest/intl_test_extent.cpp @@ -252,7 +252,7 @@ HWTEST_F(IntlTest, IntlFuncTest0053, TestSize.Level1) EXPECT_TRUE(flag); std::string number2 = "+8618622350085"; std::string formatResult = phoneNumberFormat->format(number2); - EXPECT_EQ(formatResult, "186 2235 0085"); + EXPECT_EQ(formatResult, "+86 186 2235 0085"); std::string location4 = phoneNumberFormat->getLocationName("fake-number", "zh-CN"); EXPECT_EQ(location4, ""); std::string number3 = "1068195561"; -- Gitee From f42a105d262c69b53b9d4f7fad34ac1c44943afe Mon Sep 17 00:00:00 2001 From: zhangdd_ewan Date: Fri, 8 Nov 2024 18:10:48 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=8F=B7=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangdd_ewan --- frameworks/intl/include/phone_number_format.h | 4 +-- frameworks/intl/src/phone_number_format.cpp | 26 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/frameworks/intl/include/phone_number_format.h b/frameworks/intl/include/phone_number_format.h index ed34b158..1b85f9f6 100644 --- a/frameworks/intl/include/phone_number_format.h +++ b/frameworks/intl/include/phone_number_format.h @@ -54,7 +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(const std::string &number); + std::string formatNumber(i18n::phonenumbers::PhoneNumber &phoneNumber); PhoneNumberUtil *util; std::unique_ptr formatter = nullptr; std::string country; @@ -64,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 4837649a..7b0edc43 100644 --- a/frameworks/intl/src/phone_number_format.cpp +++ b/frameworks/intl/src/phone_number_format.cpp @@ -130,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)); @@ -185,21 +183,25 @@ std::string PhoneNumberFormat::format(const std::string &number) formatted_number = GetAsYouTypeFormatResult(number); return PseudoLocalizationProcessor(formatted_number); } - formatted_number = formatNumber(number); - return PseudoLocalizationProcessor(formatted_number); -} - -std::string PhoneNumberFormat::formatNumber(const std::string &number) -{ if (number.empty() || number.at(0) == '#' || number.at(0) == '*') { - return number; + return PseudoLocalizationProcessor(number); } - std::string formattedNumber; i18n::phonenumbers::PhoneNumber phoneNumber; PhoneNumberUtil::ErrorType type = util->ParseAndKeepRawInput(number, country, &phoneNumber); if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) { - return ""; + return PseudoLocalizationProcessor(""); } + 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) { -- Gitee From 11f496001470087a01fb8b1b0e897971b9a7a364 Mon Sep 17 00:00:00 2001 From: zhangdd_ewan Date: Tue, 12 Nov 2024 09:35:32 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E5=8F=B7=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangdd_ewan --- frameworks/intl/test/unittest/i18n_test.cpp | 2 +- frameworks/intl/test/unittest/intl_test_extent.cpp | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/frameworks/intl/test/unittest/i18n_test.cpp b/frameworks/intl/test/unittest/i18n_test.cpp index 51ebaf9c..7e50b69b 100644 --- a/frameworks/intl/test/unittest/i18n_test.cpp +++ b/frameworks/intl/test/unittest/i18n_test.cpp @@ -550,7 +550,7 @@ HWTEST_F(I18nTest, I18nFuncTest014, TestSize.Level1) EXPECT_EQ(replaceCity, "安徽省宣城市2"); std::string number192 = "19200707087"; std::string formattedNumber = formatter2->format(number192); - EXPECT_EQ(formattedNumber, "192 0070 7087"); + EXPECT_EQ(formattedNumber, "+86 192 0070 7087"); } /** diff --git a/frameworks/intl/test/unittest/intl_test_extent.cpp b/frameworks/intl/test/unittest/intl_test_extent.cpp index e0b79818..a14601bc 100644 --- a/frameworks/intl/test/unittest/intl_test_extent.cpp +++ b/frameworks/intl/test/unittest/intl_test_extent.cpp @@ -252,11 +252,13 @@ HWTEST_F(IntlTest, IntlFuncTest0053, TestSize.Level1) EXPECT_TRUE(flag); std::string number2 = "+8618622350085"; std::string formatResult = phoneNumberFormat->format(number2); - EXPECT_EQ(formatResult, "+86 186 2235 0085"); + EXPECT_EQ(formatResult, "186 2235 0085"); std::string location4 = phoneNumberFormat->getLocationName("fake-number", "zh-CN"); EXPECT_EQ(location4, ""); + std::unique_ptr phoneNumberFormat3 = + std::make_unique("zh-CN"); std::string number3 = "1068195561"; - std::string formatedStr = phoneNumberFormat->format(number3); + std::string formatedStr = phoneNumberFormat3->format(number3); EXPECT_EQ(formatedStr, "10 6819 5561"); } -- Gitee From 38f3d54506b7c397c8ada6584e5ce350c7bd6241 Mon Sep 17 00:00:00 2001 From: zhangdd_ewan Date: Tue, 12 Nov 2024 10:49:37 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E5=8F=B7=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangdd_ewan --- frameworks/intl/test/unittest/intl_test_extent.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/intl/test/unittest/intl_test_extent.cpp b/frameworks/intl/test/unittest/intl_test_extent.cpp index a14601bc..283b33e6 100644 --- a/frameworks/intl/test/unittest/intl_test_extent.cpp +++ b/frameworks/intl/test/unittest/intl_test_extent.cpp @@ -255,8 +255,9 @@ 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"); + std::make_unique("zh-CN", options3); std::string number3 = "1068195561"; std::string formatedStr = phoneNumberFormat3->format(number3); EXPECT_EQ(formatedStr, "10 6819 5561"); -- Gitee