From cdc9cc50930c602bd3077ea04d3aed8cd716e6a1 Mon Sep 17 00:00:00 2001 From: fangzhiyuan Date: Mon, 23 Sep 2024 22:27:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=B5=AE=E7=82=B9=E6=95=B0?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fangzhiyuan Change-Id: Iaf6f1bb168bb058b86a8e336f645410039da7ada --- adapter/ohos/osal/resource_adapter_impl.cpp | 59 ++++++ adapter/ohos/osal/resource_adapter_impl.h | 8 + .../ohos/osal/resource_adapter_impl_v2.cpp | 60 ++++++ adapter/ohos/osal/resource_adapter_impl_v2.h | 8 + adapter/ohos/osal/resource_convertor.cpp | 18 ++ adapter/ohos/osal/resource_convertor.h | 5 +- .../osal/resource_adapter_impl_standard.cpp | 56 ++++++ .../osal/resource_adapter_impl_standard.h | 9 + adapter/preview/osal/resource_convertor.cpp | 19 ++ adapter/preview/osal/resource_convertor.h | 5 +- .../jsview/js_view_abstract.cpp | 177 ++++++++---------- .../jsview/js_view_abstract.h | 4 + .../core/common/resource/resource_wrapper.h | 36 ++++ .../core/components/theme/resource_adapter.h | 19 ++ .../core/components/theme/theme_constants.cpp | 36 ++++ .../core/components/theme/theme_constants.h | 38 ++++ .../core/common/mock_resource_adapter_v2.h | 24 +++ 17 files changed, 481 insertions(+), 100 deletions(-) diff --git a/adapter/ohos/osal/resource_adapter_impl.cpp b/adapter/ohos/osal/resource_adapter_impl.cpp index 88c6f890f6a..de22cb6c802 100755 --- a/adapter/ohos/osal/resource_adapter_impl.cpp +++ b/adapter/ohos/osal/resource_adapter_impl.cpp @@ -709,4 +709,63 @@ uint32_t ResourceAdapterImpl::GetSymbolById(uint32_t resId) const return result; } +std::string ResourceAdapterImpl::GetStringFormatById( + uint32_t resId, const std::vector>& jsParams) +{ + std::string strResult = ""; + auto manager = GetResourceManager(); + CHECK_NULL_RETURN(manager, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = manager->GetStringFormatById(resId, strResult, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get string format by id error, id=%{public}u, errorCode=%{public}d", resId, + state); + } + return strResult; +} + +std::string ResourceAdapterImpl::GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) +{ + std::string strResult = ""; + auto manager = GetResourceManager(); + CHECK_NULL_RETURN(manager, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = manager->GetStringFormatByName(resName.c_str(), strResult, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get string format by name error, name=%{public}s, errorCode=%{public}d", + resName.c_str(), state); + } + return strResult; +} + +std::string ResourceAdapterImpl::GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) +{ + std::string strResult = ""; + auto manager = GetResourceManager(); + CHECK_NULL_RETURN(manager, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = manager->GetFormatPluralStringById(strResult, resId, quantity, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get plural string format by id error, id=%{public}u, errorCode=%{public}d", + resId, state); + } + return strResult; +} + +std::string ResourceAdapterImpl::GetFormatPluralStringByName( + const std::string& resName, int32_t quantity, const std::vector>& jsParams) +{ + std::string strResult = ""; + auto manager = GetResourceManager(); + CHECK_NULL_RETURN(manager, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = manager->GetFormatPluralStringByName(strResult, resName.c_str(), quantity, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, + "Get plural string format by name error, name=%{public}s, errorCode=%{public}d", resName.c_str(), state); + } + return strResult; +} } // namespace OHOS::Ace diff --git a/adapter/ohos/osal/resource_adapter_impl.h b/adapter/ohos/osal/resource_adapter_impl.h index c249efcf5e0..2ded8c76b97 100755 --- a/adapter/ohos/osal/resource_adapter_impl.h +++ b/adapter/ohos/osal/resource_adapter_impl.h @@ -74,6 +74,14 @@ public: bool GetMediaById(const int32_t& resId, std::string& mediaPath) const override; uint32_t GetResourceLimitKeys() const override; uint32_t GetSymbolById(uint32_t resId) const override; + std::string GetStringFormatById( + uint32_t resId, const std::vector>& jsParams) override; + std::string GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) override; + std::string GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) override; + std::string GetFormatPluralStringByName(const std::string& resName, int32_t quantity, + const std::vector>& jsParams) override; private: std::string GetActualResourceName(const std::string& resName) const; diff --git a/adapter/ohos/osal/resource_adapter_impl_v2.cpp b/adapter/ohos/osal/resource_adapter_impl_v2.cpp index 15637020acf..56069ef8ac5 100755 --- a/adapter/ohos/osal/resource_adapter_impl_v2.cpp +++ b/adapter/ohos/osal/resource_adapter_impl_v2.cpp @@ -878,4 +878,64 @@ RefPtr ResourceAdapterImplV2::GetOverrideResourceAdapter( auto overrideResMgr = sysResourceManager_->GetOverrideResourceManager(overrideResConfig); return AceType::MakeRefPtr(overrideResMgr); } + +std::string ResourceAdapterImplV2::GetStringFormatById( + uint32_t resId, const std::vector>& jsParams) +{ + std::string strResult = ""; + auto manager = GetResourceManager(); + CHECK_NULL_RETURN(manager, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = manager->GetStringFormatById(resId, strResult, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get string format by id error, id=%{public}u, errorCode=%{public}d", resId, + state); + } + return strResult; +} + +std::string ResourceAdapterImplV2::GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) +{ + std::string strResult = ""; + auto manager = GetResourceManager(); + CHECK_NULL_RETURN(manager, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = manager->GetStringFormatByName(resName.c_str(), strResult, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get string format by name error, name=%{public}s, errorCode=%{public}d", + resName.c_str(), state); + } + return strResult; +} + +std::string ResourceAdapterImplV2::GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) +{ + std::string strResult = ""; + auto manager = GetResourceManager(); + CHECK_NULL_RETURN(manager, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = manager->GetFormatPluralStringById(strResult, resId, quantity, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get plural string format by id error, id=%{public}u, errorCode=%{public}d", + resId, state); + } + return strResult; +} + +std::string ResourceAdapterImplV2::GetFormatPluralStringByName( + const std::string& resName, int32_t quantity, const std::vector>& jsParams) +{ + std::string strResult = ""; + auto manager = GetResourceManager(); + CHECK_NULL_RETURN(manager, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = manager->GetFormatPluralStringByName(strResult, resName.c_str(), quantity, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, + "Get plural string format by name error, name=%{public}s, errorCode=%{public}d", resName.c_str(), state); + } + return strResult; +} } // namespace OHOS::Ace diff --git a/adapter/ohos/osal/resource_adapter_impl_v2.h b/adapter/ohos/osal/resource_adapter_impl_v2.h index 4be886e21ed..26e595b84f6 100755 --- a/adapter/ohos/osal/resource_adapter_impl_v2.h +++ b/adapter/ohos/osal/resource_adapter_impl_v2.h @@ -85,6 +85,14 @@ public: void SetAppHasDarkRes(bool hasDarkRes); RefPtr GetOverrideResourceAdapter( const ResourceConfiguration& config, const ConfigurationChange& configurationChange) override; + std::string GetStringFormatById( + uint32_t resId, const std::vector>& jsParams) override; + std::string GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) override; + std::string GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) override; + std::string GetFormatPluralStringByName(const std::string& resName, int32_t quantity, + const std::vector>& jsParams) override; private: std::string GetActualResourceName(const std::string& resName) const; diff --git a/adapter/ohos/osal/resource_convertor.cpp b/adapter/ohos/osal/resource_convertor.cpp index e7e69872788..2a325128c54 100644 --- a/adapter/ohos/osal/resource_convertor.cpp +++ b/adapter/ohos/osal/resource_convertor.cpp @@ -159,4 +159,22 @@ double ConvertDensityToAce(Global::Resource::ScreenDensity density) } } +std::vector> ConvertToGlobalParams( + const std::vector>& jsParams) +{ + std::vector> globalParams; + globalParams.reserve(jsParams.size()); + + for (const auto& jsParam : jsParams) { + if (jsParam.first == JsValueType::JS_NUMBER) { + auto globalParam = std::make_tuple(NapiValueType::NAPI_NUMBER, jsParam.second); + globalParams.push_back(globalParam); + } else if (jsParam.first == JsValueType::JS_STRING) { + auto globalParam = std::make_tuple(NapiValueType::NAPI_STRING, jsParam.second); + globalParams.push_back(globalParam); + } + } + + return globalParams; +} } // namespace OHOS::Ace \ No newline at end of file diff --git a/adapter/ohos/osal/resource_convertor.h b/adapter/ohos/osal/resource_convertor.h index b1b75ec6c29..896a7d9a44e 100644 --- a/adapter/ohos/osal/resource_convertor.h +++ b/adapter/ohos/osal/resource_convertor.h @@ -21,10 +21,12 @@ #include "base/utils/utils.h" #include "base/utils/resource_configuration.h" #include "core/common/ace_application_info.h" +#include "core/components/theme/resource_adapter.h" namespace OHOS::Ace { constexpr double DPI_BASE = 160.0; +using NapiValueType = OHOS::Global::Resource::ResourceManager::NapiValueType; Global::Resource::DeviceType ConvertDeviceTypeToGlobal(DeviceType type); Global::Resource::Direction ConvertDirectionToGlobal(DeviceOrientation orientation); @@ -35,7 +37,8 @@ std::shared_ptr ConvertConfigToGlobal(const Resourc DeviceType ConvertDeviceTypeToAce(Global::Resource::DeviceType type); DeviceOrientation ConvertDirectionToAce(Global::Resource::Direction orientation); double ConvertDensityToAce(Global::Resource::ScreenDensity density); - +std::vector> ConvertToGlobalParams( + const std::vector>& jsParams); } // namespace OHOS::Ace #endif // FOUNDATION_ACE_ADAPTER_OHOS_OSAL_RESOURCE_CONVERTOR_H \ No newline at end of file diff --git a/adapter/preview/osal/resource_adapter_impl_standard.cpp b/adapter/preview/osal/resource_adapter_impl_standard.cpp index 36f6ecd9d06..e3773bc35e4 100644 --- a/adapter/preview/osal/resource_adapter_impl_standard.cpp +++ b/adapter/preview/osal/resource_adapter_impl_standard.cpp @@ -567,4 +567,60 @@ RefPtr ResourceAdapterImpl::GetOverrideResourceAdapter( auto overrideResMgr = resourceManager_->GetOverrideResourceManager(overrideResConfig); return AceType::MakeRefPtr(overrideResMgr); } + +std::string ResourceAdapterImpl::GetStringFormatById( + uint32_t resId, const std::vector>& jsParams) +{ + std::string strResult = ""; + CHECK_NULL_RETURN(resourceManager_, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = resourceManager_->GetStringFormatById(resId, strResult, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get string format by id error, id=%{public}u, errorCode=%{public}d", resId, + state); + } + return strResult; +} + +std::string ResourceAdapterImpl::GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) +{ + std::string strResult = ""; + CHECK_NULL_RETURN(resourceManager_, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = resourceManager_->GetStringFormatByName(resName.c_str(), strResult, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get string format by name error, name=%{public}s, errorCode=%{public}d", + resName.c_str(), state); + } + return strResult; +} + +std::string ResourceAdapterImpl::GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) +{ + std::string strResult = ""; + CHECK_NULL_RETURN(resourceManager_, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = resourceManager_->GetFormatPluralStringById(strResult, resId, quantity, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get plural string format by id error, id=%{public}u, errorCode=%{public}d", + resId, state); + } + return strResult; +} + +std::string ResourceAdapterImpl::GetFormatPluralStringByName( + const std::string& resName, int32_t quantity, const std::vector>& jsParams) +{ + std::string strResult = ""; + CHECK_NULL_RETURN(resourceManager_, strResult); + auto globalParams = ConvertToGlobalParams(jsParams); + auto state = resourceManager_->GetFormatPluralStringByName(strResult, resName.c_str(), quantity, globalParams); + if (state != Global::Resource::SUCCESS) { + TAG_LOGW(AceLogTag::ACE_RESOURCE, + "Get plural string format by name error, name=%{public}s, errorCode=%{public}d", resName.c_str(), state); + } + return strResult; +} } // namespace OHOS::Ace diff --git a/adapter/preview/osal/resource_adapter_impl_standard.h b/adapter/preview/osal/resource_adapter_impl_standard.h index 529fdacd58a..5e4ed805b70 100644 --- a/adapter/preview/osal/resource_adapter_impl_standard.h +++ b/adapter/preview/osal/resource_adapter_impl_standard.h @@ -65,6 +65,15 @@ public: ColorMode GetResourceColorMode() const override; RefPtr GetOverrideResourceAdapter( const ResourceConfiguration& config, const ConfigurationChange& configurationChange) override; + std::string GetStringFormatById( + uint32_t resId, const std::vector>& jsParams) override; + std::string GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) override; + std::string GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) override; + std::string GetFormatPluralStringByName(const std::string& resName, int32_t quantity, + const std::vector>& jsParams) override; + private: static std::string GetActualResourceName(const std::string& resName); diff --git a/adapter/preview/osal/resource_convertor.cpp b/adapter/preview/osal/resource_convertor.cpp index b76a0c23467..8cd7db5753a 100644 --- a/adapter/preview/osal/resource_convertor.cpp +++ b/adapter/preview/osal/resource_convertor.cpp @@ -146,4 +146,23 @@ Global::Resource::ColorMode ConvertColorModeToGlobal(ColorMode colorMode) return Global::Resource::ColorMode::COLOR_MODE_NOT_SET; } } + +std::vector> ConvertToGlobalParams( + const std::vector>& jsParams) +{ + std::vector> globalParams; + globalParams.reserve(jsParams.size()); + + for (const auto& jsParam : jsParams) { + if (jsParam.first == JsValueType::JS_NUMBER) { + auto globalParam = std::make_tuple(NapiValueType::NAPI_NUMBER, jsParam.second); + globalParams.push_back(globalParam); + } else if (jsParam.first == JsValueType::JS_STRING) { + auto globalParam = std::make_tuple(NapiValueType::NAPI_STRING, jsParam.second); + globalParams.push_back(globalParam); + } + } + + return globalParams; +} } // namespace OHOS::Ace diff --git a/adapter/preview/osal/resource_convertor.h b/adapter/preview/osal/resource_convertor.h index 53e273ef72c..969371e6c9a 100644 --- a/adapter/preview/osal/resource_convertor.h +++ b/adapter/preview/osal/resource_convertor.h @@ -21,10 +21,12 @@ #include "base/utils/utils.h" #include "base/utils/resource_configuration.h" #include "core/common/ace_application_info.h" +#include "core/components/theme/resource_adapter.h" namespace OHOS::Ace { constexpr double DPI_BASE = 160.0; +using NapiValueType = OHOS::Global::Resource::ResourceManager::NapiValueType; Global::Resource::DeviceType ConvertDeviceTypeToGlobal(DeviceType type); Global::Resource::Direction ConvertDirectionToGlobal(DeviceOrientation orientation); @@ -35,7 +37,8 @@ std::shared_ptr ConvertConfigToGlobal(const Resourc DeviceType ConvertDeviceTypeToAce(Global::Resource::DeviceType type); DeviceOrientation ConvertDirectionToAce(Global::Resource::Direction orientation); double ConvertDensityToAce(Global::Resource::ScreenDensity density); - +std::vector> ConvertToGlobalParams( + const std::vector>& jsParams); } // namespace OHOS::Ace #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_OSAL_RESOURCE_CONVERTOR_H \ No newline at end of file diff --git a/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.cpp b/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.cpp index 09631e068b1..af0ab6905eb 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.cpp @@ -50,6 +50,7 @@ #include "bridge/declarative_frontend/engine/functions/js_on_size_change_function.h" #include "bridge/declarative_frontend/engine/functions/js_should_built_in_recognizer_parallel_with_function.h" #include "bridge/declarative_frontend/engine/functions/js_touch_intercept_function.h" +#include "bridge/declarative_frontend/engine/js_types.h" #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_utils_bridge.h" #include "bridge/declarative_frontend/engine/jsi/js_ui_index.h" #include "bridge/declarative_frontend/engine/js_converter.h" @@ -58,6 +59,8 @@ #include "bridge/declarative_frontend/jsview/js_utils.h" #include "bridge/declarative_frontend/jsview/js_view_context.h" #include "bridge/declarative_frontend/jsview/models/view_abstract_model_impl.h" +#include "core/common/resource/resource_wrapper.h" +#include "core/components/theme/resource_adapter.h" #include "canvas_napi/js_canvas.h" #if !defined(PREVIEW) && defined(OHOS_PLATFORM) #include "interfaces/inner_api/ui_session/ui_session_manager.h" @@ -318,80 +321,6 @@ void SetBgImgPosition(const DimensionUnit& typeX, const DimensionUnit& typeY, co bgImgPosition.SetSizeY(AnimatableDimension(valueY, typeY, option)); } -std::string GetReplaceContentStr(int pos, const std::string& type, JSRef params, int32_t containCount) -{ - auto index = pos + containCount; - if (index < 0) { - return std::string(); - } - - JSRef item = params->GetValueAt(static_cast(index)); - if (type == "d") { - if (item->IsNumber()) { - return std::to_string(item->ToNumber()); - } else if (item->IsObject()) { - int32_t result = 0; - JSViewAbstract::ParseJsInteger(item, result); - return std::to_string(result); - } - } else if (type == "s") { - if (item->IsString()) { - return item->ToString(); - } else if (item->IsObject()) { - std::string result; - JSViewAbstract::ParseJsString(item, result); - return result; - } - } else if (type == "f") { - if (item->IsNumber()) { - return std::to_string(item->ToNumber()); - } else if (item->IsObject()) { - double result = 0.0; - JSViewAbstract::ParseJsDouble(item, result); - return std::to_string(result); - } - } - return std::string(); -} - -void ReplaceHolder(std::string& originStr, JSRef params, int32_t containCount) -{ - auto size = static_cast(params->Length()); - if (containCount == size) { - return; - } - std::string::const_iterator start = originStr.begin(); - std::string::const_iterator end = originStr.end(); - std::smatch matches; - bool shortHolderType = false; - bool firstMatch = true; - int searchTime = 0; - while (std::regex_search(start, end, matches, RESOURCE_APP_STRING_PLACEHOLDER)) { - std::string pos = matches[2]; - std::string type = matches[4]; - if (firstMatch) { - firstMatch = false; - shortHolderType = pos.length() == 0; - } else { - if (shortHolderType ^ (pos.length() == 0)) { - return; - } - } - - std::string replaceContentStr; - if (shortHolderType) { - replaceContentStr = GetReplaceContentStr(searchTime, type, params, containCount); - } else { - replaceContentStr = GetReplaceContentStr(StringToInt(pos) - 1, type, params, containCount); - } - - originStr.replace(matches[0].first - originStr.begin(), matches[0].length(), replaceContentStr); - start = originStr.begin() + matches.prefix().length() + replaceContentStr.length(); - end = originStr.end(); - searchTime++; - } -} - bool ParseLocationProps(const JSRef& sizeObj, CalcDimension& x, CalcDimension& y) { JSRef xVal = sizeObj->GetProperty(static_cast(ArkUIIndex::X)); @@ -1515,6 +1444,38 @@ std::function ParseTransitionCallback(const JSRef& jsFunc, c }; return finishCallback; } + +std::pair MakeJsParamsFromResource(const JSRef& jsVal) +{ + std::string result; + JSViewAbstract::ParseJsString(jsVal, result); + + JSRef jsObj = JSRef::Cast(jsVal); + auto type = jsObj->GetPropertyValue("type", UNKNOWN_RESOURCE_TYPE); + if (type == static_cast(ResourceType::FLOAT) || type == static_cast(ResourceType::INTEGER)) { + return std::make_pair(JsValueType::JS_NUMBER, result); + } + return std::make_pair(JsValueType::JS_STRING, result); +} + +void MakeJsParams( + const JSRef& params, uint32_t startIndex, std::vector>& jsParams) +{ + auto length = params->Length(); + for (auto i = startIndex; i < length; i++) { + auto param = params->GetValueAt(i); + if (param->IsNumber()) { + auto jsParam = std::make_pair(JsValueType::JS_NUMBER, param->ToString()); + jsParams.emplace_back(jsParam); + } else if (param->IsString()) { + auto jsParam = std::make_pair(JsValueType::JS_STRING, param->ToString()); + jsParams.emplace_back(jsParam); + } else if (param->IsObject()) { + auto jsParam = MakeJsParamsFromResource(param); + jsParams.emplace_back(jsParam); + } + } +} } // namespace RefPtr GetResourceObject(const JSRef& jsObj) @@ -5737,30 +5698,50 @@ bool JSViewAbstract::ParseJsString(const JSRef& jsValue, std::string& res if (!IsGetResourceByName(jsObj)) { return false; } - auto param = params->GetValueAt(0); - if (type == static_cast(ResourceType::STRING)) { - auto originStr = resourceWrapper->GetStringByName(param->ToString()); - ReplaceHolder(originStr, params, 1); - result = originStr; - } else if (type == static_cast(ResourceType::PLURAL)) { - auto countJsVal = params->GetValueAt(1); - int count = 0; - if (!countJsVal->IsNumber()) { - return false; - } - count = countJsVal->ToNumber(); - auto pluralStr = resourceWrapper->GetPluralStringByName(param->ToString(), count); - ReplaceHolder(pluralStr, params, 2); - result = pluralStr; + return ParseJsStringByName(params, type, resourceWrapper, result); + } + return ParseJsStringById(resId->ToNumber(), type, params, resourceWrapper, result); +} + +bool JSViewAbstract::ParseJsStringByName( + const JSRef& params, int32_t type, RefPtr resourceWrapper, std::string& result) +{ + auto param = params->GetValueAt(0); + if (type == static_cast(ResourceType::STRING)) { + if (params->Length() > 1) { + std::vector> jsParams; + MakeJsParams(params, 1, jsParams); + result = resourceWrapper->GetStringFormatByName(param->ToString(), jsParams); } else { + result = resourceWrapper->GetStringByName(param->ToString()); + } + } else if (type == static_cast(ResourceType::PLURAL)) { + auto countJsVal = params->GetValueAt(1); + int count = 0; + if (!countJsVal->IsNumber()) { return false; } - return true; + count = countJsVal->ToNumber(); + std::vector> jsParams; + MakeJsParams(params, 2, jsParams); + result = resourceWrapper->GetFormatPluralStringByName(param->ToString(), count, jsParams); + } else { + return false; } + return true; +} + +bool JSViewAbstract::ParseJsStringById(uint32_t resId, int32_t type, const JSRef& params, + RefPtr resourceWrapper, std::string& result) +{ if (type == static_cast(ResourceType::STRING)) { - auto originStr = resourceWrapper->GetString(resId->ToNumber()); - ReplaceHolder(originStr, params, 0); - result = originStr; + if (params->Length() > 0) { + std::vector> jsParams; + MakeJsParams(params, 0, jsParams); + result = resourceWrapper->GetStringFormatById(resId, jsParams); + } else { + result = resourceWrapper->GetString(resId); + } } else if (type == static_cast(ResourceType::PLURAL)) { auto countJsVal = params->GetValueAt(0); int count = 0; @@ -5768,13 +5749,13 @@ bool JSViewAbstract::ParseJsString(const JSRef& jsValue, std::string& res return false; } count = countJsVal->ToNumber(); - auto pluralStr = resourceWrapper->GetPluralString(resId->ToNumber(), count); - ReplaceHolder(pluralStr, params, 1); - result = pluralStr; + std::vector> jsParams; + MakeJsParams(params, 1, jsParams); + result = resourceWrapper->GetFormatPluralStringById(resId, count, jsParams); } else if (type == static_cast(ResourceType::FLOAT)) { - result = std::to_string(resourceWrapper->GetDouble(resId->ToNumber())); + result = std::to_string(resourceWrapper->GetDouble(resId)); } else if (type == static_cast(ResourceType::INTEGER)) { - result = std::to_string(resourceWrapper->GetInt(resId->ToNumber())); + result = std::to_string(resourceWrapper->GetInt(resId)); } else { return false; } diff --git a/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h b/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h index af47afc2822..6eeed59f216 100755 --- a/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h +++ b/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h @@ -643,6 +643,10 @@ private: const JSRef& jsObj, int32_t resType, const RefPtr& resourceWrapper, double& result); static bool ParseResourceToDoubleById( int32_t resId, int32_t resType, const RefPtr& resourceWrapper, double& result); + static bool ParseJsStringByName( + const JSRef& params, int32_t type, RefPtr resourceWrapper, std::string& result); + static bool ParseJsStringById(uint32_t resId, int32_t type, const JSRef& params, + RefPtr resourceWrapper, std::string& result); static void ParseOnCreateMenu( const JSCallbackInfo& info, const JSRef& jsFunc, NG::OnCreateMenuCallback& onCreateMenuCallback); diff --git a/frameworks/core/common/resource/resource_wrapper.h b/frameworks/core/common/resource/resource_wrapper.h index 30d2689eeab..dbcf3d9eb32 100644 --- a/frameworks/core/common/resource/resource_wrapper.h +++ b/frameworks/core/common/resource/resource_wrapper.h @@ -113,6 +113,15 @@ public: return themeConstants_->GetString(key); } + std::string GetStringFormatById( + uint32_t key, const std::vector>& jsParams) const + { + if (SystemProperties::GetResourceDecoupling()) { + return resourceAdapter_->GetStringFormatById(key, jsParams); + } + return themeConstants_->GetStringFormatById(key, jsParams); + } + std::string GetStringByName(const std::string& resName) const { if (SystemProperties::GetResourceDecoupling()) { @@ -121,6 +130,15 @@ public: return themeConstants_->GetStringByName(resName); } + std::string GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) const + { + if (SystemProperties::GetResourceDecoupling()) { + return resourceAdapter_->GetStringFormatByName(resName, jsParams); + } + return themeConstants_->GetStringFormatByName(resName, jsParams); + } + std::string GetPluralString(uint32_t key, int count) const { if (SystemProperties::GetResourceDecoupling()) { @@ -129,6 +147,15 @@ public: return themeConstants_->GetPluralString(key, count); } + std::string GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) + { + if (SystemProperties::GetResourceDecoupling()) { + return resourceAdapter_->GetFormatPluralStringById(resId, quantity, jsParams); + } + return themeConstants_->GetFormatPluralStringById(resId, quantity, jsParams); + } + std::string GetPluralStringByName(const std::string& resName, int count) const { if (SystemProperties::GetResourceDecoupling()) { @@ -137,6 +164,15 @@ public: return themeConstants_->GetPluralStringByName(resName, count); } + std::string GetFormatPluralStringByName( + const std::string& resName, int32_t quantity, const std::vector>& jsParams) + { + if (SystemProperties::GetResourceDecoupling()) { + return resourceAdapter_->GetFormatPluralStringByName(resName, quantity, jsParams); + } + return themeConstants_->GetFormatPluralStringByName(resName, quantity, jsParams); + } + bool GetBoolean(uint32_t key) const { if (SystemProperties::GetResourceDecoupling()) { diff --git a/frameworks/core/components/theme/resource_adapter.h b/frameworks/core/components/theme/resource_adapter.h index a850194e3b3..813f121add6 100644 --- a/frameworks/core/components/theme/resource_adapter.h +++ b/frameworks/core/components/theme/resource_adapter.h @@ -16,6 +16,8 @@ #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_RESOURCE_ADAPTER_H #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_RESOURCE_ADAPTER_H +#include +#include #include "base/image/pixel_map.h" #include "base/utils/resource_configuration.h" #include "core/common/resource/resource_configuration.h" @@ -31,6 +33,11 @@ struct RawfileDescription { long length = 0; }; +enum class JsValueType : uint32_t { + JS_NUMBER = 0, + JS_STRING = 1, +}; + class ResourceAdapter : public virtual AceType { DECLARE_ACE_TYPE(ResourceAdapter, AceType); @@ -242,6 +249,18 @@ public: { return nullptr; } + + virtual std::string GetStringFormatById( + uint32_t resId, const std::vector>& jsParams) = 0; + + virtual std::string GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) = 0; + + virtual std::string GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) = 0; + + virtual std::string GetFormatPluralStringByName(const std::string& resName, int32_t quantity, + const std::vector>& jsParams) = 0; }; } // namespace OHOS::Ace diff --git a/frameworks/core/components/theme/theme_constants.cpp b/frameworks/core/components/theme/theme_constants.cpp index a3056c369dd..590630d1376 100644 --- a/frameworks/core/components/theme/theme_constants.cpp +++ b/frameworks/core/components/theme/theme_constants.cpp @@ -218,6 +218,15 @@ std::string ThemeConstants::GetString(uint32_t key) const return stringPair.second; } +std::string ThemeConstants::GetStringFormatById( + uint32_t key, const std::vector>& jsParams) const +{ + if (!resAdapter_) { + return ""; + } + return resAdapter_->GetStringFormatById(key, jsParams); +} + std::string ThemeConstants::GetStringByName(const std::string& resName) const { if (!resAdapter_) { @@ -226,6 +235,15 @@ std::string ThemeConstants::GetStringByName(const std::string& resName) const return resAdapter_->GetStringByName(resName); } +std::string ThemeConstants::GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) const +{ + if (!resAdapter_) { + return ""; + } + return resAdapter_->GetStringFormatByName(resName, jsParams); +} + std::string ThemeConstants::GetPluralString(uint32_t key, int count) const { if (IsGlobalResource(key)) { @@ -246,6 +264,15 @@ std::string ThemeConstants::GetPluralString(uint32_t key, int count) const return stringPair.second; } +std::string ThemeConstants::GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) const +{ + if (!resAdapter_) { + return ""; + } + return resAdapter_->GetFormatPluralStringById(resId, quantity, jsParams); +} + std::string ThemeConstants::GetPluralStringByName(const std::string& resName, int count) const { if (!resAdapter_) { @@ -254,6 +281,15 @@ std::string ThemeConstants::GetPluralStringByName(const std::string& resName, in return resAdapter_->GetPluralStringByName(resName, count); } +std::string ThemeConstants::GetFormatPluralStringByName(const std::string& resName, int32_t quantity, + const std::vector>& jsParams) const +{ + if (!resAdapter_) { + return ""; + } + return resAdapter_->GetFormatPluralStringByName(resName, quantity, jsParams); +} + std::vector ThemeConstants::GetStringArray(uint32_t key) const { if (IsGlobalResource(key)) { diff --git a/frameworks/core/components/theme/theme_constants.h b/frameworks/core/components/theme/theme_constants.h index 0e6611eabf5..64c9b5f58f9 100644 --- a/frameworks/core/components/theme/theme_constants.h +++ b/frameworks/core/components/theme/theme_constants.h @@ -133,6 +133,16 @@ public: */ std::string GetString(uint32_t key) const; + /* + * Get string value from platform constants with format. + * NOTE: empty string will be returned if not found or value is not string. + * @param[in] key Target key. + * @param[in] jsParams format values. + * @return String value corresponding to the key. + */ + std::string GetStringFormatById( + uint32_t key, const std::vector>& jsParams) const; + /* * Get string value from platform constants. * NOTE: empty string will be returned if not found or value is not string. @@ -141,6 +151,16 @@ public: */ std::string GetStringByName(const std::string& resName) const; + /* + * Get string value from platform constants by name. + * NOTE: empty string will be returned if not found or value is not string. + * @param[in] resName Target resName. + * @param[in] jsParams format values. + * @return String value corresponding to the key. + */ + std::string GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) const; + /* * Get plural string value from platform constants. * NOTE: empty string will be returned if not found. @@ -149,6 +169,15 @@ public: */ std::string GetPluralString(uint32_t key, int count) const; + /* + * Get plural string value from platform constants with format. + * NOTE: empty string will be returned if not found. + * @param[in] key Target key, count Target plural string quantity. + * @return plural string value corresponding to the key and count. + */ + std::string GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) const; + /* * Get plural string value from platform constants. * NOTE: empty string will be returned if not found. @@ -157,6 +186,15 @@ public: */ std::string GetPluralStringByName(const std::string& resName, int count) const; + /* + * Get plural string value from platform constants with format. + * NOTE: empty string will be returned if not found. + * @param[in] key Target key, count Target plural string quantity. + * @return plural string value corresponding to the name and count. + */ + std::string GetFormatPluralStringByName(const std::string& resName, int32_t quantity, + const std::vector>& jsParams) const; + /* * Get bool value from platform constants. * NOTE: false will be returned if not found or value is not boolean. diff --git a/test/mock/core/common/mock_resource_adapter_v2.h b/test/mock/core/common/mock_resource_adapter_v2.h index 55b94d0f6c6..471173a75eb 100644 --- a/test/mock/core/common/mock_resource_adapter_v2.h +++ b/test/mock/core/common/mock_resource_adapter_v2.h @@ -55,6 +55,30 @@ public: { return 0; } + + std::string GetStringFormatById( + uint32_t resId, const std::vector>& jsParams) override + { + return ""; + } + + std::string GetStringFormatByName( + const std::string& resName, const std::vector>& jsParams) override + { + return ""; + } + + std::string GetFormatPluralStringById( + uint32_t resId, int32_t quantity, const std::vector>& jsParams) override + { + return ""; + } + + std::string GetFormatPluralStringByName(const std::string& resName, int32_t quantity, + const std::vector>& jsParams) override + { + return ""; + } }; } // namespace OHOS::Ace -- Gitee