mirror of
https://gitee.com/openharmony/global_i18n
synced 2024-11-30 18:51:33 +00:00
update diff
Signed-off-by: zhangdd_ewan <zhangdongdong13@h-partners.com>
This commit is contained in:
parent
626246393e
commit
e9baa07b3a
@ -1,52 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_GLOBAL_I18N_PHONE_NUMBER_FORMAT_H
|
||||
#define OHOS_GLOBAL_I18N_PHONE_NUMBER_FORMAT_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "cpp/src/phonenumbers/phonenumberutil.h"
|
||||
#include "cpp/src/phonenumbers/geocoding/phonenumber_offline_geocoder.h"
|
||||
#include "cpp/src/phonenumbers/base/memory/scoped_ptr.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
using i18n::phonenumbers::PhoneNumberUtil;
|
||||
using i18n::phonenumbers::PhoneNumberOfflineGeocoder;
|
||||
using i18n::phonenumbers::scoped_ptr;
|
||||
|
||||
class PhoneNumberFormat {
|
||||
public:
|
||||
PhoneNumberFormat(const std::string &countryTag, const std::map<std::string, std::string> &options);
|
||||
virtual ~PhoneNumberFormat();
|
||||
bool isValidPhoneNumber(const std::string &number) const;
|
||||
std::string format(const std::string &number) const;
|
||||
static std::unique_ptr<PhoneNumberFormat> CreateInstance(const std::string &countryTag,
|
||||
const std::map<std::string, std::string> &options);
|
||||
std::string getLocationName(const std::string &number, const std::string &locale) const;
|
||||
|
||||
private:
|
||||
PhoneNumberUtil* GetPhoneNumberUtil();
|
||||
PhoneNumberUtil *util;
|
||||
scoped_ptr<PhoneNumberOfflineGeocoder> offLineGeocoder;
|
||||
std::string country;
|
||||
PhoneNumberUtil::PhoneNumberFormat phoneNumberFormat;
|
||||
};
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
/*
|
||||
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_GLOBAL_I18N_PHONE_NUMBER_FORMAT_H
|
||||
#define OHOS_GLOBAL_I18N_PHONE_NUMBER_FORMAT_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "cpp/src/phonenumbers/phonenumberutil.h"
|
||||
#include "cpp/src/phonenumbers/geocoding/phonenumber_offline_geocoder.h"
|
||||
#include "cpp/src/phonenumbers/base/memory/scoped_ptr.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
using i18n::phonenumbers::PhoneNumberUtil;
|
||||
using i18n::phonenumbers::PhoneNumberOfflineGeocoder;
|
||||
using i18n::phonenumbers::scoped_ptr;
|
||||
|
||||
class PhoneNumberFormat {
|
||||
public:
|
||||
PhoneNumberFormat(const std::string &countryTag, const std::map<std::string, std::string> &options);
|
||||
virtual ~PhoneNumberFormat();
|
||||
bool isValidPhoneNumber(const std::string &number) const;
|
||||
std::string format(const std::string &number) const;
|
||||
static std::unique_ptr<PhoneNumberFormat> CreateInstance(const std::string &countryTag,
|
||||
const std::map<std::string, std::string> &options);
|
||||
std::string getLocationName(const std::string &number, const std::string &locale) const;
|
||||
|
||||
private:
|
||||
PhoneNumberUtil* GetPhoneNumberUtil();
|
||||
PhoneNumberUtil *util;
|
||||
scoped_ptr<PhoneNumberOfflineGeocoder> offLineGeocoder;
|
||||
std::string country;
|
||||
PhoneNumberUtil::PhoneNumberFormat phoneNumberFormat;
|
||||
};
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
|
@ -1,109 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "locid.h"
|
||||
#include "phone_number_format.h"
|
||||
#include "cpp/src/phonenumbers/geocoding/phonenumber_offline_geocoder.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
using i18n::phonenumbers::PhoneNumberUtil;
|
||||
using i18n::phonenumbers::PhoneNumberOfflineGeocoder;
|
||||
|
||||
PhoneNumberFormat::PhoneNumberFormat(const std::string &countryTag,
|
||||
const std::map<std::string, std::string> &options)
|
||||
{
|
||||
util = PhoneNumberUtil::GetInstance();
|
||||
offLineGeocoder.reset(new PhoneNumberOfflineGeocoder());
|
||||
country = countryTag;
|
||||
|
||||
std::string type = "";
|
||||
auto search = options.find("type");
|
||||
if (search != options.end()) {
|
||||
type = search->second;
|
||||
}
|
||||
|
||||
std::map<std::string, PhoneNumberUtil::PhoneNumberFormat> type2PhoneNumberFormat = {
|
||||
{"E164", PhoneNumberUtil::PhoneNumberFormat::E164},
|
||||
{"RFC3966", PhoneNumberUtil::PhoneNumberFormat::RFC3966},
|
||||
{"INTERNATIONAL", PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL},
|
||||
{"NATIONAL", PhoneNumberUtil::PhoneNumberFormat::NATIONAL}
|
||||
};
|
||||
|
||||
std::set<std::string> validType = {"E164", "RFC3966", "INTERNATIONAL", "NATIONAL"};
|
||||
if (validType.find(type) != validType.end()) {
|
||||
phoneNumberFormat = type2PhoneNumberFormat[type];
|
||||
} else {
|
||||
phoneNumberFormat = PhoneNumberUtil::PhoneNumberFormat::NATIONAL;
|
||||
}
|
||||
}
|
||||
|
||||
PhoneNumberFormat::~PhoneNumberFormat()
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<PhoneNumberFormat> PhoneNumberFormat::CreateInstance(const std::string &countryTag,
|
||||
const std::map<std::string, std::string> &options)
|
||||
{
|
||||
std::unique_ptr<PhoneNumberFormat> phoneNumberFormat = std::make_unique<PhoneNumberFormat>(countryTag, options);
|
||||
if (phoneNumberFormat->GetPhoneNumberUtil() == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return phoneNumberFormat;
|
||||
}
|
||||
|
||||
PhoneNumberUtil* PhoneNumberFormat::GetPhoneNumberUtil()
|
||||
{
|
||||
return util;
|
||||
}
|
||||
|
||||
bool PhoneNumberFormat::isValidPhoneNumber(const std::string &number) const
|
||||
{
|
||||
i18n::phonenumbers::PhoneNumber phoneNumber;
|
||||
PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
|
||||
if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
|
||||
return false;
|
||||
}
|
||||
return util->IsValidNumber(phoneNumber);
|
||||
}
|
||||
|
||||
std::string PhoneNumberFormat::format(const std::string &number) const
|
||||
{
|
||||
i18n::phonenumbers::PhoneNumber phoneNumber;
|
||||
PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
|
||||
if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
|
||||
return "";
|
||||
}
|
||||
std::string formatted_number;
|
||||
util->Format(phoneNumber, phoneNumberFormat, &formatted_number);
|
||||
return formatted_number;
|
||||
}
|
||||
|
||||
std::string PhoneNumberFormat::getLocationName(const std::string &number,const std::string &locale) const
|
||||
{
|
||||
const char *l_name = locale.c_str();
|
||||
icu::Locale uLocale = icu::Locale::createFromName(l_name);
|
||||
i18n::phonenumbers::PhoneNumber phoneNumber;
|
||||
PhoneNumberUtil::ErrorType type = util->Parse(number, uLocale.getCountry(), &phoneNumber);
|
||||
if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
|
||||
return "";
|
||||
}
|
||||
std::string location_name = offLineGeocoder->GetDescriptionForNumber(phoneNumber, uLocale);
|
||||
return location_name;
|
||||
}
|
||||
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
/*
|
||||
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "locid.h"
|
||||
#include "phone_number_format.h"
|
||||
#include "cpp/src/phonenumbers/geocoding/phonenumber_offline_geocoder.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
using i18n::phonenumbers::PhoneNumberUtil;
|
||||
|
||||
PhoneNumberFormat::PhoneNumberFormat(const std::string &countryTag,
|
||||
const std::map<std::string, std::string> &options)
|
||||
{
|
||||
util = PhoneNumberUtil::GetInstance();
|
||||
offLineGeocoder.reset(new PhoneNumberOfflineGeocoder());
|
||||
country = countryTag;
|
||||
|
||||
std::string type = "";
|
||||
auto search = options.find("type");
|
||||
if (search != options.end()) {
|
||||
type = search->second;
|
||||
}
|
||||
|
||||
std::map<std::string, PhoneNumberUtil::PhoneNumberFormat> type2PhoneNumberFormat = {
|
||||
{"E164", PhoneNumberUtil::PhoneNumberFormat::E164},
|
||||
{"RFC3966", PhoneNumberUtil::PhoneNumberFormat::RFC3966},
|
||||
{"INTERNATIONAL", PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL},
|
||||
{"NATIONAL", PhoneNumberUtil::PhoneNumberFormat::NATIONAL}
|
||||
};
|
||||
|
||||
std::set<std::string> validType = {"E164", "RFC3966", "INTERNATIONAL", "NATIONAL"};
|
||||
if (validType.find(type) != validType.end()) {
|
||||
phoneNumberFormat = type2PhoneNumberFormat[type];
|
||||
} else {
|
||||
phoneNumberFormat = PhoneNumberUtil::PhoneNumberFormat::NATIONAL;
|
||||
}
|
||||
}
|
||||
|
||||
PhoneNumberFormat::~PhoneNumberFormat()
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<PhoneNumberFormat> PhoneNumberFormat::CreateInstance(const std::string &countryTag,
|
||||
const std::map<std::string, std::string> &options)
|
||||
{
|
||||
std::unique_ptr<PhoneNumberFormat> phoneNumberFormat = std::make_unique<PhoneNumberFormat>(countryTag, options);
|
||||
if (phoneNumberFormat->GetPhoneNumberUtil() == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return phoneNumberFormat;
|
||||
}
|
||||
|
||||
PhoneNumberUtil* PhoneNumberFormat::GetPhoneNumberUtil()
|
||||
{
|
||||
return util;
|
||||
}
|
||||
|
||||
bool PhoneNumberFormat::isValidPhoneNumber(const std::string &number) const
|
||||
{
|
||||
i18n::phonenumbers::PhoneNumber phoneNumber;
|
||||
PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
|
||||
if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
|
||||
return false;
|
||||
}
|
||||
return util->IsValidNumber(phoneNumber);
|
||||
}
|
||||
|
||||
std::string PhoneNumberFormat::format(const std::string &number) const
|
||||
{
|
||||
i18n::phonenumbers::PhoneNumber phoneNumber;
|
||||
PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
|
||||
if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
|
||||
return "";
|
||||
}
|
||||
std::string formatted_number;
|
||||
util->Format(phoneNumber, phoneNumberFormat, &formatted_number);
|
||||
return formatted_number;
|
||||
}
|
||||
|
||||
std::string PhoneNumberFormat::getLocationName(const std::string &number,const std::string &locale) const
|
||||
{
|
||||
const char *l_name = locale.c_str();
|
||||
icu::Locale uLocale = icu::Locale::createFromName(l_name);
|
||||
i18n::phonenumbers::PhoneNumber phoneNumber;
|
||||
PhoneNumberUtil::ErrorType type = util->Parse(number, uLocale.getCountry(), &phoneNumber);
|
||||
if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
|
||||
return "";
|
||||
}
|
||||
std::string location_name = offLineGeocoder->GetDescriptionForNumber(phoneNumber, uLocale);
|
||||
return location_name;
|
||||
}
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
} // namespace OHOS
|
Loading…
Reference in New Issue
Block a user