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 #include <string>
17
18 #include "napi/native_api.h"
19 #include "napi/native_node_api.h"
20
21 #include "frameworks/core/common/ace_application_info.h"
22
23 namespace OHOS::Ace::Napi {
24 namespace {
25
26 const char LOCALE_TEXT_DIR_LTR[] = "ltr";
27 const char LOCALE_TEXT_DIR_RTL[] = "rtl";
28
29 } // namespace
30
JSConfigurationGetLocale(napi_env env,napi_callback_info info)31 static napi_value JSConfigurationGetLocale(napi_env env, napi_callback_info info)
32 {
33 std::string language = OHOS::Ace::AceApplicationInfo::GetInstance().GetLanguage();
34 std::string countryOrRegion = OHOS::Ace::AceApplicationInfo::GetInstance().GetCountryOrRegion();
35 std::string dir =
36 OHOS::Ace::AceApplicationInfo::GetInstance().IsRightToLeft() ? LOCALE_TEXT_DIR_RTL : LOCALE_TEXT_DIR_LTR;
37 std::string unicodeSetting = OHOS::Ace::AceApplicationInfo::GetInstance().GetUnicodeSetting();
38 size_t languageLen = language.length();
39 size_t countryOrRegionLen = countryOrRegion.length();
40 size_t dirLen = dir.length();
41 size_t unicodeSettingLen = unicodeSetting.length();
42 if (languageLen == 0 && countryOrRegionLen == 0 && dirLen == 0 && unicodeSettingLen == 0) {
43 return nullptr;
44 }
45
46 napi_value resultArray[4] = { 0 };
47 napi_create_string_utf8(env, language.c_str(), languageLen, &resultArray[0]);
48 napi_create_string_utf8(env, countryOrRegion.c_str(), countryOrRegionLen, &resultArray[1]);
49 napi_create_string_utf8(env, dir.c_str(), dirLen, &resultArray[2]);
50 napi_create_string_utf8(env, unicodeSetting.c_str(), unicodeSettingLen, &resultArray[3]);
51
52 napi_value result = nullptr;
53 napi_create_object(env, &result);
54 napi_set_named_property(env, result, "language", resultArray[0]);
55 napi_set_named_property(env, result, "countryOrRegion", resultArray[1]);
56 napi_set_named_property(env, result, "dir", resultArray[2]);
57 napi_set_named_property(env, result, "unicodeSetting", resultArray[3]);
58
59 return result;
60 }
61
ConfigurationExport(napi_env env,napi_value exports)62 static napi_value ConfigurationExport(napi_env env, napi_value exports)
63 {
64 napi_property_descriptor configurationDesc[] = {
65 DECLARE_NAPI_FUNCTION("getLocale", JSConfigurationGetLocale),
66 };
67 NAPI_CALL(env, napi_define_properties(
68 env, exports, sizeof(configurationDesc) / sizeof(configurationDesc[0]), configurationDesc));
69 return exports;
70 }
71
72 static napi_module configurationModule = {
73 .nm_version = 1,
74 .nm_flags = 0,
75 .nm_filename = nullptr,
76 .nm_register_func = ConfigurationExport,
77 .nm_modname = "configuration",
78 .nm_priv = ((void*)0),
79 .reserved = { 0 },
80 };
81
ConfigurationRegister()82 extern "C" __attribute__((constructor)) void ConfigurationRegister()
83 {
84 napi_module_register(&configurationModule);
85 }
86
87 } // namespace OHOS::Ace::Napi