提供GetThreeLetterRegion,GetThreeLetterLanguage两个接口

Signed-off-by: w00611838 <wangkexin14@huawei.com>
This commit is contained in:
w00611838 2024-04-16 19:35:16 +08:00
parent f6753e5d3f
commit 46770ef31c
6 changed files with 163 additions and 1 deletions

View File

@ -33,6 +33,8 @@ bool GetPseudoLocalizationEnforce();
std::string PseudoLocalizationProcessor(const std::string &input);
std::string PseudoLocalizationProcessor(const std::string &input, bool ifEnforce);
std::string StrReplaceAll(std::string& str, const std::string target, const std::string replace);
std::string GetISO3Language(const std::string language);
std::string GetISO3Country(const std::string country);
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -23,6 +23,7 @@
#include "i18n_hilog.h"
#include "locale_config.h"
#include "parameter.h"
#include "unicode/localebuilder.h"
#include "utils.h"
namespace OHOS {
@ -161,6 +162,34 @@ std::string StrReplaceAll(std::string& str, const std::string target, const std:
}
return result;
}
std::string GetISO3Language(const string language)
{
UErrorCode icuStatus = U_ZERO_ERROR;
icu::Locale locale = icu::Locale::forLanguageTag(language.data(), icuStatus);
if (U_FAILURE(icuStatus) || !IsValidLocaleTag(locale)) {
return nullptr;
}
return locale.getISO3Language();
}
std::string GetISO3Country(const string country)
{
UErrorCode icuStatus = U_ZERO_ERROR;
icu::Locale locale;
if (LocaleConfig::IsValidRegion(country)) {
locale = icu::LocaleBuilder().setLanguage("zh").setRegion(country).build(icuStatus);
} else if (LocaleConfig::IsValidTag(country)) {
locale = icu::Locale::forLanguageTag(country.data(), icuStatus);
} else {
return nullptr;
}
if (U_FAILURE(icuStatus) || !IsValidLocaleTag(locale)) {
return nullptr;
}
return locale.getISO3Country();
}
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -1469,6 +1469,44 @@ HWTEST_F(I18nTest, I18nFuncTest060, TestSize.Level1)
EXPECT_EQ(order11.dateTimeOrder, "01");
EXPECT_EQ(order12.amPmTimeOrder, "10");
}
/**
* @tc.name: I18nFuncTest061
* @tc.desc: Test I18n GetISO3Language
* @tc.type: FUNC
*/
HWTEST_F(I18nTest, I18nFuncTest061, TestSize.Level1)
{
std::string language = GetISO3Language("zh");
EXPECT_EQ(language, "zho");
language = GetISO3Language("en");
EXPECT_EQ(language, "eng");
language = GetISO3Language("zh-CN");
EXPECT_EQ(language, "zho");
language = GetISO3Language("en-US");
EXPECT_EQ(language, "eng");
language = GetISO3Language("sdfsdf");
EXPECT_EQ(language, "");
}
/**
* @tc.name: I18nFuncTest062
* @tc.desc: Test I18n GetISO3Country
* @tc.type: FUNC
*/
HWTEST_F(I18nTest, I18nFuncTest062, TestSize.Level1)
{
std::string country = GetISO3Country("CN");
EXPECT_EQ(country, "CHN");
country = GetISO3Country("US");
EXPECT_EQ(country, "USA");
country = GetISO3Country("zh-CN");
EXPECT_EQ(country, "CHN");
country = GetISO3Country("en-US");
EXPECT_EQ(country, "USA");
country = GetISO3Country("sdfsdf");
EXPECT_EQ(country, "");
}
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -79,6 +79,8 @@ int I18nFuncTest057(void);
int I18nFuncTest058(void);
int I18nFuncTest059(void);
int I18nFuncTest060(void);
int I18nFuncTest061(void);
int I18nFuncTest062(void);
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -52,6 +52,8 @@ public:
static napi_value GetDateOrder(napi_env env, napi_callback_info info);
static napi_value GetTimePeriodName(napi_env env, napi_callback_info info);
static napi_value GetBestMatchLocale(napi_env env, napi_callback_info info);
static napi_value GetThreeLetterLanguage(napi_env env, napi_callback_info info);
static napi_value GetThreeLetterRegion(napi_env env, napi_callback_info info);
static napi_value InitI18nTransliterator(napi_env env, napi_value exports);
static napi_value InitIndexUtil(napi_env env, napi_value exports);
static napi_value InitI18nUtil(napi_env env, napi_value exports);

