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 #include "adapter/ohos/entrance/ace_application_info.h"
17
18 #include "base/i18n/localization.h"
19 #include "base/resource/ace_res_config.h"
20
21 namespace OHOS::Ace::Platform {
AceApplicationInfoImpl()22 AceApplicationInfoImpl::AceApplicationInfoImpl() {}
23 AceApplicationInfoImpl::~AceApplicationInfoImpl() = default;
24
SetJsEngineParam(const std::string & key,const std::string & value)25 void AceApplicationInfoImpl::SetJsEngineParam(const std::string& key, const std::string& value)
26 {
27 jsEngineParams_[key] = value;
28 }
29
GetJsEngineParam(const std::string & key) const30 std::string AceApplicationInfoImpl::GetJsEngineParam(const std::string& key) const
31 {
32 std::string value;
33 auto iter = jsEngineParams_.find(key);
34 if (iter != jsEngineParams_.end()) {
35 value = iter->second;
36 }
37 return value;
38 }
39
ChangeLocale(const std::string & language,const std::string & countryOrRegion)40 void AceApplicationInfoImpl::ChangeLocale(const std::string& language, const std::string& countryOrRegion)
41 {
42 std::string languageLower = language;
43 std::transform(language.begin(), language.end(), languageLower.begin(), ::tolower);
44
45 std::string countryOrRegionUpper = countryOrRegion;
46 std::transform(countryOrRegion.begin(), countryOrRegion.end(), countryOrRegionUpper.begin(), ::tolower);
47
48 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
49 resourceManager_->GetResConfig(*resConfig);
50
51 auto localeInfo = resConfig->GetLocaleInfo();
52 CHECK_NULL_VOID(localeInfo);
53 auto script = Localization::ComputeScript(language, countryOrRegion);
54 resConfig->SetLocaleInfo(languageLower.c_str(), script.c_str(), countryOrRegionUpper.c_str());
55 resourceManager_->UpdateResConfig(*resConfig);
56
57 SetLocale(languageLower, countryOrRegionUpper, (script.empty()) ? "" : script, "");
58 }
59
SetLocale(const std::string & language,const std::string & countryOrRegion,const std::string & script,const std::string & keywordsAndValues)60 void AceApplicationInfoImpl::SetLocale(const std::string& language, const std::string& countryOrRegion,
61 const std::string& script, const std::string& keywordsAndValues)
62 {
63 std::unique_lock<std::shared_mutex> lock(localeTagMutex_);
64 language_ = language;
65 countryOrRegion_ = countryOrRegion;
66 script_ = script;
67 keywordsAndValues_ = keywordsAndValues;
68
69 localeTag_ = language;
70 if (!script_.empty()) {
71 localeTag_.append("-" + script_);
72 }
73 if (!countryOrRegion_.empty()) {
74 localeTag_.append("-" + countryOrRegion_);
75 }
76
77 icu::Locale locale(language_.c_str(), countryOrRegion.c_str());
78 isRightToLeft_ = SystemProperties::GetRtlEnabled().value_or(locale.isRightToLeft());
79
80 auto languageList = Localization::GetLanguageList(language_);
81 if (languageList.size() == 1) {
82 Localization::SetLocale(language_, countryOrRegion_, script, languageList.front(), keywordsAndValues_);
83 } else {
84 auto selectLanguage = AceResConfig::GetLocaleFallback(localeTag_, languageList);
85 Localization::SetLocale(language_, countryOrRegion_, script, selectLanguage.front(), keywordsAndValues_);
86 }
87 }
88
GetBundleInfo(const std::string & packageName,AceBundleInfo & bundleInfo)89 bool AceApplicationInfoImpl::GetBundleInfo(const std::string& packageName, AceBundleInfo& bundleInfo)
90 {
91 return false;
92 }
93
GetInstance()94 AceApplicationInfoImpl& AceApplicationInfoImpl::GetInstance()
95 {
96 static AceApplicationInfoImpl instance;
97 return instance;
98 }
99
100 } // namespace OHOS::Ace::Platform
101
102 namespace OHOS::Ace {
103
GetInstance()104 AceApplicationInfo& AceApplicationInfo::GetInstance()
105 {
106 return Platform::AceApplicationInfoImpl::GetInstance();
107 }
108
109 } // namespace OHOS::Ace
110