• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include <cstdint>
17 #include <cstdlib>
18 #include <string>
19 #include <vector>
20 #include "locale_config.h"
21 #include "locale_info.h"
22 #include "i18n_ffi.h"
23 #include "i18n_hilog.h"
24 #include "i18n_struct.h"
25 #include "i18n_system_ffi.h"
26 #include "preferred_language.h"
27 #include "utils.h"
28 
29 namespace OHOS {
30 namespace Global {
31 namespace I18n {
32 using namespace OHOS::HiviewDFX;
33 extern "C"
34 {
FfiI18nSystemGetAppPreferredLanguage()35     char* FfiI18nSystemGetAppPreferredLanguage()
36     {
37         #ifdef SUPPORT_APP_PREFERRED_LANGUAGE
38             std::string language = PreferredLanguage::GetAppPreferredLanguage();
39         #else
40             std::string language = PreferredLanguage::GetFirstPreferredLanguage();
41         #endif
42         return MallocCString(language);
43     }
44 
FfiI18nSystemGetSystemLanguage()45     char* FfiI18nSystemGetSystemLanguage()
46     {
47         return MallocCString(LocaleConfig::GetSystemLanguage());
48     }
49 
FfiI18nSystemGetSystemRegion()50     char* FfiI18nSystemGetSystemRegion()
51     {
52         return MallocCString(LocaleConfig::GetSystemRegion());
53     }
54 
FfiI18nSystemIsSuggested(char * language,char * region,int32_t parameterStatus)55     bool FfiI18nSystemIsSuggested(char* language, char* region, int32_t parameterStatus)
56     {
57         bool isSuggested = false;
58         if (parameterStatus == 1) {
59             isSuggested = LocaleConfig::IsSuggested(std::string(language), std::string(region));
60         } else {
61             isSuggested = LocaleConfig::IsSuggested(std::string(language));
62         }
63         return isSuggested;
64     }
65 
FfiI18nSystemGetSystemCountries(char * language)66     CArrStr FfiI18nSystemGetSystemCountries(char* language)
67     {
68         std::unordered_set<std::string> systemCountries = LocaleConfig::GetSystemCountries(language);
69         std::vector<std::string> result;
70         result.assign(systemCountries.begin(), systemCountries.end());
71         return VectorStringToCArr(result);
72     }
73 
FfiI18nSystemGetDisplayCountry(char * country,char * locale,bool sentenceCase,int32_t * errcode)74     char* FfiI18nSystemGetDisplayCountry(char* country, char* locale, bool sentenceCase, int32_t* errcode)
75     {
76         std::string countryStr(country);
77         std::string localeStr(locale);
78         LocaleInfo localeInfo(countryStr);
79         if (!LocaleConfig::IsValidRegion(countryStr) && localeInfo.GetRegion().empty()) {
80             HILOG_ERROR_I18N("GetDisplayCountry: Failed to get display country, because param country is invalid!");
81             *errcode = I18N_NOT_VALID;
82             return nullptr;
83         } else if (!LocaleConfig::IsValidTag(locale)) {
84             HILOG_ERROR_I18N("GetDisplayCountry: Failed to get display country, because param locale is invalid!");
85             *errcode = I18N_NOT_VALID;
86             return nullptr;
87         }
88         std::string region = LocaleConfig::GetDisplayRegion(countryStr, localeStr, sentenceCase);
89         return MallocCString(region);
90     }
91 
FfiI18nSystemGetUsingLocalDigit()92     bool FfiI18nSystemGetUsingLocalDigit()
93     {
94         return LocaleConfig::GetUsingLocalDigit();
95     }
96 
FfiI18nSystemSetAppPreferredLanguage(char * language)97     int32_t FfiI18nSystemSetAppPreferredLanguage(char* language)
98     {
99         UErrorCode icuStatus = U_ZERO_ERROR;
100         std::string localeTag(language);
101         icu::Locale locale = icu::Locale::forLanguageTag(localeTag, icuStatus);
102         if (U_FAILURE(icuStatus) || !(IsValidLocaleTag(locale) || localeTag.compare("default") == 0)) {
103             HILOG_ERROR_I18N("SetAppPreferredLanguage does not support this locale");
104             return I18N_NOT_VALID;
105         }
106         #ifdef SUPPORT_APP_PREFERRED_LANGUAGE
107             I18nErrorCode errCode = I18nErrorCode::SUCCESS;
108             PreferredLanguage::SetAppPreferredLanguage(localeTag, errCode);
109             if (errCode != I18nErrorCode::SUCCESS) {
110                 HILOG_ERROR_I18N("Set app language to i18n app preferences failed, error code: %d", errCode);
111                 return -1;
112             }
113         #endif
114             return 0;
115     }
116 
FfiI18nSystemGetFirstPreferredLanguage()117     char* FfiI18nSystemGetFirstPreferredLanguage()
118     {
119         return MallocCString(PreferredLanguage::GetFirstPreferredLanguage());
120     }
121 
FfiI18nSystemGetPreferredLanguageList()122     CArrStr FfiI18nSystemGetPreferredLanguageList()
123     {
124         return VectorStringToCArr(PreferredLanguage::GetPreferredLanguageList());
125     }
126 
FfiI18nSystemIs24HourClock()127     bool FfiI18nSystemIs24HourClock()
128     {
129         return LocaleConfig::Is24HourClock();
130     }
131 
FfiI18nSystemGetSystemLanguages()132     CArrStr FfiI18nSystemGetSystemLanguages()
133     {
134         std::unordered_set<std::string> systemLanguages = LocaleConfig::GetSystemLanguages();
135         std::vector<std::string> result;
136         result.assign(systemLanguages.begin(), systemLanguages.end());
137         return VectorStringToCArr(result);
138     }
139 
FfiI18nSystemGetDisplayLanguage(char * language,char * locale,bool sentenceCase,int32_t * errCode)140     char* FfiI18nSystemGetDisplayLanguage(char* language, char* locale, bool sentenceCase, int32_t* errCode)
141     {
142         std::string languageStr(language);
143         std::string localeStr(locale);
144         if (!LocaleConfig::IsValidTag(localeStr)) {
145             HILOG_ERROR_I18N("GetDisplayLanguage: Failed to get display language, because param locale is invalid!");
146             *errCode = I18N_NOT_VALID;
147             return nullptr;
148         }
149         std::string value = LocaleConfig::GetDisplayLanguage(languageStr, localeStr, sentenceCase);
150         if (value.length() == 0) {
151             HILOG_ERROR_I18N("GetDisplayLanguage: result is empty.");
152         }
153         return MallocCString(value);
154     }
155 
FfiI18nSystemGetSystemLocale()156     char* FfiI18nSystemGetSystemLocale()
157     {
158         return MallocCString(LocaleConfig::GetSystemLocale());
159     }
160 }
161 }  // namespace I18n
162 }  // namespace Global
163 }  // namespace OHOS