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