#include #include #include #include #include #include #include #include namespace linguistic { OString Translate(const OString& rTargetLang, const OString& rAPIUrl, const OString& rAuthKey, const OString& rData) { constexpr tools::Long CURL_TIMEOUT = 10L; std::unique_ptr> curl(curl_easy_init(), [](CURL* p) { curl_easy_cleanup(p); }); (void)curl_easy_setopt(curl.get(), CURLOPT_URL, rAPIUrl.getStr()); (void)curl_easy_setopt(curl.get(), CURLOPT_FAILONERROR, 1L); (void)curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, CURL_TIMEOUT); std::string response_body; (void)curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, +[](void* buffer, size_t size, size_t nmemb, void* userp) -> size_t { if (!userp) return 0; std::string* response = static_cast(userp); size_t real_size = size * nmemb; response->append(static_cast(buffer), real_size); return real_size; }); (void)curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, static_cast(&response_body)); OString aLang(curl_easy_escape(curl.get(), rTargetLang.getStr(), rTargetLang.getLength())); OString aAuthKey(curl_easy_escape(curl.get(), rAuthKey.getStr(), rAuthKey.getLength())); OString aData(curl_easy_escape(curl.get(), rData.getStr(), rData.getLength())); OString aPostData("auth_key=" + aAuthKey + "&target_lang=" + aLang + "&text=" + aData); (void)curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, aPostData.getStr()); CURLcode cc = curl_easy_perform(curl.get()); if (cc != CURLE_OK) { SAL_WARN("linguistic", "Translate: CURL perform returned with error: " << static_cast(cc)); return {}; } tools::Long nStatusCode; curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &nStatusCode); if (nStatusCode != 200) { SAL_WARN("linguistic", "Translate: CURL request returned with status code: " << nStatusCode); return {}; } // parse the response boost::property_tree::ptree root; std::stringstream aStream(response_body.data()); boost::property_tree::read_json(aStream, root); boost::property_tree::ptree& translations = root.get_child("translations"); size_t size = translations.size(); if (size <= 0) { SAL_WARN("linguistic", "Translate: API did not return any translations"); } // take the first one const boost::property_tree::ptree& translation = translations.begin()->second; const std::string text = translation.get("text"); return OString(text); } } ollabora/co-24.04.6'>distro/collabora/co-24.04.6 LibreOffice 核心代码仓库文档基金会
summaryrefslogtreecommitdiff
7d9d026ccc650'>tdf#42949 Fix IWYU warnings in sd/
AgeCommit message (Expand)Author
2023-04-25Make bin/includebloat.awk script output prettierGabor Kelemen
2015-07-10rudimentary documentation for the includebloat scriptMichael Stahl
2015-07-10add script bin/includebloatMichael Stahl
Gabor Kelemen
2019-08-16use more TOOLS_WARN_EXCEPTIONNoel Grandin
2019-07-18cid#1448356 Uncaught exceptionCaolán McNamara
2019-03-07log nice exception messages whereever possibleNoel Grandin
2019-01-21tdf#42949 Fix IWYU warnings in sd/source/{core,helper}/*hxxGabor Kelemen
2018-08-30don't declare allocation operators for internal codeNoel Grandin
2018-07-30Add missing sal/log.hxx headersGabor Kelemen
2017-10-23loplugin:includeform: sdStephan Bergmann
2017-10-04add << operator for css::uno::ExceptionNoel Grandin
2017-05-31Remove some unnecessary scaffolding around SAL_WARN callsStephan Bergmann
2017-05-28remove unnecessary use of OString::getStrNoel Grandin
2017-05-07revert OSL_ASSERT changesChris Sherlock
2017-05-07tdf#43157: convert sd module away from OSL_ASSERT to assertChris Sherlock
2016-02-17use consistent #define checks for the Windows platformNoel Grandin
2014-06-25remove whitespacesMarkus Mohrhard
2014-06-05various: remove SAL_THROW macroNoel Grandin