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
16 #include "dm_language_manager.h"
17
18 #include "cJSON.h"
19 #include <map>
20
21 #include "dm_constants.h"
22 #include "dm_log.h"
23 #include "parameter.h"
24
25 namespace OHOS {
26 namespace DistributedHardware {
27 DM_IMPLEMENT_SINGLE_INSTANCE(DmLanguageManager);
28 const int32_t MAX_LEN = 128;
29 const std::string SYSTEM_LANGUAGE_KEY = "persist.global.language";
30 const std::string SYSTEM_LANGUAGE_LOCALE_KEY = "persist.global.locale";
31 const std::string DEFAULT_LANGUAGE = "zh-Hans";
32 const std::string LANGUAGE_EN = "en-Latn_US";
33 const std::string LANGUAGE_AND_LOCALE_STR =
34 R"({
35 "zh-Hant": [
36 {
37 "persist.global.locale": "zh-Hant-HK"
38 },
39 {
40 "persist.global.locale": "zh-Hant-TW"
41 }
42 ]
43 })";
44
GetSystemParam(const std::string & key)45 std::string DmLanguageManager::GetSystemParam(const std::string &key)
46 {
47 char value[MAX_LEN] = {0};
48 int32_t ret = GetParameter(key.c_str(), "", value, MAX_LEN);
49 if (ret <= 0) {
50 LOGE("GetSystemParam %{public}s failed", key.c_str());
51 return "";
52 }
53 return std::string(value);
54 }
55
GetLocaleByLanguage(const std::string & language,std::set<std::string> & localeSet)56 void DmLanguageManager::GetLocaleByLanguage(const std::string &language, std::set<std::string> &localeSet)
57 {
58 cJSON *languageAndLocaleObj = cJSON_Parse(LANGUAGE_AND_LOCALE_STR.c_str());
59 if (languageAndLocaleObj == NULL) {
60 LOGE("parse languageAndLocaleObj failed");
61 return;
62 }
63 cJSON *languageObj = cJSON_GetObjectItem(languageAndLocaleObj, language.c_str());
64 if (languageObj == NULL || !cJSON_IsArray(languageObj)) {
65 cJSON_Delete(languageAndLocaleObj);
66 return;
67 }
68 cJSON *item = NULL;
69 cJSON_ArrayForEach(item, languageObj) {
70 if (!cJSON_IsObject(item)) {
71 LOGE("item is not object!");
72 continue;
73 }
74 cJSON* localeObj = cJSON_GetObjectItemCaseSensitive(item, SYSTEM_LANGUAGE_LOCALE_KEY.c_str());
75 if (!cJSON_IsString(localeObj) || localeObj->valuestring == NULL) {
76 LOGE("Get localeObj fail!");
77 continue;
78 }
79 localeSet.insert(localeObj->valuestring);
80 }
81 cJSON_Delete(languageAndLocaleObj);
82 }
83
GetTextBySystemLanguage(const std::string & text)84 std::string DmLanguageManager::GetTextBySystemLanguage(const std::string &text)
85 {
86 if (text.empty()) {
87 return "";
88 }
89 cJSON *textObj = cJSON_Parse(text.c_str());
90 if (textObj == NULL) {
91 LOGE("parse text failed");
92 return text;
93 }
94 std::string resultText = text;
95 std::string language = GetSystemParam(SYSTEM_LANGUAGE_KEY);
96 language = language.empty() ? DEFAULT_LANGUAGE : language;
97 std::set<std::string> localeSet;
98 GetLocaleByLanguage(language, localeSet);
99 if (localeSet.size() > 0) {
100 resultText = GetTextBySystemLocale(textObj, localeSet);
101 if (!resultText.empty()) {
102 cJSON_Delete(textObj);
103 return resultText;
104 }
105 }
106 cJSON *languageJson = cJSON_GetObjectItem(textObj, language.c_str());
107 if (languageJson != NULL && cJSON_IsString(languageJson)) {
108 resultText = std::string(languageJson->valuestring);
109 cJSON_Delete(textObj);
110 return resultText;
111 }
112 cJSON *defaultJson = cJSON_GetObjectItem(textObj, DEFAULT_LANGUAGE.c_str());
113 if (defaultJson != NULL && cJSON_IsString(defaultJson)) {
114 resultText = std::string(defaultJson->valuestring);
115 cJSON_Delete(textObj);
116 return resultText;
117 }
118 cJSON *enJson = cJSON_GetObjectItem(textObj, LANGUAGE_EN.c_str());
119 if (enJson != NULL && cJSON_IsString(enJson)) {
120 resultText = std::string(enJson->valuestring);
121 cJSON_Delete(textObj);
122 return resultText;
123 }
124 cJSON_Delete(textObj);
125 return "";
126 }
127
GetTextBySystemLocale(const cJSON * const textObj,const std::set<std::string> & localeSet)128 std::string DmLanguageManager::GetTextBySystemLocale(const cJSON *const textObj,
129 const std::set<std::string> &localeSet)
130 {
131 std::string resultText = "";
132 std::string languageLocale = GetSystemParam(SYSTEM_LANGUAGE_LOCALE_KEY);
133 cJSON *languageLocaleson = cJSON_GetObjectItem(textObj, languageLocale.c_str());
134 if (languageLocaleson != NULL && cJSON_IsString(languageLocaleson)) {
135 resultText = std::string(languageLocaleson->valuestring);
136 return resultText;
137 }
138 for (std::string locale : localeSet) {
139 cJSON *localesonObj = cJSON_GetObjectItem(textObj, locale.c_str());
140 if (localesonObj != NULL && cJSON_IsString(localesonObj)) {
141 resultText = std::string(localesonObj->valuestring);
142 return resultText;
143 }
144 }
145 return "";
146 }
147 } // namespace DistributedHardware
148 } // namespace OHOS