mirror of
https://gitee.com/openharmony/global_i18n
synced 2025-02-19 23:55:44 +00:00
fix system locale does't match with system language
Signed-off-by: sunyaozu <sunyaozu@huawei.com>
This commit is contained in:
parent
05bd152d25
commit
26f96dc945
@ -57,6 +57,7 @@ private:
|
||||
static bool IsValidRegion(const std::string ®ion);
|
||||
static bool IsValidTag(const std::string &tag);
|
||||
static void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest);
|
||||
static bool UpdateSystemLocale(const std::string &language);
|
||||
static constexpr uint32_t LANGUAGE_LEN = 2;
|
||||
static constexpr uint32_t LOCALE_ITEM_COUNT = 3;
|
||||
static constexpr uint32_t SCRIPT_OFFSET = 2;
|
||||
|
@ -368,14 +368,21 @@ bool LocaleConfig::SetSystemLanguage(const string &language)
|
||||
if (!IsValidTag(language)) {
|
||||
return false;
|
||||
}
|
||||
std::string oldLanguage = GetSystemLanguage();
|
||||
if (SetParameter(LANGUAGE_KEY, language.data()) == 0) {
|
||||
bool isUpdateSuccess = UpdateSystemLocale(language);
|
||||
if(isUpdateSuccess) {
|
||||
#ifdef SUPPORT_GRAPHICS
|
||||
auto appMgrClient = std::make_unique<AppExecFwk::AppMgrClient>();
|
||||
AppExecFwk::Configuration configuration;
|
||||
configuration.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, language);
|
||||
appMgrClient->UpdateConfiguration(configuration);
|
||||
#endif
|
||||
return true;
|
||||
return true;
|
||||
} else {
|
||||
SetParameter(LANGUAGE_KEY, oldLanguage.data());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -1090,6 +1097,43 @@ bool LocaleConfig::GetUsingLocalDigit()
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LocaleConfig::UpdateSystemLocale(const std::string &language)
|
||||
{
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
icu::Locale languageLocale = icu::Locale::forLanguageTag(language.c_str(), status);
|
||||
if (U_FAILURE(status)) {
|
||||
return false;
|
||||
}
|
||||
std::string lang = languageLocale.getLanguage();
|
||||
std::string script = languageLocale.getScript();
|
||||
|
||||
std::string systemLocaleTag = GetSystemLocale();
|
||||
icu::Locale systemLocale = icu::Locale::forLanguageTag(systemLocaleTag.c_str(), status);
|
||||
if (U_FAILURE(status)) {
|
||||
return false;
|
||||
}
|
||||
std::string region = systemLocale.getCountry();
|
||||
|
||||
std::string extendParam;
|
||||
size_t pos = systemLocaleTag.find("-u-");
|
||||
if (pos < systemLocaleTag.length()) {
|
||||
extendParam = systemLocaleTag.substr(pos);
|
||||
}
|
||||
|
||||
std::string finalLocaleTag = lang;
|
||||
std::string splitor = "-";
|
||||
if (script.length() > 0) {
|
||||
finalLocaleTag += splitor + script;
|
||||
}
|
||||
if (region.length() > 0) {
|
||||
finalLocaleTag += splitor + region;
|
||||
}
|
||||
if (extendParam.length() > 0) {
|
||||
finalLocaleTag += extendParam;
|
||||
}
|
||||
return SetSystemLocale(finalLocaleTag);
|
||||
}
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
} // namespace OHOS
|
||||
|
Loading…
x
Reference in New Issue
Block a user