Merge branch 'master' of gitee.com:openharmony/global_i18n into master

Signed-off-by: LY <liuyong235@huawei.com>
This commit is contained in:
LY 2024-03-27 02:04:46 +00:00 committed by Gitee
commit 8d1cce2147
12 changed files with 71 additions and 35 deletions

View File

@ -29,6 +29,9 @@ int32_t ConvertString2Int(const std::string &numberStr, int32_t& status);
bool IsValidLocaleTag(icu::Locale &locale);
void GetAllValidLocalesTag(std::unordered_set<std::string>& allValidLocalesLanguageTag);
bool CheckTzDataFilePath(const std::string &filePath);
bool GetPseudoLocalizationEnforce();
std::string PseudoLocalizationProcessor(const std::string &input);
std::string PseudoLocalizationProcessor(const std::string &input, bool ifEnforce);
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

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

View File

@ -23,6 +23,7 @@
#include <iostream>
#include <regex>
#include "map"
#include "utils.h"
namespace OHOS {
namespace Global {
@ -51,7 +52,7 @@ HolidayManager::HolidayManager(const char* path)
char strDate[10];
size_t resCode = strftime(strDate, sizeof(strDate), "%Y%m%d", &tmObj);
if (resCode == 0) {
HILOG_ERROR_I18N("Failed: strftime error:%{public}d .", resCode);
HILOG_ERROR_I18N("Failed: strftime error:%{public}zu .", resCode);
return;
}
std::string startDate(strDate);
@ -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 localName = displayName.substr(displayName.find_first_of(":") + 1, displayName.length());
transform(language.begin(), language.end(), language.begin(), ::tolower);
HolidayLocalName localeName = {language, localName};
HolidayLocalName localeName = {language, PseudoLocalizationProcessor(localName)};
holidayItem->localNames.push_back(localeName);
}
}

View File

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

View File

@ -711,12 +711,12 @@ __attribute__((no_sanitize("cfi"))) std::string I18nTimeZone::GetDisplayName(
}
if (U_FAILURE(status)) {
HILOG_ERROR_I18N("create icu Locale for %{public}s failed.", localeStr.c_str());
return result;
return PseudoLocalizationProcessor(result);
}
icu::UnicodeString name;
timezone->getDisplayName((UBool)isDST, style, locale, name);
name.toUTF8String(result);
return GetDisplayNameByTaboo(localeStr, result);
return PseudoLocalizationProcessor(GetDisplayNameByTaboo(localeStr, result));
}
__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()) {
HILOG_ERROR_I18N("%{public}s is not supported cityID.", cityID.c_str());
return "";
return PseudoLocalizationProcessor("");
}
std::string requestLocaleStr = GetLocaleBaseName(localeStr);
if (requestLocaleStr.length() != 0) {
std::string displayName = FindCityDisplayNameFromXml(cityID, requestLocaleStr);
if (displayName.length() != 0) {
return displayName;
return PseudoLocalizationProcessor(displayName);
}
}
return "";
return PseudoLocalizationProcessor("");
}
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;
icu::Locale displayLoc = icu::Locale::forLanguageTag(displayLocale, status);
if (status != U_ZERO_ERROR) {
return "";
return PseudoLocalizationProcessor("");
}
icu::Locale locale = icu::Locale::forLanguageTag(language, status);
if (status != U_ZERO_ERROR) {
return "";
return PseudoLocalizationProcessor("");
}
icu::UnicodeString unistr;
std::string lang(locale.getLanguage());
@ -655,11 +655,11 @@ string LocaleConfig::GetDisplayLanguage(const string &language, const string &di
}
if (sentenceCase) {
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)
@ -877,14 +877,14 @@ string LocaleConfig::GetDisplayRegion(const string &region, const string &displa
}
std::string country(originLocale.getCountry());
if (country.length() == 0) {
return "";
return PseudoLocalizationProcessor("");
}
if (status != U_ZERO_ERROR) {
return "";
return PseudoLocalizationProcessor("");
}
icu::Locale locale = icu::Locale::forLanguageTag(displayLocale, status);
if (status != U_ZERO_ERROR) {
return "";
return PseudoLocalizationProcessor("");
}
icu::UnicodeString unistr;
icu::UnicodeString displayRegion;
@ -897,9 +897,9 @@ string LocaleConfig::GetDisplayRegion(const string &region, const string &displa
}
if (sentenceCase) {
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)

