!650 国际化接口支持伪本地化测试

Merge pull request !650 from sunyaozu/master
This commit is contained in:
openharmony_ci 2024-03-27 01:59:43 +00:00 committed by Gitee
commit 1b472371b4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
12 changed files with 70 additions and 34 deletions

View File

@ -31,6 +31,9 @@ void GetAllValidLocalesTag(std::unordered_set<std::string>& allValidLocalesLangu
bool CheckTzDataFilePath(const std::string &filePath); bool CheckTzDataFilePath(const std::string &filePath);
void RemoveNativeTokenTypeAndPermissions(const uint64_t selfTokenId); void RemoveNativeTokenTypeAndPermissions(const uint64_t selfTokenId);
void AddNativeTokenTypeAndPermissions(); void AddNativeTokenTypeAndPermissions();
bool GetPseudoLocalizationEnforce();
std::string PseudoLocalizationProcessor(const std::string &input);
std::string PseudoLocalizationProcessor(const std::string &input, bool ifEnforce);
} // namespace I18n } // namespace I18n
} // namespace Global } // namespace Global
} // namespace OHOS } // namespace OHOS

View File

@ -575,7 +575,7 @@ std::string DateTimeFormat::GetSystemTimezone()
std::string DateTimeFormat::Format(int64_t milliseconds) std::string DateTimeFormat::Format(int64_t milliseconds)
{ {
if (!createSuccess) { if (!createSuccess) {
return ""; return PseudoLocalizationProcessor("");
} }
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
std::string result; std::string result;
@ -590,13 +590,13 @@ std::string DateTimeFormat::Format(int64_t milliseconds)
calendar->setTime((UDate)milliseconds, status); calendar->setTime((UDate)milliseconds, status);
dateFormat->format(calendar->getTime(status), dateString, status); dateFormat->format(calendar->getTime(status), dateString, status);
dateString.toUTF8String(result); dateString.toUTF8String(result);
return result; return PseudoLocalizationProcessor(result);
} }
std::string DateTimeFormat::FormatRange(int64_t fromMilliseconds, int64_t toMilliseconds) std::string DateTimeFormat::FormatRange(int64_t fromMilliseconds, int64_t toMilliseconds)
{ {
if (!createSuccess || calendar == nullptr) { if (!createSuccess || calendar == nullptr) {
return ""; return PseudoLocalizationProcessor("");
} }
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
std::string result; std::string result;
@ -612,7 +612,7 @@ std::string DateTimeFormat::FormatRange(int64_t fromMilliseconds, int64_t toMill
auto toCalendar = std::unique_ptr<Calendar>(Calendar::createInstance(locale, status)); auto toCalendar = std::unique_ptr<Calendar>(Calendar::createInstance(locale, status));
if (status != U_ZERO_ERROR || toCalendar == nullptr) { if (status != U_ZERO_ERROR || toCalendar == nullptr) {
return ""; return PseudoLocalizationProcessor("");
} }
toCalendar->clear(); toCalendar->clear();
if (zone != nullptr) { if (zone != nullptr) {
@ -622,7 +622,7 @@ std::string DateTimeFormat::FormatRange(int64_t fromMilliseconds, int64_t toMill
FieldPosition pos = 0; FieldPosition pos = 0;
dateIntvFormat->format(*calendar, *toCalendar, dateString, pos, status); dateIntvFormat->format(*calendar, *toCalendar, dateString, pos, status);
dateString.toUTF8String(result); dateString.toUTF8String(result);
return result; return PseudoLocalizationProcessor(result);
} }
void DateTimeFormat::GetResolvedOptions(std::map<std::string, std::string> &map) void DateTimeFormat::GetResolvedOptions(std::map<std::string, std::string> &map)

View File

@ -23,6 +23,7 @@
#include <iostream> #include <iostream>
#include <regex> #include <regex>
#include "map" #include "map"
#include "utils.h"
namespace OHOS { namespace OHOS {
namespace Global { namespace Global {
@ -205,7 +206,7 @@ void HolidayManager::ParseFileLine(const std::string &line, HolidayInfoItem *hol
std::string language = displayName.substr(0, displayName.find_first_of(":")); std::string language = displayName.substr(0, displayName.find_first_of(":"));
std::string localName = displayName.substr(displayName.find_first_of(":") + 1, displayName.length()); std::string localName = displayName.substr(displayName.find_first_of(":") + 1, displayName.length());
transform(language.begin(), language.end(), language.begin(), ::tolower); transform(language.begin(), language.end(), language.begin(), ::tolower);
HolidayLocalName localeName = {language, localName}; HolidayLocalName localeName = {language, PseudoLocalizationProcessor(localName)};
holidayItem->localNames.push_back(localeName); holidayItem->localNames.push_back(localeName);
} }
} }

View File

@ -34,6 +34,7 @@
#include "unicode/urename.h" #include "unicode/urename.h"
#include "ustr_imp.h" #include "ustr_imp.h"
#include "unicode/ustring.h" #include "unicode/ustring.h"
#include "utils.h"
namespace OHOS { namespace OHOS {
namespace Global { namespace Global {
@ -287,16 +288,16 @@ bool I18nCalendar::IsWeekend(void)
std::string I18nCalendar::GetDisplayName(std::string &displayLocaleTag) std::string I18nCalendar::GetDisplayName(std::string &displayLocaleTag)
{ {
if (calendar_ == nullptr) { if (calendar_ == nullptr) {
return ""; return PseudoLocalizationProcessor("");
} }
const char *type = calendar_->getType(); const char *type = calendar_->getType();
if (type == nullptr) { if (type == nullptr) {
return ""; return PseudoLocalizationProcessor("");
} }
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
icu::Locale displayLocale = icu::Locale::forLanguageTag(displayLocaleTag, status); icu::Locale displayLocale = icu::Locale::forLanguageTag(displayLocaleTag, status);
if (status != U_ZERO_ERROR) { if (status != U_ZERO_ERROR) {
return ""; return PseudoLocalizationProcessor("");
} }
icu::LocaleDisplayNames *dspName = icu::LocaleDisplayNames::createInstance(displayLocale); icu::LocaleDisplayNames *dspName = icu::LocaleDisplayNames::createInstance(displayLocale);
icu::UnicodeString unistr; icu::UnicodeString unistr;
@ -306,7 +307,7 @@ std::string I18nCalendar::GetDisplayName(std::string &displayLocaleTag)
} }
std::string ret; std::string ret;
unistr.toUTF8String<std::string>(ret); unistr.toUTF8String<std::string>(ret);
return ret; return PseudoLocalizationProcessor(ret);
} }
int32_t I18nCalendar::CompareDays(UDate date) int32_t I18nCalendar::CompareDays(UDate date)

View File

@ -711,12 +711,12 @@ __attribute__((no_sanitize("cfi"))) std::string I18nTimeZone::GetDisplayName(
} }
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
HILOG_ERROR_I18N("create icu Locale for %{public}s failed.", localeStr.c_str()); HILOG_ERROR_I18N("create icu Locale for %{public}s failed.", localeStr.c_str());
return result; return PseudoLocalizationProcessor(result);
} }
icu::UnicodeString name; icu::UnicodeString name;
timezone->getDisplayName((UBool)isDST, style, locale, name); timezone->getDisplayName((UBool)isDST, style, locale, name);
name.toUTF8String(result); name.toUTF8String(result);
return GetDisplayNameByTaboo(localeStr, result); return PseudoLocalizationProcessor(GetDisplayNameByTaboo(localeStr, result));
} }
__attribute__((no_sanitize("cfi"))) std::string I18nTimeZone::GetDisplayNameByTaboo( __attribute__((no_sanitize("cfi"))) std::string I18nTimeZone::GetDisplayNameByTaboo(
@ -1012,16 +1012,16 @@ std::string I18nTimeZone::GetCityDisplayName(std::string &cityID, std::string &l
} }
if (availableZoneCityIDs.find(cityID) == availableZoneCityIDs.end()) { if (availableZoneCityIDs.find(cityID) == availableZoneCityIDs.end()) {
HILOG_ERROR_I18N("%{public}s is not supported cityID.", cityID.c_str()); HILOG_ERROR_I18N("%{public}s is not supported cityID.", cityID.c_str());
return ""; return PseudoLocalizationProcessor("");
} }
std::string requestLocaleStr = GetLocaleBaseName(localeStr); std::string requestLocaleStr = GetLocaleBaseName(localeStr);
if (requestLocaleStr.length() != 0) { if (requestLocaleStr.length() != 0) {
std::string displayName = FindCityDisplayNameFromXml(cityID, requestLocaleStr); std::string displayName = FindCityDisplayNameFromXml(cityID, requestLocaleStr);
if (displayName.length() != 0) { if (displayName.length() != 0) {
return displayName; return PseudoLocalizationProcessor(displayName);
} }
} }
return ""; return PseudoLocalizationProcessor("");
} }
std::string I18nTimeZone::GetLocaleBaseName(std::string &localeStr) std::string I18nTimeZone::GetLocaleBaseName(std::string &localeStr)

View File

@ -638,11 +638,11 @@ string LocaleConfig::GetDisplayLanguage(const string &language, const string &di
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
icu::Locale displayLoc = icu::Locale::forLanguageTag(displayLocale, status); icu::Locale displayLoc = icu::Locale::forLanguageTag(displayLocale, status);
if (status != U_ZERO_ERROR) { if (status != U_ZERO_ERROR) {
return ""; return PseudoLocalizationProcessor("");
} }
icu::Locale locale = icu::Locale::forLanguageTag(language, status); icu::Locale locale = icu::Locale::forLanguageTag(language, status);
if (status != U_ZERO_ERROR) { if (status != U_ZERO_ERROR) {
return ""; return PseudoLocalizationProcessor("");
} }
icu::UnicodeString unistr; icu::UnicodeString unistr;
std::string lang(locale.getLanguage()); std::string lang(locale.getLanguage());
@ -655,11 +655,11 @@ string LocaleConfig::GetDisplayLanguage(const string &language, const string &di
} }
if (sentenceCase) { if (sentenceCase) {
char ch = static_cast<char>(toupper(result[0])); char ch = static_cast<char>(toupper(result[0]));
return result.replace(0, 1, 1, ch); return PseudoLocalizationProcessor(result.replace(0, 1, 1, ch));
} }
return result; return PseudoLocalizationProcessor(result);
} }
return GetDisplayLanguageInner(adjust, displayLocale, sentenceCase); return PseudoLocalizationProcessor(GetDisplayLanguageInner(adjust, displayLocale, sentenceCase));
} }
std::string LocaleConfig::ComputeLocale(const std::string &displayLocale) std::string LocaleConfig::ComputeLocale(const std::string &displayLocale)
@ -877,14 +877,14 @@ string LocaleConfig::GetDisplayRegion(const string &region, const string &displa
} }
std::string country(originLocale.getCountry()); std::string country(originLocale.getCountry());
if (country.length() == 0) { if (country.length() == 0) {
return ""; return PseudoLocalizationProcessor("");
} }
if (status != U_ZERO_ERROR) { if (status != U_ZERO_ERROR) {
return ""; return PseudoLocalizationProcessor("");
} }
icu::Locale locale = icu::Locale::forLanguageTag(displayLocale, status); icu::Locale locale = icu::Locale::forLanguageTag(displayLocale, status);
if (status != U_ZERO_ERROR) { if (status != U_ZERO_ERROR) {
return ""; return PseudoLocalizationProcessor("");
} }
icu::UnicodeString unistr; icu::UnicodeString unistr;
icu::UnicodeString displayRegion; icu::UnicodeString displayRegion;
@ -897,9 +897,9 @@ string LocaleConfig::GetDisplayRegion(const string &region, const string &displa
} }
if (sentenceCase) { if (sentenceCase) {
char ch = static_cast<char>(toupper(result[0])); char ch = static_cast<char>(toupper(result[0]));
return result.replace(0, 1, 1, ch); return PseudoLocalizationProcessor(result.replace(0, 1, 1, ch));
} }
return result; return PseudoLocalizationProcessor(result);
} }
bool LocaleConfig::IsRTL(const string &locale) bool LocaleConfig::IsRTL(const string &locale)

