Merge remote-tracking branch 'Blu3train/yandexTranslateExtension'

This commit is contained in:
Chenx221 2024-09-05 12:38:37 +08:00
commit fad9fb3d51
3 changed files with 243 additions and 1 deletions

View File

@ -61,7 +61,8 @@ foreach ($language in @{
"Replacer",
"Replacer Translated Text",
"Styler",
"Thread Linker"
"Thread Linker",
"Yandex Translate"
))
{
copy -Force -Recurse -Verbose -Destination "$folder/$arch/$extension.xdll" -Path "Release_$arch/$extension.dll";

View File

@ -21,6 +21,7 @@ add_library(Replacer MODULE replacer.cpp extensionimpl.cpp)
add_library(Replacer\ Translated\ Text MODULE replacertranslatedtext.cpp extensionimpl.cpp)
add_library(Styler MODULE styler.cpp extensionimpl.cpp)
add_library(Thread\ Linker MODULE threadlinker.cpp extensionimpl.cpp)
add_library(Yandex\ Translate MODULE yandextranslate.cpp translatewrapper.cpp network.cpp extensionimpl.cpp)
target_precompile_headers(Bing\ Translate REUSE_FROM pch)
target_precompile_headers(Copy\ to\ Clipboard REUSE_FROM pch)
@ -43,6 +44,7 @@ target_precompile_headers(Replacer REUSE_FROM pch)
target_precompile_headers(Replacer\ Translated\ Text REUSE_FROM pch)
target_precompile_headers(Styler REUSE_FROM pch)
target_precompile_headers(Thread\ Linker REUSE_FROM pch)
target_precompile_headers(Yandex\ Translate REUSE_FROM pch)
target_link_libraries(Bing\ Translate winhttp Qt5::Widgets)
target_link_libraries(DeepL\ Translate winhttp Qt5::Widgets)
@ -55,6 +57,7 @@ target_link_libraries(Lua lua53 Qt5::Widgets)
target_link_libraries(Regex\ Filter Qt5::Widgets)
target_link_libraries(Styler Qt5::Widgets)
target_link_libraries(Thread\ Linker Qt5::Widgets)
target_link_libraries(Yandex\ Translate winhttp Qt5::Widgets)
add_custom_target(Cleaner ALL COMMAND del *.xdll WORKING_DIRECTORY ${CMAKE_FINAL_OUTPUT_DIRECTORY})

View File

@ -0,0 +1,238 @@
#include "qtcommon.h"
#include "translatewrapper.h"
#include "network.h"
extern const wchar_t* TRANSLATION_ERROR;
const char* TRANSLATION_PROVIDER = "Yandex Translate";
const char* GET_API_KEY_FROM = nullptr;
extern const QStringList languagesTo
{
"Afrikaans",
"Albanian",
"Amharic",
"Arabic",
"Armenian",
"Azerbaijani",
"Bashkir",
"Basque",
"Belarusian",
"Bengali",
"Bosnian",
"Bulgarian",
"Burmese",
"Catalan",
"Cebuano",
"Chinese",
"Chuvash",
"Croatian",
"Czech",
"Danish",
"Dutch",
"Elvish (Sindarin)",
"Emoji",
"English",
"Esperanto",
"Estonian",
"Finnish",
"French",
"Galician",
"Georgian",
"German",
"Greek",
"Gujarati",
"Haitian",
"Hebrew",
"Hill Mari",
"Hindi",
"Hungarian",
"Icelandic",
"Indonesian",
"Irish",
"Italian",
"Japanese",
"Javanese",
"Kannada",
"Kazakh",
"Kazakh (Latin)",
"Khmer",
"Korean",
"Kyrgyz",
"Lao",
"Latin",
"Latvian",
"Lithuanian",
"Luxembourgish",
"Macedonian",
"Malagasy",
"Malay",
"Malayalam",
"Maltese",
"Maori",
"Marathi",
"Mari",
"Mongolian",
"Nepali",
"Norwegian",
"Papiamento",
"Persian",
"Polish",
"Portuguese",
"Punjabi",
"Romanian",
"Russian",
"Scottish Gaelic",
"Serbian",
"Sinhalese",
"Slovak",
"Slovenian",
"Spanish",
"Sundanese",
"Swahili",
"Swedish",
"Tagalog",
"Tajik",
"Tamil",
"Tatar",
"Telugu",
"Thai",
"Turkish",
"Udmurt",
"Ukrainian",
"Urdu",
"Uzbek",
"Uzbek (Cyrillic)",
"Vietnamese",
"Welsh",
"Xhosa",
"Yakut",
"Yiddish",
"Zulu"
}, languagesFrom = languagesTo;
extern const std::unordered_map<std::wstring, std::wstring> codes
{
{ { L"Afrikaans" }, { L"af" } },
{ { L"Albanian" }, { L"sq" } },
{ { L"Amharic" }, { L"am" } },
{ { L"Arabic" }, { L"ar" } },
{ { L"Armenian" }, { L"hy" } },
{ { L"Azerbaijani" }, { L"az" } },
{ { L"Bashkir" }, { L"ba" } },
{ { L"Basque" }, { L"eu" } },
{ { L"Belarusian" }, { L"be" } },
{ { L"Bengali" }, { L"bn" } },
{ { L"Bosnian" }, { L"bs" } },
{ { L"Bulgarian" }, { L"bg" } },
{ { L"Burmese" }, { L"my" } },
{ { L"Catalan" }, { L"ca" } },
{ { L"Cebuano" }, { L"ceb" } },
{ { L"Chinese" }, { L"zh" } },
{ { L"Chuvash" }, { L"cv" } },
{ { L"Croatian" }, { L"hr" } },
{ { L"Czech" }, { L"cs" } },
{ { L"Danish" }, { L"da" } },
{ { L"Dutch" }, { L"nl" } },
{ { L"Elvish (Sindarin)" }, { L"sjn" } },
{ { L"Emoji" }, { L"emj" } },
{ { L"English" }, { L"en" } },
{ { L"Esperanto" }, { L"eo" } },
{ { L"Estonian" }, { L"et" } },
{ { L"Finnish" }, { L"fi" } },
{ { L"French" }, { L"fr" } },
{ { L"Galician" }, { L"gl" } },
{ { L"Georgian" }, { L"ka" } },
{ { L"German" }, { L"de" } },
{ { L"Greek" }, { L"el" } },
{ { L"Gujarati" }, { L"gu" } },
{ { L"Haitian" }, { L"ht" } },
{ { L"Hebrew" }, { L"he" } },
{ { L"Hill Mari" }, { L"mrj" } },
{ { L"Hindi" }, { L"hi" } },
{ { L"Hungarian" }, { L"hu" } },
{ { L"Icelandic" }, { L"is" } },
{ { L"Indonesian" }, { L"id" } },
{ { L"Irish" }, { L"ga" } },
{ { L"Italian" }, { L"it" } },
{ { L"Japanese" }, { L"ja" } },
{ { L"Javanese" }, { L"jv" } },
{ { L"Kannada" }, { L"kn" } },
{ { L"Kazakh" }, { L"kk" } },
{ { L"Kazakh (Latin)" }, { L"kazlat" } },
{ { L"Khmer" }, { L"km" } },
{ { L"Korean" }, { L"ko" } },
{ { L"Kyrgyz" }, { L"ky" } },
{ { L"Lao" }, { L"lo" } },
{ { L"Latin" }, { L"la" } },
{ { L"Latvian" }, { L"lv" } },
{ { L"Lithuanian" }, { L"lt" } },
{ { L"Luxembourgish" }, { L"lb" } },
{ { L"Macedonian" }, { L"mk" } },
{ { L"Malagasy" }, { L"mg" } },
{ { L"Malay" }, { L"ms" } },
{ { L"Malayalam" }, { L"ml" } },
{ { L"Maltese" }, { L"mt" } },
{ { L"Maori" }, { L"mi" } },
{ { L"Marathi" }, { L"mr" } },
{ { L"Mari" }, { L"mhr" } },
{ { L"Mongolian" }, { L"mn" } },
{ { L"Nepali" }, { L"ne" } },
{ { L"Norwegian" }, { L"no" } },
{ { L"Papiamento" }, { L"pap" } },
{ { L"Persian" }, { L"fa" } },
{ { L"Polish" }, { L"pl" } },
{ { L"Portuguese" }, { L"pt" } },
{ { L"Punjabi" }, { L"pa" } },
{ { L"Romanian" }, { L"ro" } },
{ { L"Russian" }, { L"ru" } },
{ { L"Scottish Gaelic" }, { L"gd" } },
{ { L"Serbian" }, { L"sr" } },
{ { L"Sinhalese" }, { L"si" } },
{ { L"Slovak" }, { L"sk" } },
{ { L"Slovenian" }, { L"sl" } },
{ { L"Spanish" }, { L"es" } },
{ { L"Sundanese" }, { L"su" } },
{ { L"Swahili" }, { L"sw" } },
{ { L"Swedish" }, { L"sv" } },
{ { L"Tagalog" }, { L"tl" } },
{ { L"Tajik" }, { L"tg" } },
{ { L"Tamil" }, { L"ta" } },
{ { L"Tatar" }, { L"tt" } },
{ { L"Telugu" }, { L"te" } },
{ { L"Thai" }, { L"th" } },
{ { L"Turkish" }, { L"tr" } },
{ { L"Udmurt" }, { L"udm" } },
{ { L"Ukrainian" }, { L"uk" } },
{ { L"Urdu" }, { L"ur" } },
{ { L"Uzbek" }, { L"uz" } },
{ { L"Uzbek (Cyrillic)" }, { L"uzbcyr" } },
{ { L"Vietnamese" }, { L"vi" } },
{ { L"Welsh" }, { L"cy" } },
{ { L"Xhosa" }, { L"xh" } },
{ { L"Yakut" }, { L"sah" } },
{ { L"Yiddish" }, { L"yi" } },
{ { L"Zulu" }, { L"zu" } },
{ { L"?" }, { L"auto" } }
};
bool translateSelectedOnly = true, useRateLimiter = true, rateLimitSelected = false, useCache = true, useFilter = true;
int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 1000;
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp)
{
//Thnx @Dangetsu
if (HttpRequest httpRequest{
L"Mozilla/5.0 Textractor",
L"api.browser.yandex.ru",
L"GET",
FormatString(L"/dictionary/translate?brandID=yandex&statLang=%s&targetLang=%s&url=http://vnr.aniclan.com/&locale=ru&text=%s", (tlp.translateFrom == L"?") ? codes.at(tlp.translateTo) : codes.at(tlp.translateFrom), codes.at(tlp.translateTo), Escape(text)).c_str()
}) {
//In case of sentence translation
if (auto translation = Copy(JSON::Parse(httpRequest.response)[L"text"].String())) {
if (translation.value() != L"") return { true, translation.value() };}
else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
//In case of single word translation
if (auto translation = Copy(JSON::Parse(httpRequest.response)[L"directions"][0][L"translations"][0][L"variants"][0][L"meanings"][0][L"Text"].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) };
}