add i18n interfaces

Signed-off-by: Nmeaty-bag-and-wangwang-meat <zouzhexi@huawei.com>
上级 81da9dfe
/*
* 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();
private:
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 GetDisplayRegion(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 void SetSystemLanguage(napi_env env, napi_callback_info info);
static void SetSystemRegion(napi_env env, napi_callback_info info);
static void 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 "i18n_addon.h"
#include <vector>
#include "hilog/log.h"
#include "node_api.h"
namespace OHOS {
namespace Global {
namespace I18n {
#define GET_PARAMS(env, info, num) \
size_t argc = num; \
napi_value argv[num]; \
napi_value thisVar = nullptr; \
void *data = nullptr; \
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)
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("getDisplayRegion", GetDisplayRegion),
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),
};
napi_status 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)
{
GET_PARAMS(env, info, 0);
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) {
context.SetErrorMsg("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) {
context.SetErrorMsg("Failed to create string item");
return nullptr;
}
status = napi_set_element(env, result, i, value);
if (status != napi_ok) {
context.SetErrorMsg("Failed to set array item");
return nullptr;
}
}
}
return result;
}
napi_value I18nAddon::GetSystemCountries(napi_env env, napi_callback_info info)
{
GET_PARAMS(env, info, 0);
std::vector<std::string> systemCountries;
LocaleConfig::GetSystemCountries(systemCountries);
napi_value result;
napi_status status = napi_create_array_with_length(env, systemCountries.size(), &result);
if (status != napi_ok) {
context.SetErrorMsg("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) {
context.SetErrorMsg("Failed to create string item");
return nullptr;
}
status = napi_set_element(env, result, i, value);
if (status != napi_ok) {
context.SetErrorMsg("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) {
context.SetErrorMsg("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) {
context.SetErrorMsg("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) {
context.SetErrorMsg("Failed to create string item");
return nullptr;
}
return result;
}
napi_value I18nAddon::GetDisplayLanguage(napi_env env, napi_callback_info info)
{
GET_PARAMS(env, info, 3);
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) {
context.SetErrorMsg("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) {
context.SetErrorMsg("Failed to get string item");
return nullptr;
}
bool sentenceCase = true;
if (argv[2] != nullptr) {
napi_get_value_bool(env, argv[2], &sentenceCase);
}
std::string value = LocaleConfig::GetDisplayLanguage(localeBuf.data(), displayLocaleBuf.data(), sentenceCase);
napi_value result;
napi_status status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
context.SetErrorMsg("Failed to create string item");
return nullptr;
}
return result;
}
napi_value I18nAddon::GetDisplayRegion(napi_env env, napi_callback_info info)
{
GET_PARAMS(env, info, 3);
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) {
context.SetErrorMsg("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) {
context.SetErrorMsg("Failed to get string item");
return nullptr;
}
bool sentenceCase = true;
if (argv[2] != nullptr) {
napi_get_value_bool(env, argv[2], &sentenceCase);
}
std::string value = LocaleConfig::GetDisplayRegion(localeBuf.data(), displayLocaleBuf.data(), sentenceCase);
napi_value result;
napi_status status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
context.SetErrorMsg("Failed to create string item");
return nullptr;
}
return result;
}
napi_value I18nAddon::IsSuggested(napi_env env, napi_callback_info info)
{
GET_PARAMS(env, info, 2);
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) {
context.SetErrorMsg("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) {
context.SetErrorMsg("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;
}
void I18nAddon::SetSystemLanguage(napi_env env, napi_callback_info info)
{
GET_PARAMS(env, info, 1);
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) {
context.SetErrorMsg("Failed to get string item");
return nullptr;
}
LocaleConfig::SetSystemLanguage(languageBuf.data());
}
void I18nAddon::SetSystemRegion(napi_env env, napi_callback_info info)
{
GET_PARAMS(env, info, 1);
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) {
context.SetErrorMsg("Failed to get string item");
return nullptr;
}
LocaleConfig::SetSystemLanguage(regionBuf.data());
}
void I18nAddon::SetSystemLocale(napi_env env, napi_callback_info info)
{
GET_PARAMS(env, info, 1);
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) {
context.SetErrorMsg("Failed to get string item");
return nullptr;
}
LocaleConfig::SetSystemLanguage(localeBuf.data());
}
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 AbilityRegister()
{
napi_module_register(&g_i18nModule);
}
} // namespace I18n
} // namespace Global
} // namespace OHOS
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册