1 /*
2  * Copyright (c) 2021-2022 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/preview/entrance/ace_application_info.h"
17 
18 #include "unicode/locid.h"
19 
20 #ifdef WINDOWS_PLATFORM
21 #include <windows.h>
22 #else
23 #include <dirent.h>
24 #include <sys/types.h>
25 #endif
26 
27 #include "base/i18n/localization.h"
28 #include "base/log/ace_trace.h"
29 #include "base/log/log.h"
30 #include "base/resource/ace_res_config.h"
31 #include "base/resource/ace_res_data_struct.h"
32 #include "core/common/ace_engine.h"
33 
34 namespace OHOS::Ace::Platform {
35 
ChangeLocale(const std::string & language,const std::string & countryOrRegion)36 void AceApplicationInfoImpl::ChangeLocale(const std::string& language, const std::string& countryOrRegion)
37 {
38     std::string languageLower = language;
39     std::transform(language.begin(), language.end(), languageLower.begin(), ::tolower);
40 
41     std::string countryOrRegionUpper = countryOrRegion;
42     std::transform(countryOrRegion.begin(), countryOrRegion.end(), countryOrRegionUpper.begin(), ::tolower);
43 
44     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
45     resourceManager_->GetResConfig(*resConfig);
46 
47     auto localeInfo = resConfig->GetLocaleInfo();
48     if (localeInfo == nullptr) {
49         LOGW("get local info failed");
50         return;
51     }
52 
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     language_ = language;
64     countryOrRegion_ = countryOrRegion;
65     script_ = (!script.empty()) ? script : Localization::ComputeScript(language, countryOrRegion);
66     keywordsAndValues_ = keywordsAndValues;
67 
68     localeTag_ = language;
69     if (!script_.empty()) {
70         localeTag_.append("-" + script_);
71     }
72     if (!countryOrRegion_.empty()) {
73         localeTag_.append("-" + countryOrRegion_);
74     }
75 
76     icu::Locale locale(language_.c_str(), countryOrRegion.c_str());
77     isRightToLeft_ = locale.isRightToLeft();
78 
79     auto languageList = Localization::GetLanguageList(language_);
80     Localization::SetLocale(
81         language_, countryOrRegion_, script_, languageList.front(), keywordsAndValues_);
82 }
83 
GetBundleInfo(const std::string & packageName,AceBundleInfo & bundleInfo)84 bool AceApplicationInfoImpl::GetBundleInfo(const std::string& packageName, AceBundleInfo& bundleInfo)
85 {
86     return false;
87 }
88 
GetLifeTime() const89 double AceApplicationInfoImpl::GetLifeTime() const
90 {
91     return 0;
92 }
93 
GetJsEngineParam(const std::string & key) const94 std::string AceApplicationInfoImpl::GetJsEngineParam(const std::string& key) const
95 {
96     return "";
97 }
98 
GetInstance()99 AceApplicationInfoImpl& AceApplicationInfoImpl::GetInstance()
100 {
101     static AceApplicationInfoImpl instance;
102     return instance;
103 }
104 
SetDebug(bool isDebugVersion,bool needDebugBreakpoint)105 void AceApplicationInfoImpl::SetDebug(bool isDebugVersion, bool needDebugBreakpoint)
106 {
107 }
108 
109 } // namespace OHOS::Ace::Platform
110 
111 namespace OHOS::Ace {
112 
GetInstance()113 AceApplicationInfo& AceApplicationInfo::GetInstance()
114 {
115     return Platform::AceApplicationInfoImpl::GetInstance();
116 }
117 
118 } // namespace OHOS::Ace
119