mirror of
https://github.com/Artikash/Textractor.git
synced 2024-12-23 08:54:12 +08:00
add basic extension
This commit is contained in:
parent
aa0c0e0047
commit
a070a9e417
@ -10,6 +10,7 @@ add_library(DevTools\ DeepL\ Translate MODULE devtoolsdeepltranslate.cpp devtool
|
||||
add_library(Extra\ Newlines MODULE extranewlines.cpp extensionimpl.cpp)
|
||||
add_library(Extra\ Window MODULE extrawindow.cpp extensionimpl.cpp)
|
||||
add_library(Google\ Translate MODULE googletranslate.cpp translatewrapper.cpp network.cpp extensionimpl.cpp)
|
||||
add_library(Libre\ Translate MODULE libretranslate.cpp translatewrapper.cpp network.cpp extensionimpl.cpp)
|
||||
add_library(Lua MODULE lua.cpp extensionimpl.cpp)
|
||||
add_library(Regex\ Filter MODULE regexfilter.cpp extensionimpl.cpp)
|
||||
add_library(Remove\ Repeated\ Characters MODULE removerepeatchar.cpp extensionimpl.cpp)
|
||||
@ -27,6 +28,7 @@ target_precompile_headers(DevTools\ DeepL\ Translate REUSE_FROM pch)
|
||||
target_precompile_headers(Extra\ Newlines REUSE_FROM pch)
|
||||
target_precompile_headers(Extra\ Window REUSE_FROM pch)
|
||||
target_precompile_headers(Google\ Translate REUSE_FROM pch)
|
||||
target_precompile_headers(Libre\ Translate REUSE_FROM pch)
|
||||
target_precompile_headers(Lua REUSE_FROM pch)
|
||||
target_precompile_headers(Regex\ Filter REUSE_FROM pch)
|
||||
target_precompile_headers(Remove\ Repeated\ Characters REUSE_FROM pch)
|
||||
@ -42,6 +44,7 @@ target_link_libraries(DeepL\ Translate winhttp Qt5::Widgets)
|
||||
target_link_libraries(DevTools\ DeepL\ Translate shell32 winhttp Qt5::Widgets Qt5::WebSockets)
|
||||
target_link_libraries(Extra\ Window Qt5::Widgets)
|
||||
target_link_libraries(Google\ Translate winhttp Qt5::Widgets)
|
||||
target_link_libraries(Libre\ Translate winhttp Qt5::Widgets)
|
||||
target_link_libraries(Lua lua53 Qt5::Widgets)
|
||||
target_link_libraries(Regex\ Filter Qt5::Widgets)
|
||||
target_link_libraries(Styler Qt5::Widgets)
|
||||
|
47
extensions/libretranslate.cpp
Normal file
47
extensions/libretranslate.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "qtcommon.h"
|
||||
#include "network.h"
|
||||
|
||||
extern const wchar_t* TRANSLATION_ERROR;
|
||||
|
||||
extern Synchronized<std::wstring> translateTo, translateFrom;
|
||||
|
||||
const char* TRANSLATION_PROVIDER = "Libre Translate";
|
||||
const char* GET_API_KEY_FROM = nullptr;
|
||||
QStringList languages
|
||||
{
|
||||
"Arabic: ar",
|
||||
"Chinese: zh",
|
||||
"English: en",
|
||||
"French: fr",
|
||||
"German: de",
|
||||
"Hindi: hi",
|
||||
"Irish: ga",
|
||||
"Italian: it",
|
||||
"Japanese: ja",
|
||||
"Korean: ko",
|
||||
"Portuguese: pt",
|
||||
"Russian: ru",
|
||||
"Spanish: es"
|
||||
};
|
||||
std::wstring autoDetectLanguage = L"";
|
||||
|
||||
bool translateSelectedOnly = true, rateLimitAll = true, rateLimitSelected = false, useCache = true, useFilter = true;
|
||||
int tokenCount = 10, tokenRestoreDelay = 60000, maxSentenceSize = 10000;
|
||||
|
||||
std::pair<bool, std::wstring> Translate(const std::wstring& text)
|
||||
{
|
||||
if (HttpRequest httpRequest{
|
||||
L"Mozilla/5.0 Textractor",
|
||||
L"127.0.0.1",
|
||||
L"POST",
|
||||
L"/translate",
|
||||
FormatString("q=%S&source=%S&target=%S", Escape(text), translateFrom.Copy(), translateTo.Copy()),
|
||||
NULL,
|
||||
5000,
|
||||
NULL,
|
||||
0
|
||||
})
|
||||
if (auto translation = Copy(JSON::Parse(httpRequest.response)[L"translatedText"].String())) return { true, translation.value() };
|
||||
else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
|
||||
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user