123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci
1723b3eb3cSopenharmony_ci#include "napi/native_node_api.h"
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include "frameworks/core/common/ace_application_info.h"
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_cinamespace OHOS::Ace::Napi {
2223b3eb3cSopenharmony_cinamespace {
2323b3eb3cSopenharmony_ci
2423b3eb3cSopenharmony_ciconst char LOCALE_TEXT_DIR_LTR[] = "ltr";
2523b3eb3cSopenharmony_ciconst char LOCALE_TEXT_DIR_RTL[] = "rtl";
2623b3eb3cSopenharmony_ci
2723b3eb3cSopenharmony_ci} // namespace
2823b3eb3cSopenharmony_ci
2923b3eb3cSopenharmony_cistatic napi_value JSConfigurationGetLocale(napi_env env, napi_callback_info info)
3023b3eb3cSopenharmony_ci{
3123b3eb3cSopenharmony_ci    std::string language = OHOS::Ace::AceApplicationInfo::GetInstance().GetLanguage();
3223b3eb3cSopenharmony_ci    std::string countryOrRegion = OHOS::Ace::AceApplicationInfo::GetInstance().GetCountryOrRegion();
3323b3eb3cSopenharmony_ci    std::string dir =
3423b3eb3cSopenharmony_ci        OHOS::Ace::AceApplicationInfo::GetInstance().IsRightToLeft() ? LOCALE_TEXT_DIR_RTL : LOCALE_TEXT_DIR_LTR;
3523b3eb3cSopenharmony_ci    std::string unicodeSetting = OHOS::Ace::AceApplicationInfo::GetInstance().GetUnicodeSetting();
3623b3eb3cSopenharmony_ci    size_t languageLen = language.length();
3723b3eb3cSopenharmony_ci    size_t countryOrRegionLen = countryOrRegion.length();
3823b3eb3cSopenharmony_ci    size_t dirLen = dir.length();
3923b3eb3cSopenharmony_ci    size_t unicodeSettingLen = unicodeSetting.length();
4023b3eb3cSopenharmony_ci    if (languageLen == 0 && countryOrRegionLen == 0 && dirLen == 0 && unicodeSettingLen == 0) {
4123b3eb3cSopenharmony_ci        return nullptr;
4223b3eb3cSopenharmony_ci    }
4323b3eb3cSopenharmony_ci
4423b3eb3cSopenharmony_ci    napi_value resultArray[4] = { 0 };
4523b3eb3cSopenharmony_ci    napi_create_string_utf8(env, language.c_str(), languageLen, &resultArray[0]);
4623b3eb3cSopenharmony_ci    napi_create_string_utf8(env, countryOrRegion.c_str(), countryOrRegionLen, &resultArray[1]);
4723b3eb3cSopenharmony_ci    napi_create_string_utf8(env, dir.c_str(), dirLen, &resultArray[2]);
4823b3eb3cSopenharmony_ci    napi_create_string_utf8(env, unicodeSetting.c_str(), unicodeSettingLen, &resultArray[3]);
4923b3eb3cSopenharmony_ci
5023b3eb3cSopenharmony_ci    napi_value result = nullptr;
5123b3eb3cSopenharmony_ci    napi_create_object(env, &result);
5223b3eb3cSopenharmony_ci    napi_set_named_property(env, result, "language", resultArray[0]);
5323b3eb3cSopenharmony_ci    napi_set_named_property(env, result, "countryOrRegion", resultArray[1]);
5423b3eb3cSopenharmony_ci    napi_set_named_property(env, result, "dir", resultArray[2]);
5523b3eb3cSopenharmony_ci    napi_set_named_property(env, result, "unicodeSetting", resultArray[3]);
5623b3eb3cSopenharmony_ci
5723b3eb3cSopenharmony_ci    return result;
5823b3eb3cSopenharmony_ci}
5923b3eb3cSopenharmony_ci
6023b3eb3cSopenharmony_cistatic napi_value ConfigurationExport(napi_env env, napi_value exports)
6123b3eb3cSopenharmony_ci{
6223b3eb3cSopenharmony_ci    napi_property_descriptor configurationDesc[] = {
6323b3eb3cSopenharmony_ci        DECLARE_NAPI_FUNCTION("getLocale", JSConfigurationGetLocale),
6423b3eb3cSopenharmony_ci    };
6523b3eb3cSopenharmony_ci    NAPI_CALL(env, napi_define_properties(
6623b3eb3cSopenharmony_ci        env, exports, sizeof(configurationDesc) / sizeof(configurationDesc[0]), configurationDesc));
6723b3eb3cSopenharmony_ci    return exports;
6823b3eb3cSopenharmony_ci}
6923b3eb3cSopenharmony_ci
7023b3eb3cSopenharmony_cistatic napi_module configurationModule = {
7123b3eb3cSopenharmony_ci    .nm_version = 1,
7223b3eb3cSopenharmony_ci    .nm_flags = 0,
7323b3eb3cSopenharmony_ci    .nm_filename = nullptr,
7423b3eb3cSopenharmony_ci    .nm_register_func = ConfigurationExport,
7523b3eb3cSopenharmony_ci    .nm_modname = "configuration",
7623b3eb3cSopenharmony_ci    .nm_priv = ((void*)0),
7723b3eb3cSopenharmony_ci    .reserved = { 0 },
7823b3eb3cSopenharmony_ci};
7923b3eb3cSopenharmony_ci
8023b3eb3cSopenharmony_ciextern "C" __attribute__((constructor)) void ConfigurationRegister()
8123b3eb3cSopenharmony_ci{
8223b3eb3cSopenharmony_ci    napi_module_register(&configurationModule);
8323b3eb3cSopenharmony_ci}
8423b3eb3cSopenharmony_ci
8523b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Napi