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 #ifndef OHOS_ACELITE_LOCALIZATION_MODULE_H 16 #define OHOS_ACELITE_LOCALIZATION_MODULE_H 17 18 #include "acelite_config.h" 19 #include "non_copyable.h" 20 #if (defined(FEATURE_LOCALIZATION_MODULE) && (FEATURE_LOCALIZATION_MODULE == 1)) 21 #include "cjson_parser.h" 22 #include "presets/preset_module.h" 23 namespace OHOS { 24 namespace ACELite { 25 class LocalizationModule final { 26 public: 27 ACE_DISALLOW_COPY_AND_MOVE(LocalizationModule); 28 void Clear(); 29 30 void Init(); 31 GetInstance()32 static LocalizationModule *GetInstance() 33 { 34 static LocalizationModule localizationModule; 35 return &localizationModule; 36 } Translate(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argNum)37 static jerry_value_t Translate(const jerry_value_t func, 38 const jerry_value_t context, 39 const jerry_value_t *args, 40 const jerry_length_t argNum) 41 { 42 #ifdef LOCALIZATION_PLURAL 43 return GetValueByKey(args, argNum, false); 44 #else 45 return GetValueByKey(args, argNum); 46 #endif 47 } 48 TranslatePlural(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argNum)49 static jerry_value_t TranslatePlural(const jerry_value_t func, 50 const jerry_value_t context, 51 const jerry_value_t *args, 52 const jerry_length_t argNum) 53 { 54 #ifdef LOCALIZATION_PLURAL 55 return GetValueByKey(args, argNum, true); 56 #else 57 return GetValueByKey(args, argNum); 58 #endif 59 } 60 61 #ifdef LOCALIZATION_PLURAL 62 static jerry_value_t GetValueByKey(const jerry_value_t *args, const jerry_length_t argsNum, bool isPlural); 63 #else 64 static jerry_value_t GetValueByKey(const jerry_value_t *args, const jerry_length_t argsNum); 65 #endif 66 67 private: LocalizationModule()68 LocalizationModule() : parser_(nullptr) {} ~LocalizationModule()69 ~LocalizationModule() 70 { 71 Clear(); 72 } 73 CJSONParser *parser_; 74 }; 75 } // namespace ACELite 76 } // namespace OHOS 77 #endif // LOCALIZATION_MODULE 78 79 namespace OHOS { 80 namespace ACELite { 81 class LocalModule final { 82 public: 83 ACE_DISALLOW_COPY_AND_MOVE(LocalModule); 84 LocalModule() = default; 85 ~LocalModule() = default; Load()86 static void Load() 87 { 88 #if (defined(FEATURE_LOCALIZATION_MODULE) && (FEATURE_LOCALIZATION_MODULE == 1)) 89 LocalizationModule *localizationModule = LocalizationModule::GetInstance(); 90 localizationModule->Init(); 91 #endif 92 } Clear()93 static void Clear() 94 { 95 #if (defined(FEATURE_LOCALIZATION_MODULE) && (FEATURE_LOCALIZATION_MODULE == 1)) 96 LocalizationModule *localization = LocalizationModule::GetInstance(); 97 localization->Clear(); 98 #endif 99 } 100 }; 101 } // namespace ACELite 102 } // namespace OHOS 103 #endif // OHOS_ACELITE_LOCALIZATION_MODULE_H 104