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 <unicode/locid.h> 22 #include "rstate.h" 23 #include "lock.h" 24 25 using icu::Locale; 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 RState CopyFromLocaleInfo(const Locale *other); 49 50 RState Copy(const ResLocale *other); 51 52 static const Locale *GetDefault(); 53 54 static bool UpdateDefault(const Locale &localeInfo, bool needNotify); 55 56 static ResLocale *BuildFromString(const char *bcp47String, char sep, RState &rState); 57 58 static ResLocale *BuildFromParts(const char *language, const char *script, const char *region, RState &rState); 59 60 ~ResLocale(); 61 62 static constexpr uint16_t END_TYPE = 0x0000; 63 // language parse 64 static constexpr uint16_t LANG_TYPE = 0x0001; 65 // script parse 66 static constexpr uint16_t SCRIPT_TYPE = 0x0002; 67 // region parse 68 static constexpr uint16_t REGION_TYPE = 0x0004; 69 70 static constexpr size_t MIN_BCP47_STR_LEN = 2; 71 72 private: 73 RState SetLanguage(const char *language, size_t len); 74 75 RState SetScript(const char *script, size_t len); 76 77 RState SetRegion(const char *region, size_t len); 78 79 static ResLocale *DoParse(const char *bcp47String, char sep, RState &rState); 80 81 static ResLocale *CreateResLocale(ParseResult &parseResult, RState &rState); 82 83 RState Init(const char *language, size_t languageLen, const char *script, size_t scriptLen, 84 const char *region, size_t regionLen); 85 86 const char *language_; 87 88 const char *region_; 89 90 const char *script_; 91 92 static Locale *defaultLocale_; 93 94 static Lock lock_; 95 96 friend class LocaleMatcher; 97 }; 98 } // namespace Resource 99 } // namespace Global 100 } // namespace OHOS 101 #endif 102