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 LOCINFO_H 17 #define LOCINFO_H 18 19 /** 20 * @addtogroup I18N 21 * @{ 22 * 23 * @brief Provides functions related to internationalization (i18n), with which you can format date, time and numbers. 24 * 25 * @since 2.2 26 * @version 1.0 27 */ 28 29 /** 30 * @file locale_info.h 31 * 32 * @brief Declares functions for obtaining locale information, including language, script, and country/region. 33 * 34 * Example code: \n 35 * Creating a <b>LocaleInfo</b> instance: \n 36 * {@code LocaleInfo locale("zh", "Hans", "CN");} 37 * Obtaining the language: \n 38 * {@code const char *language = locale.GetLanguage();} 39 * Output: \n 40 * zh 41 * 42 * @since 2.2 43 * @version 1.0 44 */ 45 46 #include <cstdint> 47 #include <string> 48 #include <set> 49 #include "types.h" 50 51 namespace OHOS { 52 namespace I18N { 53 class LocaleInfo { 54 public: 55 /** 56 * @brief A constructor used to create a <b>LocaleInfo</b> instance with specified language, 57 * script, and country/region. 58 * 59 * @param lang Indicates the pointer to the specified language. 60 * @param script Indicates the pointer to the specified script. 61 * @param region Indicates the pointer to the specified country/region. 62 * @since 2.2 63 * @version 1.0 64 */ 65 LocaleInfo(const char *lang, const char *script, const char *region); 66 67 /** 68 * @brief A constructor used to create a <b>LocaleInfo</b> instance with specified language and country/region. 69 * 70 * @param lang Indicates the pointer to the specified language. 71 * @param region Indicates the pointer to the specified country/region. 72 * @since 2.2 73 * @version 1.0 74 */ 75 LocaleInfo(const char *lang, const char *region); 76 77 /** 78 * @brief A constructor used to create a <b>LocaleInfo</b> instance by copying a specified one. 79 * 80 * @param locale Indicates the specified <b>LocaleInfo</b> instance. 81 * @since 2.2 82 * @version 1.0 83 */ 84 LocaleInfo(const LocaleInfo& locale); 85 86 /** 87 * @brief Default constructor used to create a <b>LocaleInfo</b> instance. 88 * 89 * @since 2.2 90 * @version 1.0 91 */ 92 LocaleInfo(); 93 94 /** 95 * @brief A destructor used to delete the <b>LocaleInfo</b> instance. 96 * 97 * @since 2.2 98 * @version 1.0 99 */ 100 virtual ~LocaleInfo(); 101 102 /** 103 * @brief Checks whether this <b>LocaleInfo</b> object equals a specified one. 104 * 105 * @param other Indicates the <b>LocaleInfo</b> object to compare. 106 * @return Returns <b>true</b> if the two objects are equal; returns <b>false</b> otherwise. 107 * @since 2.2 108 * @version 1.0 109 */ 110 virtual bool operator ==(const LocaleInfo &other) const; 111 112 /** 113 * @brief Creates a new <b>LocaleInfo</b> object based on a specified one. 114 * 115 * @param other Indicates the specified <b>LocaleInfo</b> object. 116 * @return Returns the new <b>LocaleInfo</b> object. 117 * @since 2.2 118 * @version 1.0 119 */ 120 virtual LocaleInfo &operator =(const LocaleInfo &other); 121 122 /** 123 * @brief Obtains the ID of this <b>LocaleInfo</b> object, which consists of the language, 124 * script, and country/region. 125 * 126 * @return Returns the ID. 127 * @since 2.2 128 * @version 1.0 129 */ 130 const char *GetId() const; 131 132 /** 133 * @brief Obtains the language specified in this <b>LocaleInfo</b> object. 134 * 135 * @return Returns the language. 136 * @since 2.2 137 * @version 1.0 138 */ 139 const char *GetLanguage() const; 140 141 /** 142 * @brief Obtains the script specified in this <b>LocaleInfo</b> object. 143 * 144 * @return Returns the script. 145 * @since 2.2 146 * @version 1.0 147 */ 148 const char *GetScript() const; 149 150 /** 151 * @brief Obtains the country/region specified in this <b>LocaleInfo</b> object. 152 * 153 * @return Returns the country/region. 154 * @since 2.2 155 * @version 1.0 156 */ 157 const char *GetRegion() const; 158 159 /** 160 * @brief Obtains the mask of this <b>LocaleInfo</b> object. 161 * 162 * @return Returns the mask. 163 * @since 2.2 164 * @version 1.0 165 */ 166 uint32_t GetMask() const; 167 168 /** 169 * @brief Checks whether this <b>LocaleInfo</b> object represents the default locale (en-US). 170 * 171 * @return Returns <b>true</b> if the <b>LocaleInfo</b> object represents the default locale; 172 * returns <b>false</b> otherwise. 173 * @since 2.2 174 * @version 1.0 175 */ 176 bool IsDefaultLocale() const; 177 178 /** 179 * @brief Parse a language tag, and returns an associated <b>LocaleInfo</b> instance. 180 * 181 * @param languageTag Indicates the language tag, which is to be parsed. 182 * @param status Indicates the status of the creating process. 183 * @return Returns the associated LocaleInfo instances. 184 */ 185 static LocaleInfo ForLanguageTag(const char *languageTag, I18nStatus &status); 186 187 /** 188 * @brief Get extension subtag associated with the key. 189 * 190 * @param key Get the extension subtag using the key. 191 * @return Returns the subtag 192 */ 193 const char *GetExtension(const char *key); 194 private: 195 bool ChangeLanguageCode(char *lang, const int32_t dstSize, const char *src, const int32_t srcSize) const; 196 void FreeResource(); 197 static void ProcessExtension(LocaleInfo &locale, const char *key, const char *value); 198 static void ConfirmTagType(const char *start, size_t length, uint8_t &type, const char* &key, const char* &value); 199 static void ParseLanguageTag(LocaleInfo &locale, const char *languageTag, I18nStatus &status); 200 static bool ParseNormalSubTag(LocaleInfo &locale, const char *start, size_t tagLength, uint16_t &options, 201 uint8_t &type); 202 static bool IsLanguage(const char *start, uint8_t length); 203 static bool IsScript(const char *start, uint8_t length); 204 static bool IsRegion(const char *start, uint8_t length); 205 void InitIdstr(); 206 char *language = nullptr; 207 char *script = nullptr; 208 char *region = nullptr; 209 char *id = nullptr; 210 char *numberDigits = nullptr; 211 bool isSucc = true; 212 bool IsSuccess(); 213 void SetFail(); 214 void Init(const char *lang, const char *script, const char *region, int &status); 215 const int CHAR_OFF = 48; 216 static const std::set<std::string> SCRIPTS; 217 static constexpr uint16_t OPT_LANG = 0x0001; 218 static constexpr uint16_t OPT_SCRIPT = 0x0002; 219 static constexpr uint16_t OPT_REGION = 0x0004; 220 static constexpr uint16_t OPT_EXTENSION = 0x0008; 221 static constexpr uint8_t TAG_COMMON = 0; 222 static constexpr uint8_t TAG_U = 1; 223 static constexpr uint8_t TAG_KEY = 2; 224 static constexpr uint8_t TAG_VALUE = 3; 225 static constexpr int LANGUAGE_MIN_LENGTH = 2; 226 static constexpr int LANGUAGE_MAX_LENGTH = 3; 227 static constexpr int REGION_LENGTH = 2; 228 static constexpr int SCRIPT_LENGTH = 4; 229 }; 230 231 enum ESupportScript { 232 NOKOWN = 0x0, 233 LATN = 0x1, 234 HANS = 0x2, 235 HANT = 0x3, 236 QAAG = 0x4, 237 CYRL = 0x5, 238 DEVA = 0x6, 239 GURU = 0x7 240 }; 241 242 enum EMask { 243 REGION_FIRST_LETTER = 7, 244 SCRIPT_BEGIN = 14, 245 LANG_SECOND_BEGIN = 18, 246 LANG_FIRST_BEGIN = 25 247 }; 248 } // namespace I18N 249 } // namespace OHOS 250 /** @} */ 251 #endif 252