1 /*
2 * Copyright (c) 2020-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 "localization_module.h"
17 #include "ace_event_error_code.h"
18 #if (FEATURE_LOCALIZATION_MODULE == 1)
19 #include "ace_log.h"
20 #include "js_fwk_common.h"
21 #include <stdlib.h>
22
23 namespace OHOS {
24 namespace ACELite {
Init()25 void LocalizationModule::Init()
26 {
27 jerry_value_t globalContext = jerry_get_global_object();
28 const char * const name = "ViewModel";
29 jerry_value_t propertyName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(name));
30 if (JerryHasProperty(globalContext, propertyName)) {
31 // get the prototype of AbilitySlice
32 jerry_value_t currentApp = jerry_get_property(globalContext, propertyName);
33 jerry_value_t protoType = jerryx_get_property_str(currentApp, "prototype");
34 // register $t to the prototype of abilitySlice
35 jerry_value_t functionHandle = jerry_create_external_function(Translate);
36 const char * const propName = "$t";
37 JerrySetNamedProperty(protoType, propName, functionHandle);
38 // register $tc to the prototype of abilitySlice
39 #ifdef LOCALIZATION_PLURAL
40 jerry_value_t pluralHandle = jerry_create_external_function(TranslatePlural);
41 const char * const pluralFuncName = "$tc";
42 JerrySetNamedProperty(protoType, pluralFuncName, pluralHandle);
43 jerry_release_value(pluralHandle);
44 #endif // LOCALIZATION_PLURAL
45 ReleaseJerryValue(functionHandle, protoType, currentApp, VA_ARG_END_FLAG);
46 } else {
47 HILOG_ERROR(HILOG_MODULE_ACE, "app is not create.");
48 }
49 ReleaseJerryValue(propertyName, globalContext, VA_ARG_END_FLAG);
50 }
51
Clear()52 void LocalizationModule::Clear()
53 {
54 if (parser_ != nullptr) {
55 delete parser_;
56 parser_ = nullptr;
57 }
58 }
59
60 #ifdef LOCALIZATION_PLURAL
GetValueByKey(const jerry_value_t * args,const jerry_length_t argsNum,bool isPlural)61 jerry_value_t LocalizationModule::GetValueByKey(const jerry_value_t *args, const jerry_length_t argsNum, bool isPlural)
62 #else
63 jerry_value_t LocalizationModule::GetValueByKey(const jerry_value_t *args, const jerry_length_t argsNum)
64 #endif
65 {
66 if (argsNum == 0) {
67 HILOG_ERROR(HILOG_MODULE_ACE, "GetValueByKey failed: args number error");
68 return UNDEFINED;
69 }
70 LocalizationModule *localization = GetInstance();
71 if (localization->parser_ == nullptr) {
72 localization->parser_ = new CJSONParser();
73 if (localization->parser_ == nullptr) {
74 HILOG_ERROR(HILOG_MODULE_ACE, "create json parser failed");
75 return UNDEFINED;
76 }
77 if (!localization->parser_->Init()) {
78 ACE_ERROR_CODE_PRINT(EXCE_ACE_LOCALIZATION_FAILED, EXCE_ACE_LOCALIZATION_SYSTEM_LANGUAGE_NOT_INITIALIZED);
79 localization->Clear(); // parser init failed, release the resource of this module
80 HILOG_ERROR(HILOG_MODULE_ACE, "GetValueByKey failed: parser initialize error");
81 return UNDEFINED;
82 }
83 }
84 // if system language origion info changed, cache new file to psram/ram
85 CJSONParser::LanguageState change = localization->parser_->ChangeLanguage();
86 if ((change == CJSONParser::LanguageState::LANGUAGE_CHANGED) ||
87 (change == CJSONParser::LanguageState::LANGUAGE_FAIL)) {
88 if (!localization->parser_->CacheFile()) {
89 HILOG_ERROR(HILOG_MODULE_ACE, "cache file failed");
90 }
91 }
92 uint16_t paramStrLength = 0;
93 char *param = MallocStringOf(args[0], ¶mStrLength);
94 if (param == nullptr || paramStrLength == 0) {
95 HILOG_ERROR(HILOG_MODULE_ACE, "GetValueByKey failed: parse key to string error");
96 return UNDEFINED;
97 }
98 jerry_value_t resultProp = localization->parser_->GetValue(param, args, argsNum);
99 if (!jerry_value_is_string(resultProp) && !jerry_value_is_number(resultProp)) {
100 HILOG_ERROR(HILOG_MODULE_ACE,
101 "GetValueByKey failed: the final result error, keyLen[%{public}d]", strlen(param));
102 }
103 ace_free(param);
104 param = nullptr;
105 return resultProp;
106 }
107 } // namespace ACELite
108 } // namespace OHOS
109 #endif // LOCALIZATION_MODULE
110