• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 GLOBAL_LOCALE_HELPER_H
16 #define GLOBAL_LOCALE_HELPER_H
17 
18 #include <string>
19 #include <map>
20 #include <mutex>
21 #include <set>
22 #include <unordered_set>
23 #include <vector>
24 #include "unicode/locid.h"
25 #include "i18n_types.h"
26 
27 namespace OHOS {
28 namespace Global {
29 namespace I18n {
30 class LocaleHelper {
31 public:
32     static bool IsValidOptionName(const std::string& key, const std::string& option);
33     static std::string ParseOptionWithoutCheck(const std::map<std::string, std::string> &options,
34                                                const std::string &key,
35                                                const std::string &defaultOption);
36     static std::string ParseOption(const std::map<std::string, std::string> &options,
37                                    const std::string &key,
38                                    const std::string &defaultOption,
39                                    bool isOptional,
40                                    I18nErrorCode &status);
41     static std::string BestAvailableLocale(const std::set<std::string> &availableLocales,
42                                            const std::string &locale);
43     static std::vector<std::string> LookupSupportedLocales(const std::set<std::string> &availableLocales,
44                                                            const std::vector<std::string> &requestLocales);
45     static std::vector<std::string> CanonicalizeLocaleList(const std::vector<std::string> &locales,
46                                                            I18nErrorCode &status);
47     static bool IsUnicodeScriptSubtag(const std::string &value);
48     static bool IsUnicodeRegionSubtag(const std::string &value);
49     static bool IsWellFormedCurrencyCode(const std::string &currency);
50     static bool IsWellFormedCalendarCode(const std::string &calendar);
51     static bool IsStructurallyValidLanguageTag(const std::string& tag);
52     static bool DealwithLanguageTag(const std::vector<std::string> &containers, size_t &address);
53     static bool IsAlpha(const std::string &str, size_t min, size_t max);
54     static bool IsNormativeCalendar(const std::string &value);
55     static bool IsNormativeNumberingSystem(const std::string &value);
56     static double TruncateDouble(double number);
57     static std::set<std::string> GetAvailableLocales();
58     static std::vector<std::string> SupportedLocalesOf(const std::vector<std::string> &requestLocales,
59         const std::map<std::string, std::string> &configs, I18nErrorCode &status);
60     static std::string CheckParamLocales(const std::vector<std::string> &localeArray);
61     static std::string DefaultLocale();
62     static std::string LookupMatcher(const std::set<std::string>& availableLocales,
63                                      const std::vector<std::string>& requestedLocales);
64 
65 private:
66     static bool IsWellAlphaNumList(const std::string &value);
67 
68     static const std::unordered_map<std::string, std::unordered_set<std::string>> KEY_TO_OPTION_NAME;
69     static std::string defaultLocale;
70     static std::mutex defaultLocaleMutex;
71 };
72 } // namespace I18n
73 } // namespace Global
74 } // namespace OHOS
75 #endif