diff options
author | Andrey Lihatskiy <alihatskiy@productengine.com> | 2024-04-12 15:43:34 +0300 |
---|---|---|
committer | Andrey Lihatskiy <alihatskiy@productengine.com> | 2024-04-12 15:43:34 +0300 |
commit | eab5beb54cacc2b0dc2cddad4a78634e7468a298 (patch) | |
tree | 51f17910f529af57819bcd77f1a7dd9e92f6291e /indra/newview/lltranslate.cpp | |
parent | 00e09ddcad8ec2c33ecbcdd0da09bd7819bc3509 (diff) | |
parent | cc8fdf341dbbe6ab940d7b24bcf81cbed252cb69 (diff) |
Merge branch 'main' into marchcat/x-merge
# Conflicts:
# indra/llimage/llimageworker.cpp
# indra/llimage/llimageworker.h
# indra/newview/llcontrolavatar.cpp
# indra/newview/llfloaterprofiletexture.cpp
# indra/newview/lloutfitslist.cpp
# indra/newview/lloutfitslist.h
# indra/newview/lltexturefetch.cpp
Diffstat (limited to 'indra/newview/lltranslate.cpp')
-rw-r--r-- | indra/newview/lltranslate.cpp | 53 |
1 files changed, 27 insertions, 26 deletions
diff --git a/indra/newview/lltranslate.cpp b/indra/newview/lltranslate.cpp index 6526e1df92..979b495906 100644 --- a/indra/newview/lltranslate.cpp +++ b/indra/newview/lltranslate.cpp @@ -254,7 +254,7 @@ void LLTranslationAPIHandler::translateMessageCoro(LanguagePair_t fromTo, std::s try { - res = this->parseResponse(httpResults, parseResult, body, translation, detected_lang, err_msg); + res = parseResponse(httpResults, parseResult, body, translation, detected_lang, err_msg); } catch (std::out_of_range&) { @@ -294,8 +294,6 @@ void LLTranslationAPIHandler::translateMessageCoro(LanguagePair_t fromTo, std::s if (!failure.empty()) failure(status, err_msg); } - - } //========================================================================= @@ -354,7 +352,6 @@ private: std::string& translation, std::string& detected_lang); static std::string getAPIKey(); - }; //------------------------------------------------------------------------- @@ -392,36 +389,37 @@ bool LLGoogleTranslationHandler::checkVerificationResponse( // virtual bool LLGoogleTranslationHandler::parseResponse( - const LLSD& http_response, + const LLSD& http_response, int& status, const std::string& body, std::string& translation, std::string& detected_lang, std::string& err_msg) const { + const std::string& text = !body.empty() ? body : http_response["error_body"].asStringRef(); + Json::Value root; Json::Reader reader; - if (!reader.parse(body, root)) + if (reader.parse(text, root)) { - err_msg = reader.getFormatedErrorMessages(); - return false; + if (root.isObject()) + { + // Request succeeded, extract translation from the XML body. + if (parseTranslation(root, translation, detected_lang)) + return true; + + // Request failed. Extract error message from the XML body. + parseErrorResponse(root, status, err_msg); + } } - - if (!root.isObject()) // empty response? should not happen + else { - return false; - } - - if (status != HTTP_OK) - { - // Request failed. Extract error message from the response. - parseErrorResponse(root, status, err_msg); - return false; + // XML parsing failed. Extract error message from the XML parser. + err_msg = reader.getFormatedErrorMessages(); } - // Request succeeded, extract translation from the response. - return parseTranslation(root, translation, detected_lang); + return false; } // virtual @@ -494,7 +492,7 @@ void LLGoogleTranslationHandler::verifyKey(const LLSD &key, LLTranslate::KeyVeri /*virtual*/ void LLGoogleTranslationHandler::initHttpHeader(LLCore::HttpHeaders::ptr_t headers, const std::string& user_agent) const { - headers->append(HTTP_OUT_HEADER_ACCEPT, HTTP_CONTENT_TEXT_PLAIN); + headers->append(HTTP_OUT_HEADER_ACCEPT, HTTP_CONTENT_JSON); headers->append(HTTP_OUT_HEADER_USER_AGENT, user_agent); } @@ -504,8 +502,7 @@ void LLGoogleTranslationHandler::initHttpHeader( const std::string& user_agent, const LLSD &key) const { - headers->append(HTTP_OUT_HEADER_ACCEPT, HTTP_CONTENT_TEXT_PLAIN); - headers->append(HTTP_OUT_HEADER_USER_AGENT, user_agent); + initHttpHeader(headers, user_agent); } LLSD LLGoogleTranslationHandler::sendMessageAndSuspend(LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t adapter, @@ -729,7 +726,7 @@ bool LLAzureTranslationHandler::parseResponse( return false; } - translation = first["text"].asString(); + translation = LLURI::unescape(first["text"].asString()); return true; } @@ -829,8 +826,13 @@ LLSD LLAzureTranslationHandler::sendMessageAndSuspend(LLCoreHttpUtil::HttpCorout { LLCore::BufferArray::ptr_t rawbody(new LLCore::BufferArray); LLCore::BufferArrayStream outs(rawbody.get()); + + static const std::string allowed_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz " + "0123456789" + "-._~"; + outs << "[{\"text\":\""; - outs << msg; + outs << LLURI::escape(msg, allowed_chars); outs << "\"}]"; return adapter->postRawAndSuspend(request, url, rawbody, options, headers); @@ -1315,5 +1317,4 @@ LLTranslationAPIHandler& LLTranslate::getHandler(EService service) } return azure; - } |