• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "locale_module.h"
17 #include "global.h"
18 #include "securec.h"
19 
20 namespace OHOS {
21 namespace ACELite {
22 const char * const LocaleModule::TEXT_DIRECTION_LTR = "ltr";
23 // caller must free the returned value by this function
GetLanguage(void)24 static char *GetLanguage(void)
25 {
26     char *lang = reinterpret_cast<char *>(ace_malloc(MAX_LANGUAGE_LENGTH));
27     if (lang == nullptr) {
28         return nullptr;
29     }
30     (void)memset_s(lang, MAX_LANGUAGE_LENGTH, 0x0, MAX_LANGUAGE_LENGTH);
31     if (GLOBAL_GetLanguage(lang, MAX_LANGUAGE_LENGTH) != 0) {
32         ace_free(lang);
33         return nullptr;
34     }
35     return lang;
36 }
37 
38 // caller must free the returned value by this function
GetRegion(void)39 static char *GetRegion(void)
40 {
41     char *region = reinterpret_cast<char *>(ace_malloc(MAX_REGION_LENGTH));
42     if (region == nullptr) {
43         return nullptr;
44     }
45     (void)memset_s(region, MAX_REGION_LENGTH, 0x0, MAX_REGION_LENGTH);
46     if (GLOBAL_GetRegion(region, MAX_REGION_LENGTH) != 0) {
47         ace_free(region);
48         return nullptr;
49     }
50     return region;
51 }
52 
GetTextDirection(void)53 static const char *GetTextDirection(void)
54 {
55     return LocaleModule::TEXT_DIRECTION_LTR;
56 }
57 
InitLocaleModule(JSIValue exports)58 void InitLocaleModule(JSIValue exports)
59 {
60     JSI::SetModuleAPI(exports, "getLocale", LocaleModule::GetLocale);
61 }
62 
GetLocale(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)63 JSIValue LocaleModule::GetLocale(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
64 {
65     JSIValue result = JSI::CreateObject();
66     char *lang = GetLanguage();
67     if (lang == nullptr) {
68         JSI::ReleaseValue(result);
69         return JSI::CreateUndefined();
70     }
71     JSI::SetStringProperty(result, "language", lang);
72     ace_free(lang);
73 
74     char *region = GetRegion();
75     if (region == nullptr) {
76         JSI::ReleaseValue(result);
77         return JSI::CreateUndefined();
78     }
79     JSI::SetStringProperty(result, "countryOrRegion", region);
80     ace_free(region);
81 
82     const char *dir = GetTextDirection();
83     if (dir == nullptr) {
84         JSI::ReleaseValue(result);
85         return JSI::CreateUndefined();
86     }
87     JSI::SetStringProperty(result, "dir", dir);
88     return result;
89 }
90 } // namespace ACELite
91 } // namespace OHOS
92