View File

@ -306,7 +306,7 @@ void NumberFormat::SetUnit(std::string &preferredUnit)
std::string NumberFormat::Format(double number) std::string NumberFormat::Format(double number)
{ {
if (!createSuccess) { if (!createSuccess) {
return ""; return PseudoLocalizationProcessor("");
} }
double finalNumber = number; double finalNumber = number;
if (!unitUsage.empty()) { if (!unitUsage.empty()) {
@ -343,7 +343,7 @@ std::string NumberFormat::Format(double number)
std::string result; std::string result;
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
numberFormat.formatDouble(finalNumber, status).toString(status).toUTF8String(result); numberFormat.formatDouble(finalNumber, status).toString(status).toUTF8String(result);
return result; return PseudoLocalizationProcessor(result);
} }
void NumberFormat::GetResolvedOptions(std::map<std::string, std::string> &map) void NumberFormat::GetResolvedOptions(std::map<std::string, std::string> &map)

View File

@ -27,6 +27,7 @@
#include "string" #include "string"
#include "taboo_utils.h" #include "taboo_utils.h"
#include "utility" #include "utility"
#include "utils.h"
namespace OHOS { namespace OHOS {
@ -111,11 +112,11 @@ std::string PhoneNumberFormat::format(const std::string &number) const
i18n::phonenumbers::PhoneNumber phoneNumber; i18n::phonenumbers::PhoneNumber phoneNumber;
PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber); PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) { if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
return ""; return PseudoLocalizationProcessor("");
} }
std::string formatted_number; std::string formatted_number;
util->Format(phoneNumber, phoneNumberFormat, &formatted_number); util->Format(phoneNumber, phoneNumberFormat, &formatted_number);
return formatted_number; return PseudoLocalizationProcessor(formatted_number);
} }
__attribute__((no_sanitize("cfi"))) std::string PhoneNumberFormat::getLocationName( __attribute__((no_sanitize("cfi"))) std::string PhoneNumberFormat::getLocationName(

View File

@ -15,6 +15,7 @@
#include "locale_config.h" #include "locale_config.h"
#include "ohos/init_data.h" #include "ohos/init_data.h"
#include "parameter.h" #include "parameter.h"
#include "utils.h"
#include "relative_time_format.h" #include "relative_time_format.h"
namespace OHOS { namespace OHOS {
@ -121,7 +122,7 @@ void RelativeTimeFormat::ParseConfigs(std::map<std::string, std::string> &config
std::string RelativeTimeFormat::Format(double number, const std::string &unit) std::string RelativeTimeFormat::Format(double number, const std::string &unit)
{ {
if (!createSuccess || !relativeUnits.count(unit)) { if (!createSuccess || !relativeUnits.count(unit)) {
return ""; return PseudoLocalizationProcessor("");
} }
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString formattedTime; icu::UnicodeString formattedTime;
@ -132,7 +133,7 @@ std::string RelativeTimeFormat::Format(double number, const std::string &unit)
formattedTime = relativeTimeFormat->formatToValue(number, relativeUnits[unit], status).toString(status); formattedTime = relativeTimeFormat->formatToValue(number, relativeUnits[unit], status).toString(status);
} }
formattedTime.toUTF8String(result); formattedTime.toUTF8String(result);
return result; return PseudoLocalizationProcessor(result);
} }
void RelativeTimeFormat::InsertInfo(std::vector<std::vector<std::string>> &timeVector, void RelativeTimeFormat::InsertInfo(std::vector<std::vector<std::string>> &timeVector,

View File

@ -91,6 +91,7 @@ std::vector<LocaleItem> SystemLocaleManager::GetCountryInfoArray(const std::vect
std::unordered_set<std::string> blockedRegions = LocaleConfig::GetBlockedRegions(); std::unordered_set<std::string> blockedRegions = LocaleConfig::GetBlockedRegions();
std::unordered_set<std::string> blockedLanguageRegions = LocaleConfig::GetLanguageBlockedRegions(); std::unordered_set<std::string> blockedLanguageRegions = LocaleConfig::GetLanguageBlockedRegions();
std::vector<LocaleItem> localeItemList; std::vector<LocaleItem> localeItemList;
std::string pseudoProcessedRegion = PseudoLocalizationProcessor("");
for (auto it = countries.begin(); it != countries.end(); ++it) { for (auto it = countries.begin(); it != countries.end(); ++it) {
if (blockedRegions.find(*it) != blockedRegions.end() || blockedLanguageRegions.find(*it) != if (blockedRegions.find(*it) != blockedRegions.end() || blockedLanguageRegions.find(*it) !=
blockedLanguageRegions.end()) { blockedLanguageRegions.end()) {
@ -103,7 +104,7 @@ std::vector<LocaleItem> SystemLocaleManager::GetCountryInfoArray(const std::vect
if (isSuggestedRegion) { if (isSuggestedRegion) {
suggestionType = SuggestionType::SUGGESTION_TYPE_RELATED; suggestionType = SuggestionType::SUGGESTION_TYPE_RELATED;
} }
LocaleItem item { *it, suggestionType, regionDisplayName, "" }; LocaleItem item { *it, suggestionType, regionDisplayName, pseudoProcessedRegion };
localeItemList.push_back(item); localeItemList.push_back(item);
} }
SortLocaleItemList(localeItemList, options); SortLocaleItemList(localeItemList, options);
@ -155,6 +156,7 @@ std::vector<TimeZoneCityItem> SystemLocaleManager::GetTimezoneCityInfoArray()
std::string localeBaseName = I18nTimeZone::GetLocaleBaseName(locale); std::string localeBaseName = I18nTimeZone::GetLocaleBaseName(locale);
std::map<std::string, std::string> displayNameMap = I18nTimeZone::FindCityDisplayNameMap(localeBaseName); std::map<std::string, std::string> displayNameMap = I18nTimeZone::FindCityDisplayNameMap(localeBaseName);
std::map<std::string, icu::TimeZone*> tzMap; std::map<std::string, icu::TimeZone*> tzMap;
bool ifEnforce = GetPseudoLocalizationEnforce();
for (auto it = zoneCityIds.begin(); it != zoneCityIds.end(); ++it) { for (auto it = zoneCityIds.begin(); it != zoneCityIds.end(); ++it) {
std::string cityId = *it; std::string cityId = *it;
std::string cityDisplayName = ""; std::string cityDisplayName = "";
@ -180,7 +182,8 @@ std::vector<TimeZoneCityItem> SystemLocaleManager::GetTimezoneCityInfoArray()
tzMap.insert({timezoneId, icuTimeZone}); tzMap.insert({timezoneId, icuTimeZone});
} }
struct TimeZoneCityItem tzCityItem = { struct TimeZoneCityItem tzCityItem = {
timezoneId, cityId, cityDisplayName, dstOffset + rawOffset, "", rawOffset timezoneId, cityId, PseudoLocalizationProcessor(cityDisplayName, ifEnforce), dstOffset + rawOffset,
PseudoLocalizationProcessor("", ifEnforce), rawOffset
}; };
result.push_back(tzCityItem); result.push_back(tzCityItem);
} }

View File

@ -23,6 +23,7 @@
#include <vector> #include <vector>
#include "accesstoken_kit.h" #include "accesstoken_kit.h"
#include "i18n_hilog.h" #include "i18n_hilog.h"
#include "locale_config.h"
#include "nativetoken_kit.h" #include "nativetoken_kit.h"
#include "parameter.h" #include "parameter.h"
#include "token_setproc.h" #include "token_setproc.h"
@ -34,6 +35,9 @@ namespace I18n {
using namespace std; using namespace std;
static constexpr int32_t I18N_UID = 3013; static constexpr int32_t I18N_UID = 3013;
static constexpr int32_t ROOT_UID = 0; static constexpr int32_t ROOT_UID = 0;
static const std::string PSEUDO_LOCALE_TAG = "en-XA";
static const std::string PSEUDO_START_TAG = "{";
static const std::string PSEUDO_END_TAG = "}";
void Split(const string &src, const string &sep, vector<string> &dest) void Split(const string &src, const string &sep, vector<string> &dest)
{ {
@ -157,6 +161,28 @@ void AddNativeTokenTypeAndPermissions()
OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
seteuid(I18N_UID); seteuid(I18N_UID);
} }
bool GetPseudoLocalizationEnforce()
{
std::string systemLocale = LocaleConfig::GetSystemLocale();
if (systemLocale.compare(PSEUDO_LOCALE_TAG) == 0) {
return true;
}
return false;
}
std::string PseudoLocalizationProcessor(const std::string &input)
{
return PseudoLocalizationProcessor(input, GetPseudoLocalizationEnforce());
}
std::string PseudoLocalizationProcessor(const std::string &input, bool ifEnforce)
{
if (ifEnforce) {
return PSEUDO_START_TAG + input + PSEUDO_END_TAG;
}
return input;
}
} // namespace I18n } // namespace I18n
} // namespace Global } // namespace Global
} // namespace OHOS } // namespace OHOS

View File

@ -294,7 +294,7 @@ napi_value I18nAddon::GetTimePeriodName(napi_env env, napi_callback_info info)
calendar->set(UCalendarDateFields::UCAL_HOUR_OF_DAY, hour); calendar->set(UCalendarDateFields::UCAL_HOUR_OF_DAY, hour);
formatter->format(calendar->getTime(icuStatus), name); formatter->format(calendar->getTime(icuStatus), name);
name.toUTF8String(temp); name.toUTF8String(temp);
napi_create_string_utf8(env, temp.c_str(), NAPI_AUTO_LENGTH, &result); napi_create_string_utf8(env, PseudoLocalizationProcessor(temp).c_str(), NAPI_AUTO_LENGTH, &result);
delete formatter; delete formatter;
delete calendar; delete calendar;
return result; return result;