View File

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

View File

@ -27,6 +27,7 @@
#include "string"
#include "taboo_utils.h"
#include "utility"
#include "utils.h"
namespace OHOS {
@ -111,11 +112,11 @@ 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 "";
return PseudoLocalizationProcessor("");
}
std::string formatted_number;
util->Format(phoneNumber, phoneNumberFormat, &formatted_number);
return formatted_number;
return PseudoLocalizationProcessor(formatted_number);
}
__attribute__((no_sanitize("cfi"))) std::string PhoneNumberFormat::getLocationName(

View File

@ -15,6 +15,7 @@
#include "locale_config.h"
#include "ohos/init_data.h"
#include "parameter.h"
#include "utils.h"
#include "relative_time_format.h"
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)
{
if (!createSuccess || !relativeUnits.count(unit)) {
return "";
return PseudoLocalizationProcessor("");
}
UErrorCode status = U_ZERO_ERROR;
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.toUTF8String(result);
return result;
return PseudoLocalizationProcessor(result);
}
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> blockedLanguageRegions = LocaleConfig::GetLanguageBlockedRegions();
std::vector<LocaleItem> localeItemList;
std::string pseudoProcessedRegion = PseudoLocalizationProcessor("");
for (auto it = countries.begin(); it != countries.end(); ++it) {
if (blockedRegions.find(*it) != blockedRegions.end() || blockedLanguageRegions.find(*it) !=
blockedLanguageRegions.end()) {
@ -103,7 +104,7 @@ std::vector<LocaleItem> SystemLocaleManager::GetCountryInfoArray(const std::vect
if (isSuggestedRegion) {
suggestionType = SuggestionType::SUGGESTION_TYPE_RELATED;
}
LocaleItem item { *it, suggestionType, regionDisplayName, "" };
LocaleItem item { *it, suggestionType, regionDisplayName, pseudoProcessedRegion };
localeItemList.push_back(item);
}
SortLocaleItemList(localeItemList, options);
@ -155,6 +156,7 @@ std::vector<TimeZoneCityItem> SystemLocaleManager::GetTimezoneCityInfoArray()
std::string localeBaseName = I18nTimeZone::GetLocaleBaseName(locale);
std::map<std::string, std::string> displayNameMap = I18nTimeZone::FindCityDisplayNameMap(localeBaseName);
std::map<std::string, icu::TimeZone*> tzMap;
bool ifEnforce = GetPseudoLocalizationEnforce();
for (auto it = zoneCityIds.begin(); it != zoneCityIds.end(); ++it) {
std::string cityId = *it;
std::string cityDisplayName = "";
@ -180,7 +182,8 @@ std::vector<TimeZoneCityItem> SystemLocaleManager::GetTimezoneCityInfoArray()
tzMap.insert({timezoneId, icuTimeZone});
}
struct TimeZoneCityItem tzCityItem = {
timezoneId, cityId, cityDisplayName, dstOffset + rawOffset, "", rawOffset
timezoneId, cityId, PseudoLocalizationProcessor(cityDisplayName, ifEnforce), dstOffset + rawOffset,
PseudoLocalizationProcessor("", ifEnforce), rawOffset
};
result.push_back(tzCityItem);
}

View File

@ -21,6 +21,7 @@
#include <string>
#include <vector>
#include "i18n_hilog.h"
#include "locale_config.h"
#include "parameter.h"
#include "utils.h"
@ -28,6 +29,9 @@ namespace OHOS {
namespace Global {
namespace I18n {
using namespace std;
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)
{
@ -122,6 +126,28 @@ bool CheckTzDataFilePath(const std::string &filePath)
realpathRes = nullptr;
return true;
}
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 Global
} // 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);
formatter->format(calendar->getTime(icuStatus), name);
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 calendar;
return result;