fix LocaleInfo

Signed-off-by: sunyaozu <sunyaozu@huawei.com>
This commit is contained in:
sunyaozu 2022-11-25 17:47:56 +08:00
parent e837fbf788
commit 7953dfa95a
3 changed files with 31 additions and 7 deletions

View File

@ -63,6 +63,7 @@ private:
std::string finalLocaleTag;
std::string localeTag;
icu::Locale locale;
bool localeStatus = false;
std::string calendarTag = "-ca-";
std::string collationTag = "-co-";
std::string hourCycleTag = "-hc-";
@ -76,6 +77,7 @@ private:
void ComputeFinalLocaleTag(const std::string &localeTag);
void ParseConfigs();
void ParseLocaleTag(const std::string &localeTag);
void ResetFinalLocaleStatus();
};
} // namespace I18n
} // namespace Global

View File

@ -47,6 +47,18 @@ std::set<std::string> LocaleInfo::GetValidLocales()
return allValidLocales;
}
void LocaleInfo::ResetFinalLocaleStatus()
{
finalLocaleTag = "";
calendar = "";
collation = "";
hourCycle = "";
numberingSystem = "";
numeric = "";
caseFirst = "";
configs = {};
}
LocaleInfo::LocaleInfo(const std::string &localeTag)
{
UErrorCode status = U_ZERO_ERROR;
@ -60,7 +72,7 @@ LocaleInfo::LocaleInfo(const std::string &localeTag)
Locale defaultLocale(defaultLocaleTag.c_str());
locale = defaultLocale;
finalLocaleTag = "";
ResetFinalLocaleStatus();
ComputeFinalLocaleTag(defaultLocaleTag);
}
language = locale.getLanguage();
@ -87,17 +99,20 @@ LocaleInfo::LocaleInfo(const std::string &localeTag, std::map<std::string, std::
}
if (localeTag == "" || status != U_ZERO_ERROR) {
std::string defaultLocaleTag = LocaleConfig::GetSystemLocale();
finalLocaleTag = "";
ResetFinalLocaleStatus();
ComputeFinalLocaleTag(defaultLocaleTag);
status = U_ZERO_ERROR;
builder->clear();
locale = builder->setLanguageTag(StringPiece(finalLocaleTag)).build(status);
}
language = locale.getLanguage();
script = locale.getScript();
region = locale.getCountry();
baseName = locale.getBaseName();
std::replace(baseName.begin(), baseName.end(), '_', '-');
if (status == U_ZERO_ERROR) {
localeStatus = true;
language = locale.getLanguage();
script = locale.getScript();
region = locale.getCountry();
baseName = locale.getBaseName();
std::replace(baseName.begin(), baseName.end(), '_', '-');
}
}
LocaleInfo::~LocaleInfo() {}
@ -263,6 +278,9 @@ Locale LocaleInfo::GetLocale() const
std::string LocaleInfo::Maximize()
{
if (!localeStatus) {
return "";
}
UErrorCode status = U_ZERO_ERROR;
Locale curLocale = locale;
curLocale.addLikelySubtags(status);
@ -281,6 +299,9 @@ std::string LocaleInfo::Maximize()
std::string LocaleInfo::Minimize()
{
if (!localeStatus) {
return "";
}
UErrorCode status = U_ZERO_ERROR;
Locale curLocale = locale;
curLocale.minimizeSubtags(status);

View File

@ -489,6 +489,7 @@ napi_value I18nAddon::TransliteratorConstructor(napi_env env, napi_callback_info
return nullptr;
}
if (!obj->InitTransliteratorContext(env, info, idTag)) {
obj.release();
return nullptr;
}
obj.release();