diff --git a/extensions/CMakeLists.txt b/extensions/CMakeLists.txt index e4a5232..60e7e79 100644 --- a/extensions/CMakeLists.txt +++ b/extensions/CMakeLists.txt @@ -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) diff --git a/extensions/libretranslate.cpp b/extensions/libretranslate.cpp new file mode 100644 index 0000000..dbae220 --- /dev/null +++ b/extensions/libretranslate.cpp @@ -0,0 +1,47 @@ +#include "qtcommon.h" +#include "network.h" + +extern const wchar_t* TRANSLATION_ERROR; + +extern Synchronized 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 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) }; +}