• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "types.h"
48 
49 namespace OHOS {
50 namespace I18N {
51 class LocaleInfo {
52 public:
53     /**
54     * @brief A constructor used to create a <b>LocaleInfo</b> instance with specified language,
55     *   script, and country/region.
56     *
57     * @param lang Indicates the pointer to the specified language.
58     * @param script Indicates the pointer to the specified script.
59     * @param region Indicates the pointer to the specified country/region.
60     * @since 2.2
61     * @version 1.0
62     */
63     LocaleInfo(const char *lang, const char *script, const char *region);
64 
65     /**
66     * @brief A constructor used to create a <b>LocaleInfo</b> instance with specified language and country/region.
67     *
68     * @param lang Indicates the pointer to the specified language.
69     * @param region Indicates the pointer to the specified country/region.
70     * @since 2.2
71     * @version 1.0
72     */
73     LocaleInfo(const char *lang, const char *region);
74 
75     /**
76     * @brief A constructor used to create a <b>LocaleInfo</b> instance by copying a specified one.
77     *
78     * @param locale Indicates the specified <b>LocaleInfo</b> instance.
79     * @since 2.2
80     * @version 1.0
81     */
82     LocaleInfo(const LocaleInfo& locale);
83 
84     /**
85     * @brief Default constructor used to create a <b>LocaleInfo</b> instance.
86     *
87     * @since 2.2
88     * @version 1.0
89     */
90     LocaleInfo();
91 
92     /**
93     * @brief A destructor used to delete the <b>LocaleInfo</b> instance.
94     *
95     * @since 2.2
96     * @version 1.0
97     */
98     virtual ~LocaleInfo();
99 
100     /**
101     * @brief Checks whether this <b>LocaleInfo</b> object equals a specified one.
102     *
103     * @param other Indicates the <b>LocaleInfo</b> object to compare.
104     * @return Returns <b>true</b> if the two objects are equal; returns <b>false</b> otherwise.
105     * @since 2.2
106     * @version 1.0
107     */
108     virtual bool operator ==(const LocaleInfo &other) const;
109 
110     /**
111     * @brief Creates a new <b>LocaleInfo</b> object based on a specified one.
112     *
113     * @param other Indicates the specified <b>LocaleInfo</b> object.
114     * @return Returns the new <b>LocaleInfo</b> object.
115     * @since 2.2
116     * @version 1.0
117     */
118     virtual LocaleInfo &operator =(const LocaleInfo &other);
119 
120     /**
121     * @brief Obtains the ID of this <b>LocaleInfo</b> object, which consists of the language,
122     *   script, and country/region.
123     *
124     * @return Returns the ID.
125     * @since 2.2
126     * @version 1.0
127     */
128     const char *GetId() const;
129 
130     /**
131     * @brief Obtains the language specified in this <b>LocaleInfo</b> object.
132     *
133     * @return Returns the language.
134     * @since 2.2
135     * @version 1.0
136     */
137     const char *GetLanguage() const;
138 
139     /**
140     * @brief Obtains the script specified in this <b>LocaleInfo</b> object.
141     *
142     * @return Returns the script.
143     * @since 2.2
144     * @version 1.0
145     */
146     const char *GetScript() const;
147 
148     /**
149     * @brief Obtains the country/region specified in this <b>LocaleInfo</b> object.
150     *
151     * @return Returns the country/region.
152     * @since 2.2
153     * @version 1.0
154     */
155     const char *GetRegion() const;
156 
157     /**
158     * @brief Obtains the mask of this <b>LocaleInfo</b> object.
159     *
160     * @return Returns the mask.
161     * @since 2.2
162     * @version 1.0
163     */
164     uint32_t GetMask() const;
165 
166     /**
167     * @brief Checks whether this <b>LocaleInfo</b> object represents the default locale (en-US).
168     *
169     * @return Returns <b>true</b> if the <b>LocaleInfo</b> object represents the default locale;
170     *   returns <b>false</b> otherwise.
171     * @since 2.2
172     * @version 1.0
173     */
174     bool IsDefaultLocale() const;
175 
176     /**
177     * @brief Parse a language tag, and returns an associated <b>LocaleInfo</b> instance.
178     *
179     * @param languageTag Indicates the language tag, which is to be parsed.
180     * @param status Indicates the status of the creating process.
181     * @return Returns the associated LocaleInfo instances.
182     */
183     static LocaleInfo ForLanguageTag(const char *languageTag, I18nStatus &status);
184 
185     /**
186     * @brief Get extension subtag associated with the key.
187     *
188     * @param key Get the extension subtag using the key.
189     * @return Returns the subtag
190     */
191     const char *GetExtension(const char *key);
192 private:
193     bool ChangeLanguageCode(char *lang, const int32_t dstSize, const char *src, const int32_t srcSize) const;
194     void FreeResource();
195     static void ProcessExtension(LocaleInfo &locale, const char *key, const char *value);
196     static void ConfirmTagType(const char *start, size_t length, uint8_t &type, const char* &key, const char* &value);
197     static void ParseLanguageTag(LocaleInfo &locale, const char *languageTag, I18nStatus &status);
198     static bool ParseNormalSubTag(LocaleInfo &locale, const char *start, size_t tagLength, uint16_t &options,
199         uint8_t &type);
200     static bool IsLanguage(const char *start, uint8_t length);
201     static bool IsScript(const char *start, uint8_t length);
202     static bool IsRegion(const char *start, uint8_t length);
203     void InitIdstr();
204     char *language = nullptr;
205     char *script = nullptr;
206     char *region = nullptr;
207     char *id = nullptr;
208     char *numberDigits = nullptr;
209     bool isSucc = true;
210     bool IsSuccess();
211     void SetFail();
212     void Init(const char *lang, const char *script, const char *region, int &status);
213     const int CHAR_OFF = 48;
214 };
215 
216 enum ESupportScript {
217     NOKOWN = 0x0,
218     LATN = 0x1,
219     HANS = 0x2,
220     HANT = 0x3,
221     QAAG = 0x4,
222     CYRL = 0x5,
223     DEVA = 0x6,
224     GURU = 0x7
225 };
226 
227 enum EMask {
228     REGION_FIRST_LETTER = 7,
229     SCRIPT_BEGIN = 14,
230     LANG_SECOND_BEGIN = 18,
231     LANG_FIRST_BEGIN = 25
232 };
233 } // namespace I18N
234 } // namespace OHOS
235 /** @} */
236 #endif
237