!233 icu NDK接口默认获取系统语言

Merge pull request !233 from wangkexin/ndk
This commit is contained in:
openharmony_ci 2024-05-13 01:56:30 +00:00 committed by Gitee
commit 7ec7f1a2e8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 35 additions and 83 deletions

View File

@ -531,9 +531,6 @@ ohos_shared_library("shared_icuuc") {
":icu_config",
":static_icustubdata_all_deps_config",
]
external_deps = []
defines = [
"U_ATTRIBUTE_DEPRECATED=",
"U_COMMON_IMPLEMENTATION",
@ -542,10 +539,6 @@ ohos_shared_library("shared_icuuc") {
"UCONFIG_USE_WINDOWS_LCID_MAPPING_API=0",
"_REENTRANT",
]
if (icu_support_libbegetutil && !is_mingw && is_ohos) {
defines += [ "ICU_SUPPORT_LIBBEGETUTIL" ]
external_deps += [ "init:libbegetutil" ]
}
sources = icu_common_source
deps = [
":ohos_icudat",

View File

@ -167,71 +167,6 @@ U_CDECL_END
U_NAMESPACE_BEGIN
#ifdef ICU_SUPPORT_LIBBEGETUTIL
std::string ReadSystemParameter(const char *paramKey)
{
char param[128];
int status = GetParameter(paramKey, "", param, 128);
if (status > 0) {
return param;
}
return "";
}
std::string GetSystemLocale()
{
const char *LOCALE_KEY = "persist.global.locale";
const char *DEFAULT_LOCALE_KEY = "const.global.locale";
std::string systemLocale = ReadSystemParameter(LOCALE_KEY);
if (systemLocale.empty()) {
systemLocale = ReadSystemParameter(DEFAULT_LOCALE_KEY);
}
return systemLocale;
}
Locale *locale_get_default_internal(UErrorCode& status)
{
// Synchronize this entire function.
Mutex lock(&gDefaultLocaleMutex);
std::string locale = GetSystemLocale();
const char *id = locale.c_str();
CharString localeNameBuf;
{
CharStringByteSink sink(&localeNameBuf);
ulocimp_canonicalize(id, sink, &status);
}
if (U_FAILURE(status)) {
return gDefaultLocale;
}
if (gDefaultLocalesHashT == NULL) {
gDefaultLocalesHashT = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status);
if (U_FAILURE(status)) {
return gDefaultLocale;
}
uhash_setValueDeleter(gDefaultLocalesHashT, deleteLocale);
ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
}
Locale *newDefault = (Locale *)uhash_get(gDefaultLocalesHashT, localeNameBuf.data());
if (newDefault == NULL) {
newDefault = new Locale(Locale::eBOGUS);
if (newDefault == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return gDefaultLocale;
}
newDefault->init(localeNameBuf.data(), false);
uhash_put(gDefaultLocalesHashT, (char*) newDefault->getName(), newDefault, &status);
if (U_FAILURE(status)) {
return gDefaultLocale;
}
}
return newDefault;
}
#endif
Locale *locale_set_default_internal(const char *id, UErrorCode& status) {
// Synchronize this entire function.
Mutex lock(&gDefaultLocaleMutex);
@ -2100,11 +2035,7 @@ Locale::getDefault()
}
}
UErrorCode status = U_ZERO_ERROR;
#ifdef ICU_SUPPORT_LIBBEGETUTIL
return *locale_get_default_internal(status);
#else
return *locale_set_default_internal(NULL, status);
#endif
}

View File

@ -35,9 +35,6 @@
#if U_SHOW_CPLUSPLUS_API
#ifdef ICU_SUPPORT_LIBBEGETUTIL
#include "parameter.h"
#endif
#include "unicode/bytestream.h"
#include "unicode/localpointer.h"
#include "unicode/strenum.h"
@ -1160,10 +1157,6 @@ private:
*/
friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
#ifdef ICU_SUPPORT_LIBBEGETUTIL
friend Locale *locale_get_default_internal(UErrorCode& status);
#endif
/**
* @internal (private)
*/

View File

@ -13,6 +13,7 @@
import("//build/ohos.gni")
import("//build/ohos/ndk/ndk.gni")
import("//third_party/icu/icu.gni")
ohos_shared_library("icundk") {
ldflags = [
@ -31,6 +32,12 @@ ohos_shared_library("icundk") {
"//third_party/icu/icu4c:shared_icui18n",
"//third_party/icu/icu4c:shared_icuuc",
]
external_deps = []
defines = []
if (icu_support_libbegetutil && is_ohos) {
defines += [ "ICU_SUPPORT_LIBBEGETUTIL" ]
external_deps += [ "init:libbegetutil" ]
}
version_script = "libicu.map"
output_name = "icu"

View File

@ -32,6 +32,13 @@
#include <unicode/uset.h>
#include <unicode/utrans.h>
#ifdef ICU_SUPPORT_LIBBEGETUTIL
#include "parameter.h"
#endif
#ifdef ICU_SUPPORT_LIBBEGETUTIL
bool g_isCustomLocale = false;
#endif
/* Restore C api definition */
#undef UCNV_TO_U_CALLBACK_ESCAPE
#undef u_charDigitValue
@ -1474,7 +1481,25 @@ int32_t uloc_getCountry(const char * localeID, char * country, int32_t countryCa
return U_ICU_ENTRY_POINT_RENAME(uloc_getCountry)(localeID, country, countryCapacity, err);
}
const char * uloc_getDefault() {
#ifdef ICU_SUPPORT_LIBBEGETUTIL
if (g_isCustomLocale) {
return U_ICU_ENTRY_POINT_RENAME(uloc_getDefault)();
}
static char param[128];
const char *LOCALE_KEY = "persist.global.locale";
const char *DEFAULT_LOCALE_KEY = "const.global.locale";
int status = GetParameter(LOCALE_KEY, "", param, 128);
if (status <= 0) {
status = GetParameter(DEFAULT_LOCALE_KEY, "", param, 128);
}
if (status <= 0) {
return "";
}
return param;
#else
return U_ICU_ENTRY_POINT_RENAME(uloc_getDefault)();
#endif
}
int32_t uloc_getDisplayCountry(const char * locale, const char * displayLocale, UChar * country, int32_t countryCapacity, UErrorCode * status) {
return U_ICU_ENTRY_POINT_RENAME(uloc_getDisplayCountry)(locale, displayLocale, country, countryCapacity, status);
@ -1549,6 +1574,9 @@ UEnumeration * uloc_openKeywords(const char * localeID, UErrorCode * status) {
return U_ICU_ENTRY_POINT_RENAME(uloc_openKeywords)(localeID, status);
}
void uloc_setDefault(const char * localeID, UErrorCode * status) {
#ifdef ICU_SUPPORT_LIBBEGETUTIL
g_isCustomLocale = true;
#endif
U_ICU_ENTRY_POINT_RENAME(uloc_setDefault)(localeID, status);
}
int32_t uloc_setKeywordValue(const char * keywordName, const char * keywordValue, char * buffer, int32_t bufferCapacity, UErrorCode * status) {