mirror of
https://gitee.com/openharmony/global_i18n
synced 2024-11-30 18:51:33 +00:00
fix bug: GetSystemRegion
Signed-off-by: sunyaozu <sunyaozu@huawei.com>
This commit is contained in:
parent
51e6f3da8e
commit
1e09e4a97b
@ -20,6 +20,8 @@ namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
enum I18nErrorCode {
|
||||
NO_PERMISSION = -3,
|
||||
INVALID_PARAMETER = -2,
|
||||
FAILED = -1,
|
||||
SUCCESS = 0,
|
||||
};
|
||||
|
@ -299,19 +299,29 @@ string LocaleConfig::GetSystemLanguage()
|
||||
|
||||
string LocaleConfig::GetSystemRegion()
|
||||
{
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
const char *country = nullptr;
|
||||
std::string systemRegion = ReadSystemParameter(LOCALE_KEY, CONFIG_LEN);
|
||||
if (!systemRegion.empty()) {
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
icu::Locale origin = icu::Locale::forLanguageTag(systemRegion, status);
|
||||
if (status == U_ZERO_ERROR) {
|
||||
const char *country = origin.getCountry();
|
||||
if (U_SUCCESS(status)) {
|
||||
country = origin.getCountry();
|
||||
if (country != nullptr) {
|
||||
return country;
|
||||
}
|
||||
}
|
||||
}
|
||||
systemRegion = ReadSystemParameter(DEFAULT_LOCALE_KEY, CONFIG_LEN);
|
||||
return systemRegion;
|
||||
if (!systemRegion.empty()) {
|
||||
icu::Locale origin = icu::Locale::forLanguageTag(systemRegion, status);
|
||||
if (U_SUCCESS(status)) {
|
||||
country = origin.getCountry();
|
||||
if (country != nullptr) {
|
||||
return country;
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
string LocaleConfig::GetSystemLocale()
|
||||
|
@ -71,13 +71,15 @@ bool PreferredLanguage::AddPreferredLanguageNonExist(std::vector<std::string> &p
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int index)
|
||||
void PreferredLanguage::AddPreferredLanguage(const std::string &language, int index, I18nErrorCode &errorCode)
|
||||
{
|
||||
if (!LocaleConfig::CheckPermission()) {
|
||||
return false;
|
||||
errorCode = I18nErrorCode::NO_PERMISSION;
|
||||
return;
|
||||
}
|
||||
if (!IsValidTag(language)) {
|
||||
return false;
|
||||
errorCode = I18nErrorCode::INVALID_PARAMETER;
|
||||
return;
|
||||
}
|
||||
std::vector<std::string> preferredLanguageList = GetPreferredLanguageList();
|
||||
int idx = index;
|
||||
@ -104,7 +106,8 @@ bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int in
|
||||
status = AddPreferredLanguageExist(preferredLanguageList, languageIdx, idx, language);
|
||||
}
|
||||
if (!status) {
|
||||
return false;
|
||||
errorCode = I18nErrorCode::FAILED;
|
||||
return;
|
||||
}
|
||||
std::string result = "";
|
||||
for (size_t i = 0; i < preferredLanguageList.size(); i++) {
|
||||
@ -112,11 +115,9 @@ bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int in
|
||||
result += ";";
|
||||
}
|
||||
result.pop_back();
|
||||
if (result.length() > CONFIG_LEN) {
|
||||
return false;
|
||||
if (result.length() > CONFIG_LEN || SetParameter(PREFERRED_LANGUAGES, result.data()) != 0) {
|
||||
errorCode = I18nErrorCode::FAILED;
|
||||
}
|
||||
status = SetParameter(PREFERRED_LANGUAGES, result.data()) == 0;
|
||||
return status;
|
||||
}
|
||||
|
||||
bool PreferredLanguage::RemovePreferredLanguage(int index)
|
||||
|
@ -56,7 +56,9 @@ HWTEST_F(I18nTest, I18nFuncTest001, TestSize.Level1)
|
||||
EXPECT_TRUE(preferredLocale.size() > 0);
|
||||
string systemLocale = "zh-Hans-CN";
|
||||
if (LocaleConfig::SetSystemLocale(systemLocale)) {
|
||||
if (PreferredLanguage::AddPreferredLanguage("en-US", 0)) {
|
||||
I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
|
||||
PreferredLanguage::AddPreferredLanguage("en-US", 0, errorCode);
|
||||
if (errorCode == I18nErrorCode::SUCCESS) {
|
||||
preferredLocale = PreferredLanguage::GetPreferredLocale();
|
||||
EXPECT_EQ(preferredLocale, "en-CN");
|
||||
}
|
||||
|
@ -3036,15 +3036,23 @@ napi_value I18nAddon::AddPreferredLanguageImpl(napi_env env, napi_callback_info
|
||||
ErrorUtil::NapiThrow(env, I18N_NOT_VALID, throwError);
|
||||
return nullptr;
|
||||
}
|
||||
bool success = PreferredLanguage::AddPreferredLanguage(language.data(), index);
|
||||
I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
|
||||
PreferredLanguage::AddPreferredLanguage(language.data(), index, errorCode);
|
||||
if (throwError) {
|
||||
if (!success) {
|
||||
if (errorCode == I18nErrorCode::NO_PERMISSION) {
|
||||
ErrorUtil::NapiThrow(env, I18N_NO_PERMISSION, throwError);
|
||||
}
|
||||
if (errorCode == I18nErrorCode::INVALID_PARAMETER || errorCode == I18nErrorCode::FAILED) {
|
||||
ErrorUtil::NapiThrow(env, I18N_NOT_VALID, throwError);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
bool addResult = true;
|
||||
if (errorCode != I18nErrorCode::SUCCESS) {
|
||||
addResult = false;
|
||||
}
|
||||
napi_value result = nullptr;
|
||||
status = napi_get_boolean(env, success, &result);
|
||||
status = napi_get_boolean(env, addResult, &result);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "addPreferrdLanguage: create boolean result failed");
|
||||
return nullptr;
|
||||
|
@ -18,13 +18,14 @@
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "i18n_types.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
class PreferredLanguage {
|
||||
public:
|
||||
static bool AddPreferredLanguage(const std::string& language, int index);
|
||||
static void AddPreferredLanguage(const std::string& language, int index, I18nErrorCode &errorCode);
|
||||
static bool RemovePreferredLanguage(int index);
|
||||
static std::vector<std::string> GetPreferredLanguageList();
|
||||
static std::string GetFirstPreferredLanguage();
|
||||
|
Loading…
Reference in New Issue
Block a user