1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 
17 #include "napi/native_node_api.h"
18 
19 #include "frameworks/core/common/ace_application_info.h"
20 
21 namespace OHOS::Ace::Napi {
22 namespace {
23 
24 const char LOCALE_TEXT_DIR_LTR[] = "ltr";
25 const char LOCALE_TEXT_DIR_RTL[] = "rtl";
26 
27 } // namespace
28 
JSConfigurationGetLocale(napi_env env, napi_callback_info info)29 static napi_value JSConfigurationGetLocale(napi_env env, napi_callback_info info)
30 {
31     std::string language = OHOS::Ace::AceApplicationInfo::GetInstance().GetLanguage();
32     std::string countryOrRegion = OHOS::Ace::AceApplicationInfo::GetInstance().GetCountryOrRegion();
33     std::string dir =
34         OHOS::Ace::AceApplicationInfo::GetInstance().IsRightToLeft() ? LOCALE_TEXT_DIR_RTL : LOCALE_TEXT_DIR_LTR;
35     std::string unicodeSetting = OHOS::Ace::AceApplicationInfo::GetInstance().GetUnicodeSetting();
36     size_t languageLen = language.length();
37     size_t countryOrRegionLen = countryOrRegion.length();
38     size_t dirLen = dir.length();
39     size_t unicodeSettingLen = unicodeSetting.length();
40     if (languageLen == 0 && countryOrRegionLen == 0 && dirLen == 0 && unicodeSettingLen == 0) {
41         return nullptr;
42     }
43 
44     napi_value resultArray[4] = { 0 };
45     napi_create_string_utf8(env, language.c_str(), languageLen, &resultArray[0]);
46     napi_create_string_utf8(env, countryOrRegion.c_str(), countryOrRegionLen, &resultArray[1]);
47     napi_create_string_utf8(env, dir.c_str(), dirLen, &resultArray[2]);
48     napi_create_string_utf8(env, unicodeSetting.c_str(), unicodeSettingLen, &resultArray[3]);
49 
50     napi_value result = nullptr;
51     napi_create_object(env, &result);
52     napi_set_named_property(env, result, "language", resultArray[0]);
53     napi_set_named_property(env, result, "countryOrRegion", resultArray[1]);
54     napi_set_named_property(env, result, "dir", resultArray[2]);
55     napi_set_named_property(env, result, "unicodeSetting", resultArray[3]);
56 
57     return result;
58 }
59 
ConfigurationExport(napi_env env, napi_value exports)60 static napi_value ConfigurationExport(napi_env env, napi_value exports)
61 {
62     napi_property_descriptor configurationDesc[] = {
63         DECLARE_NAPI_FUNCTION("getLocale", JSConfigurationGetLocale),
64     };
65     NAPI_CALL(env, napi_define_properties(
66         env, exports, sizeof(configurationDesc) / sizeof(configurationDesc[0]), configurationDesc));
67     return exports;
68 }
69 
70 static napi_module configurationModule = {
71     .nm_version = 1,
72     .nm_flags = 0,
73     .nm_filename = nullptr,
74     .nm_register_func = ConfigurationExport,
75     .nm_modname = "configuration",
76     .nm_priv = ((void*)0),
77     .reserved = { 0 },
78 };
79 
ConfigurationRegister()80 extern "C" __attribute__((constructor)) void ConfigurationRegister()
81 {
82     napi_module_register(&configurationModule);
83 }
84 
85 } // namespace OHOS::Ace::Napi