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 #ifndef OHOS_RESOURCE_MANAGER_RES_LOCALE_H 17 #define OHOS_RESOURCE_MANAGER_RES_LOCALE_H 18 19 #include <cstdint> 20 #include <cstddef> 21 #include "rstate.h" 22 #include "lock.h" 23 #include "locale_info.h" 24 25 using OHOS::I18N::LocaleInfo; 26 namespace OHOS { 27 namespace Global { 28 namespace Resource { 29 struct ParseResult { 30 const char *tempLanguage = nullptr; 31 const char *tempScript = nullptr; 32 const char *tempRegion = nullptr; 33 int16_t languageTagLen = 0; 34 int16_t scriptTagLen = 0; 35 int16_t regionTagLen = 0; 36 }; 37 38 class ResLocale { 39 public: 40 const char *GetLanguage() const; 41 42 const char *GetRegion() const; 43 44 const char *GetScript() const; 45 46 ResLocale(); 47 48 /** 49 * Copy from other LocaleInfo to this 50 * @param other the other LocaleInfo copy to this localeInfo 51 * @return SUCCESS if copy other LocaleInfo success, else ERROR 52 */ 53 RState CopyFromLocaleInfo(const LocaleInfo *other); 54 55 /** 56 * Copy from other ResLocale to this 57 * @param other the other ResLocale copy to this ResLocale 58 * @return SUCCESS if copy other ResLocale success, else ERROR 59 */ 60 RState Copy(const ResLocale *other); 61 62 static const LocaleInfo *GetDefault(); 63 64 static bool UpdateDefault(const LocaleInfo &localeInfo, bool needNotify); 65 66 /** 67 * Build resLocal from string 68 * @param bcp47String the target string 69 * @param sep the parse string position 70 * @param rState the parse status 71 * @return the resLocal after parse bcp47String 72 */ 73 static ResLocale *BuildFromString(const char *bcp47String, char sep, RState &rState); 74 75 /** 76 * Build resLocal from parts 77 * @param language the resLocal language 78 * @param script the resLocal script 79 * @param region the resLocal region 80 * @param rState the Build status 81 * @return the resLocal after Build resLocal from parts if success, else return nullptr 82 */ 83 static ResLocale *BuildFromParts(const char *language, const char *script, const char *region, RState &rState); 84 85 ~ResLocale(); 86 87 static constexpr uint16_t END_TYPE = 0x0000; 88 // language parse 89 static constexpr uint16_t LANG_TYPE = 0x0001; 90 // script parse 91 static constexpr uint16_t SCRIPT_TYPE = 0x0002; 92 // region parse 93 static constexpr uint16_t REGION_TYPE = 0x0004; 94 95 static constexpr size_t MIN_BCP47_STR_LEN = 2; 96 97 private: 98 RState SetLanguage(const char *language, size_t len); 99 100 RState SetScript(const char *script, size_t len); 101 102 RState SetRegion(const char *region, size_t len); 103 104 static ResLocale *DoParse(const char *bcp47String, char sep, RState &rState); 105 106 static ResLocale *CreateResLocale(ParseResult &parseResult, RState &rState); 107 108 RState Init(const char *language, size_t languageLen, const char *script, size_t scriptLen, 109 const char *region, size_t regionLen); 110 111 const char *language_; 112 113 const char *region_; 114 115 const char *script_; 116 117 static LocaleInfo *defaultLocale_; 118 119 static Lock lock_; 120 121 friend class LocaleMatcher; 122 }; 123 } // namespace Resource 124 } // namespace Global 125 } // namespace OHOS 126 #endif 127