add i18n interfaces

Signed-off-by: Nmeaty-bag-and-wangwang-meat <zouzhexi@huawei.com>
上级 de5320d1
......@@ -77,7 +77,7 @@ export function getSystemLanguage(): string;
* @param language The language to be used.
* @since 7
*/
export function setSystemLanguage(language: string);
export function setSystemLanguage(language: string): boolean;
/**
* Obtain the region currently used by the system.
......@@ -93,7 +93,7 @@ export function getSystemRegion(): string;
* @param region The region to be used.
* @since 7
*/
export function setSystemRegion(region: string);
export function setSystemRegion(region: string): boolean;
/**
* Obtain the locale currently used by the system.
......@@ -109,5 +109,5 @@ export function getSystemLocale(): string;
* @param locale The locale to be used.
* @since 7
*/
export function setSystemLocale(locale: string);
export function setSystemLocale(locale: string): boolean;
}
\ No newline at end of file
......@@ -39,3 +39,31 @@ ohos_shared_library("intl") {
subsystem_name = "global"
part_name = "i18n_standard"
}
ohos_shared_library("i18n") {
include_dirs = [
"//base/global/i18n_standard/frameworks/intl/include",
"//base/global/i18n_standard/interfaces/js/kits/include",
"//base/hiviewdfx/interfaces/native/innerkits/include",
"//foundation/ace/napi/native_engine",
"//foundation/ace/napi/interfaces/kits",
"//third_party/node/src",
"//third_party/icu/icu4c/source",
"//third_party/icu/icu4c/source/common",
"//third_party/icu/icu4c/source/i18n",
]
sources = [ "src/i18n_addon.cpp" ]
deps = [
"//base/global/i18n_standard/frameworks/intl:intl_util",
"//foundation/ace/napi:ace_napi",
"//third_party/icu/icu4c:shared_icui18n",
"//third_party/icu/icu4c:shared_icuuc",
]
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
relative_install_dir = "module"
subsystem_name = "global"
part_name = "i18n_standard"
}
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef I18N_ADDON_H
#define I18N_ADDON_H
#include <string>
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "locale_config.h"
namespace OHOS {
namespace Global {
namespace I18n {
class I18nAddon {
public:
static napi_value Init(napi_env env, napi_value exports);
static void Destructor(napi_env env, void *nativeObject, void *finalize_hint);
I18nAddon();
virtual ~I18nAddon();
static napi_value GetSystemLanguages(napi_env env, napi_callback_info info);
static napi_value GetSystemCountries(napi_env env, napi_callback_info info);
static napi_value IsSuggested(napi_env env, napi_callback_info info);
static napi_value GetDisplayLanguage(napi_env env, napi_callback_info info);
static napi_value GetDisplayCountry(napi_env env, napi_callback_info info);
static napi_value GetSystemLanguage(napi_env env, napi_callback_info info);
static napi_value GetSystemRegion(napi_env env, napi_callback_info info);
static napi_value GetSystemLocale(napi_env env, napi_callback_info info);
static napi_value SetSystemLanguage(napi_env env, napi_callback_info info);
static napi_value SetSystemRegion(napi_env env, napi_callback_info info);
static napi_value SetSystemLocale(napi_env env, napi_callback_info info);
};
} // namespace I18n
} // namespace Global
} // namespace OHOS
#endif
\ No newline at end of file
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <vector>
#include "i18n_addon.h"
#include "hilog/log.h"
#include "node_api.h"
namespace OHOS {
namespace Global {
namespace I18n {
static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "I18nJs" };
using namespace OHOS::HiviewDFX;
I18nAddon::I18nAddon() {}
I18nAddon::~I18nAddon()
{
}
void I18nAddon::Destructor(napi_env env, void *nativeObject, void *hint)
{
if (nativeObject == nullptr) {
return;
}
reinterpret_cast<I18nAddon *>(nativeObject)->~I18nAddon();
}
napi_value I18nAddon::Init(napi_env env, napi_value exports)
{
napi_status status;
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION("getSystemLanguages", GetSystemLanguages),
DECLARE_NAPI_FUNCTION("getSystemCountries", GetSystemCountries),
DECLARE_NAPI_FUNCTION("isSuggested", IsSuggested),
DECLARE_NAPI_FUNCTION("getDisplayLanguage", GetDisplayLanguage),
DECLARE_NAPI_FUNCTION("getDisplayCountry", GetDisplayCountry),
DECLARE_NAPI_FUNCTION("getSystemLanguage", GetSystemLanguage),
DECLARE_NAPI_FUNCTION("getSystemRegion", GetSystemRegion),
DECLARE_NAPI_FUNCTION("getSystemLocale", GetSystemLocale),
DECLARE_NAPI_FUNCTION("setSystemLanguage", SetSystemLanguage),
DECLARE_NAPI_FUNCTION("setSystemRegion", SetSystemRegion),
DECLARE_NAPI_FUNCTION("setSystemLocale", SetSystemLocale),
};
status = napi_define_properties(env, exports,
sizeof(properties) / sizeof(napi_property_descriptor), properties);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to set properties at init");
return nullptr;
}
return exports;
}
napi_value I18nAddon::GetSystemLanguages(napi_env env, napi_callback_info info)
{
std::vector<std::string> systemLanguages;
LocaleConfig::GetSystemLanguages(systemLanguages);
napi_value result;
napi_status status = napi_create_array_with_length(env, systemLanguages.size(), &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create array");
return nullptr;
}
for (size_t i = 0; i < systemLanguages.size(); i++) {
napi_value value;
status = napi_create_string_utf8(env, systemLanguages[i].c_str(), NAPI_AUTO_LENGTH, &value);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create string item");
return nullptr;
}
status = napi_set_element(env, result, i, value);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to set array item");
return nullptr;
}
}
return result;
}
napi_value I18nAddon::GetSystemCountries(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);
size_t len;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
std::vector<char> localeBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[0], localeBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
std::vector<std::string> systemCountries;
LocaleConfig::GetSystemCountries(systemCountries);
napi_value result;
status = napi_create_array_with_length(env, systemCountries.size(), &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create array");
return nullptr;
}
for (size_t i = 0; i < systemCountries.size(); i++) {
napi_value value;
status = napi_create_string_utf8(env, systemCountries[i].c_str(), NAPI_AUTO_LENGTH, &value);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create string item");
return nullptr;
}
status = napi_set_element(env, result, i, value);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to set array item");
return nullptr;
}
}
return result;
}
napi_value I18nAddon::GetSystemLanguage(napi_env env, napi_callback_info info)
{
std::string value = LocaleConfig::GetSystemLanguage();
napi_value result;
napi_status status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create string item");
return nullptr;
}
return result;
}
napi_value I18nAddon::GetSystemRegion(napi_env env, napi_callback_info info)
{
std::string value = LocaleConfig::GetSystemRegion();
napi_value result;
napi_status status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create string item");
return nullptr;
}
return result;
}
napi_value I18nAddon::GetSystemLocale(napi_env env, napi_callback_info info)
{
std::string value = LocaleConfig::GetSystemLocale();
napi_value result;
napi_status status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create string item");
return nullptr;
}
return result;
}
napi_value I18nAddon::GetDisplayLanguage(napi_env env, napi_callback_info info)
{
// Need to get three parameters to get the display Language.
size_t argc = 3;
napi_value argv[3] = { 0 };
napi_value thisVar = nullptr;
void *data = nullptr;
napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
size_t len;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
std::vector<char> localeBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[0], localeBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
napi_get_value_string_utf8(env, argv[1], nullptr, 0, &len);
std::vector<char> displayLocaleBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[1], displayLocaleBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
bool sentenceCase = true;
int sentenceCaseIndex = 2;
if (argv[sentenceCaseIndex] != nullptr) {
napi_get_value_bool(env, argv[sentenceCaseIndex], &sentenceCase);
}
std::string value = LocaleConfig::GetDisplayLanguage(localeBuf.data(), displayLocaleBuf.data(), sentenceCase);
napi_value result;
status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create string item");
return nullptr;
}
return result;
}
napi_value I18nAddon::GetDisplayCountry(napi_env env, napi_callback_info info)
{
// Need to get three parameters to get the display country.
size_t argc = 3;
napi_value argv[3] = { 0 };
napi_value thisVar = nullptr;
void *data = nullptr;
napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
size_t len;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
std::vector<char> localeBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[0], localeBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
napi_get_value_string_utf8(env, argv[1], nullptr, 0, &len);
std::vector<char> displayLocaleBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[1], displayLocaleBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
bool sentenceCase = true;
int sentenceCaseIndex = 2;
if (argv[sentenceCaseIndex] != nullptr) {
napi_get_value_bool(env, argv[sentenceCaseIndex], &sentenceCase);
}
std::string value = LocaleConfig::GetDisplayRegion(localeBuf.data(), displayLocaleBuf.data(), sentenceCase);
napi_value result;
status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create string item");
return nullptr;
}
return result;
}
napi_value I18nAddon::IsSuggested(napi_env env, napi_callback_info info)
{
// Need to get two parameters to check is suggested or not.
size_t argc = 2;
napi_value argv[2] = { 0 };
napi_value thisVar = nullptr;
void *data = nullptr;
napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
size_t len;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
std::vector<char> languageBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[0], languageBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
bool isSuggested = false;
if (argv[1] != nullptr) {
napi_get_value_string_utf8(env, argv[1], nullptr, 0, &len);
std::vector<char> regionBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[1], regionBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
isSuggested = LocaleConfig::IsSuggested(languageBuf.data(), regionBuf.data());
} else {
isSuggested = LocaleConfig::IsSuggested(languageBuf.data());
}
napi_value result;
status = napi_get_boolean(env, isSuggested, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create case first boolean value failed");
return nullptr;
}
return result;
}
napi_value I18nAddon::SetSystemLanguage(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);
size_t len;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
std::vector<char> languageBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[0], languageBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
bool success = LocaleConfig::SetSystemLanguage(languageBuf.data());
napi_value result;
status = napi_get_boolean(env, success, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create set system language boolean value failed");
return nullptr;
}
return result;
}
napi_value I18nAddon::SetSystemRegion(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);
size_t len;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
std::vector<char> regionBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[0], regionBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
bool success = LocaleConfig::SetSystemRegion(regionBuf.data());
napi_value result;
status = napi_get_boolean(env, success, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create set system language boolean value failed");
return nullptr;
}
return result;
}
napi_value I18nAddon::SetSystemLocale(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);
size_t len;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
std::vector<char> localeBuf(len + 1);
status = napi_get_value_string_utf8(env, argv[0], localeBuf.data(), len + 1, &len);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to get string item");
return nullptr;
}
bool success = LocaleConfig::SetSystemLocale(localeBuf.data());
napi_value result;
status = napi_get_boolean(env, success, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create set system language boolean value failed");
return nullptr;
}
return result;
}
napi_value Init(napi_env env, napi_value exports)
{
return I18nAddon::Init(env, exports);
}
static napi_module g_i18nModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "i18n",
.nm_priv = ((void *)0),
.reserved = { 0 }
};
extern "C" __attribute__((constructor)) void I18nRegister()
{
napi_module_register(&g_i18nModule);
}
} // namespace I18n
} // namespace Global
} // namespace OHOS
\ No newline at end of file
......@@ -6,6 +6,7 @@
"//base/global/i18n_standard/frameworks/zone:zone_util",
"//base/global/i18n_standard/frameworks/intl:intl_util",
"//base/global/i18n_standard/interfaces/js/kits:intl",
"//base/global/i18n_standard/interfaces/js/kits:i18n",
"//third_party/icu/icu4c:shared_icuuc",
"//third_party/icu/icu4c:shared_icui18n",
"//third_party/icu/icu4c:ohos_icudat",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册