View File

@ -66,7 +66,9 @@ napi_value I18nAddon::InitI18nUtil(napi_env env, napi_value exports)
DECLARE_NAPI_STATIC_FUNCTION("unitConvert", UnitConvert),
DECLARE_NAPI_STATIC_FUNCTION("getDateOrder", GetDateOrder),
DECLARE_NAPI_STATIC_FUNCTION("getTimePeriodName", GetTimePeriodName),
DECLARE_NAPI_STATIC_FUNCTION("getBestMatchLocale", GetBestMatchLocale)
DECLARE_NAPI_STATIC_FUNCTION("getBestMatchLocale", GetBestMatchLocale),
DECLARE_NAPI_STATIC_FUNCTION("getThreeLetterLanguage", GetThreeLetterLanguage),
DECLARE_NAPI_STATIC_FUNCTION("getThreeLetterRegion", GetThreeLetterRegion)
};
napi_value constructor = nullptr;
napi_status status = napi_define_class(env, "I18NUtil", NAPI_AUTO_LENGTH, JSUtils::DefaultConstructor, nullptr,
@ -440,6 +442,93 @@ napi_value I18nAddon::GetBestMatchLocale(napi_env env, napi_callback_info info)
return result;
}
napi_value I18nAddon::GetThreeLetterLanguage(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1] = { 0 };
napi_value thisVar = nullptr;
void *data = nullptr;
napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
if (status != napi_ok) {
HILOG_ERROR_I18N("GetThreeLetterLanguage napi get param error.");
return nullptr;
} else if (argc < 1) {
HILOG_ERROR_I18N("GetThreeLetterLanguage param argc < 1.");
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
return nullptr;
}
napi_valuetype valueType = napi_valuetype::napi_undefined;
napi_typeof(env, argv[0], &valueType);
if (valueType != napi_valuetype::napi_string) {
HILOG_ERROR_I18N("GetThreeLetterLanguage param not valid.");
ErrorUtil::NapiThrow(env, I18N_NOT_VALID, true);
return nullptr;
}
int32_t code = 0;
std::string languageTag = VariableConvertor::GetString(env, argv[0], code);
if (code != 0) {
HILOG_ERROR_I18N("GetThreeLetterLanguage get param locale failed.");
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
return nullptr;
}
std::string language = GetISO3Language(languageTag);
napi_value result;
status = napi_create_string_utf8(env, language.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
HILOG_ERROR_I18N("GetThreeLetterLanguage Failed to create string item");
return nullptr;
}
return result;
}
napi_value I18nAddon::GetThreeLetterRegion(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1] = { 0 };
napi_value thisVar = nullptr;
void *data = nullptr;
napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
if (status != napi_ok) {
HILOG_ERROR_I18N("GetThreeLetterRegion napi get param error.");
return nullptr;
} else if (argc < 1) {
HILOG_ERROR_I18N("GetThreeLetterRegion param argc < 1.");
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
return nullptr;
}
napi_valuetype valueType = napi_valuetype::napi_undefined;
napi_typeof(env, argv[0], &valueType);
if (valueType != napi_valuetype::napi_string) {
HILOG_ERROR_I18N("GetThreeLetterRegion param not valid.");
ErrorUtil::NapiThrow(env, I18N_NOT_VALID, true);
return nullptr;
}
int32_t code = 0;
std::string regionTag = VariableConvertor::GetString(env, argv[0], code);
if (code != 0) {
HILOG_ERROR_I18N("GetThreeLetterRegion get param locale failed.");
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
return nullptr;
}
std::string country = GetISO3Country(regionTag);
napi_value result;
status = napi_create_string_utf8(env, country.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
HILOG_ERROR_I18N("GetThreeLetterRegion Failed to create string item");
return nullptr;
}
return result;
}
void I18nAddon::ProcessNormal(char ch, int *order, size_t orderSize, int *lengths, size_t lengsSize)
{
char adjust;