merge google and google cloud

This commit is contained in:
Akash Mozumdar 2020-03-26 06:12:20 -06:00
parent 87c056a5b3
commit 8a710659dc
4 changed files with 53 additions and 178 deletions

View File

@ -41,7 +41,6 @@ foreach ($language in @{
"Extra Newlines.dll", "Extra Newlines.dll",
"Extra Window.dll", "Extra Window.dll",
"Google Translate.dll", "Google Translate.dll",
"Google Cloud Translate.dll",
"Lua.dll", "Lua.dll",
"Regex Filter.dll", "Regex Filter.dll",
"Remove Repeated Characters.dll", "Remove Repeated Characters.dll",

View File

@ -10,7 +10,6 @@ add_library(DeepL\ Translate MODULE deepltranslate.cpp translatewrapper.cpp netw
add_library(Extra\ Newlines MODULE extranewlines.cpp extensionimpl.cpp) add_library(Extra\ Newlines MODULE extranewlines.cpp extensionimpl.cpp)
add_library(Extra\ Window MODULE extrawindow.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(Google\ Translate MODULE googletranslate.cpp translatewrapper.cpp network.cpp extensionimpl.cpp)
add_library(Google\ Cloud\ Translate MODULE googlecloudtranslate.cpp translatewrapper.cpp network.cpp extensionimpl.cpp)
add_library(Lua MODULE lua.cpp extensionimpl.cpp) add_library(Lua MODULE lua.cpp extensionimpl.cpp)
add_library(Regex\ Filter MODULE regexfilter.cpp extensionimpl.cpp) add_library(Regex\ Filter MODULE regexfilter.cpp extensionimpl.cpp)
add_library(Remove\ Repeated\ Characters MODULE removerepeatchar.cpp extensionimpl.cpp) add_library(Remove\ Repeated\ Characters MODULE removerepeatchar.cpp extensionimpl.cpp)
@ -24,7 +23,6 @@ target_link_libraries(Bing\ Translate winhttp Qt5::Widgets)
target_link_libraries(DeepL\ Translate winhttp Qt5::Widgets) target_link_libraries(DeepL\ Translate winhttp Qt5::Widgets)
target_link_libraries(Extra\ Window Qt5::Widgets) target_link_libraries(Extra\ Window Qt5::Widgets)
target_link_libraries(Google\ Translate winhttp Qt5::Widgets) target_link_libraries(Google\ Translate winhttp Qt5::Widgets)
target_link_libraries(Google\ Cloud\ Translate winhttp Qt5::Widgets)
target_link_libraries(Lua lua53 Qt5::Widgets) target_link_libraries(Lua lua53 Qt5::Widgets)
target_link_libraries(Regex\ Filter Qt5::Widgets) target_link_libraries(Regex\ Filter Qt5::Widgets)
target_link_libraries(Thread\ Linker Qt5::Widgets) target_link_libraries(Thread\ Linker Qt5::Widgets)

View File

@ -1,119 +0,0 @@
#include "qtcommon.h"
#include "extension.h"
#include "network.h"
extern const wchar_t* TRANSLATION_ERROR;
extern const char* API_KEY;
extern QFormLayout* display;
extern QSettings settings;
extern Synchronized<std::wstring> translateTo;
const char* TRANSLATION_PROVIDER = "Google Cloud Translate";
QStringList languages
{
"Afrikaans: af",
"Arabic: ar",
"Albanian: sq",
"Belarusian: be",
"Bengali: bn",
"Bosnian: bs",
"Bulgarian: bg",
"Catalan: ca",
"Chinese(Simplified): zh-CH",
"Chinese(Traditional): zh-TW",
"Croatian: hr",
"Czech: cs",
"Danish: da",
"Dutch: nl",
"English: en",
"Esperanto: eo",
"Estonian: et",
"Filipino: tl",
"Finnish: fi",
"French: fr",
"Galician: gl",
"German: de",
"Greek: el",
"Hebrew: iw",
"Hindi: hi",
"Hungarian: hu",
"Icelandic: is",
"Indonesian: id",
"Irish: ga",
"Italian: it",
"Japanese: ja",
"Klingon: tlh",
"Korean: ko",
"Latin: la",
"Latvian: lv",
"Lithuanian: lt",
"Macedonian: mk",
"Malay: ms",
"Maltese: mt",
"Norwegian: no",
"Persian: fa",
"Polish: pl",
"Portuguese: pt",
"Romanian: ro",
"Russian: ru",
"Serbian: sr",
"Slovak: sk",
"Slovenian: sl",
"Somali: so",
"Spanish: es",
"Swahili: sw",
"Swedish: sv",
"Thai: th",
"Turkish: tr",
"Ukranian: uk",
"Urdu: ur",
"Vietnamese: vi",
"Welsh: cy",
"Yiddish: yi",
"Zulu: zu"
};
Synchronized<std::wstring> key;
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
auto keyInput = new QLineEdit(settings.value(API_KEY).toString());
key->assign(S(keyInput->text()));
QObject::connect(keyInput, &QLineEdit::textChanged, [](QString key) { settings.setValue(API_KEY, S(::key->assign(S(key)))); });
display->addRow(API_KEY, keyInput);
auto googleCloudInfo = new QLabel(
"<a href=\"https://codelabs.developers.google.com/codelabs/cloud-translation-intro\">https://codelabs.developers.google.com/codelabs/cloud-translation-intro</a>"
);
googleCloudInfo->setOpenExternalLinks(true);
display->addRow(googleCloudInfo);
}
break;
case DLL_PROCESS_DETACH:
{
}
break;
}
return TRUE;
}
std::pair<bool, std::wstring> Translate(const std::wstring& text, SentenceInfo)
{
if (HttpRequest httpRequest{
L"Mozilla/5.0 Textractor",
L"translation.googleapis.com",
L"GET",
FormatString(L"/language/translate/v2?format=text&q=%s&target=%s&key=%s", Escape(text), translateTo->c_str(), key->c_str()).c_str()
})
{
// Response formatted as JSON: starts with "translatedText": " and translation is enclosed in quotes followed by a comma
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"\"translatedText\": \"(.+?)\","))) return { true, results[1] };
return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
}
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
}

View File

@ -1,32 +1,28 @@
#include "extension.h" #include "qtcommon.h"
#include "extension.h"
#include "network.h" #include "network.h"
#include <ctime> #include <ctime>
#include <QStringList>
extern const wchar_t* TRANSLATION_ERROR; extern const wchar_t* TRANSLATION_ERROR;
extern const char* API_KEY;
extern QFormLayout* display;
extern QSettings settings;
extern Synchronized<std::wstring> translateTo; extern Synchronized<std::wstring> translateTo;
const char* TRANSLATION_PROVIDER = "Google Translate"; const char* TRANSLATION_PROVIDER = "Google Translate";
QStringList languages QStringList languages
{ {
"Afrikaans: af", "Afrikaans: af",
"Albanian: sq",
"Amharic: am",
"Arabic: ar", "Arabic: ar",
"Armenian: hy", "Albanian: sq",
"Azerbaijani: az",
"Basque: eu",
"Belarusian: be", "Belarusian: be",
"Bengali: bn", "Bengali: bn",
"Bosnian: bs", "Bosnian: bs",
"Bulgarian: bg", "Bulgarian: bg",
"Catalan: ca", "Catalan: ca",
"Cebuano: ceb", "Chinese(Simplified): zh-CH",
"Chichewa: ny",
"Chinese (Simplified): zh",
"Chinese(Traditional): zh-TW", "Chinese(Traditional): zh-TW",
"Corsican: co",
"Croatian: hr", "Croatian: hr",
"Czech: cs", "Czech: cs",
"Danish: da", "Danish: da",
@ -37,92 +33,77 @@ QStringList languages
"Filipino: tl", "Filipino: tl",
"Finnish: fi", "Finnish: fi",
"French: fr", "French: fr",
"Frisian: fy",
"Galician: gl", "Galician: gl",
"Georgian: ka",
"German: de", "German: de",
"Greek: el", "Greek: el",
"Gujarati: gu",
"Haitian Creole: ht",
"Hausa: ha",
"Hawaiian: haw",
"Hebrew: iw", "Hebrew: iw",
"Hindi: hi", "Hindi: hi",
"Hmong: hmn",
"Hungarian: hu", "Hungarian: hu",
"Icelandic: is", "Icelandic: is",
"Igbo: ig",
"Indonesian: id", "Indonesian: id",
"Irish: ga", "Irish: ga",
"Italian: it", "Italian: it",
"Japanese: ja", "Japanese: ja",
"Javanese: jw", "Klingon: tlh",
"Kannada: kn",
"Kazakh: kk",
"Khmer: km",
"Kinyarwanda: rw",
"Korean: ko", "Korean: ko",
"Kurdish (Kurmanji): ku",
"Kyrgyz: ky",
"Lao: lo",
"Latin: la", "Latin: la",
"Latvian: lv", "Latvian: lv",
"Lithuanian: lt", "Lithuanian: lt",
"Luxembourgish: lb",
"Macedonian: mk", "Macedonian: mk",
"Malagasy: mg",
"Malay: ms", "Malay: ms",
"Malayalam: ml",
"Maltese: mt", "Maltese: mt",
"Maori: mi",
"Marathi: mr",
"Mongolian: mn",
"Myanmar (Burmese): my",
"Nepali: ne",
"Norwegian: no", "Norwegian: no",
"Odia (Oriya): or",
"Pashto: ps",
"Persian: fa", "Persian: fa",
"Polish: pl", "Polish: pl",
"Portuguese: pt", "Portuguese: pt",
"Punjabi: pa",
"Romanian: ro", "Romanian: ro",
"Russian: ru", "Russian: ru",
"Samoan: sm",
"Scots Gaelic: gd",
"Serbian: sr", "Serbian: sr",
"Sesotho: st",
"Shona: sn",
"Sindhi: sd",
"Sinhala: si",
"Slovak: sk", "Slovak: sk",
"Slovenian: sl", "Slovenian: sl",
"Somali: so", "Somali: so",
"Spanish: es", "Spanish: es",
"Sundanese: su",
"Swahili: sw", "Swahili: sw",
"Swedish: sv", "Swedish: sv",
"Tajik: tg",
"Tamil: ta",
"Tatar: tt",
"Telugu: te",
"Thai: th", "Thai: th",
"Turkish: tr", "Turkish: tr",
"Turkmen: tk", "Ukranian: uk",
"Ukrainian: uk",
"Urdu: ur", "Urdu: ur",
"Uyghur: ug",
"Uzbek: uz",
"Vietnamese: vi", "Vietnamese: vi",
"Welsh: cy", "Welsh: cy",
"Xhosa: xh",
"Yiddish: yi", "Yiddish: yi",
"Yoruba: yo",
"Zulu: zu" "Zulu: zu"
}; };
Synchronized<std::wstring> key;
unsigned TKK = 0; unsigned TKK = 0;
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
auto keyInput = new QLineEdit(settings.value(API_KEY).toString());
key->assign(S(keyInput->text()));
QObject::connect(keyInput, &QLineEdit::textChanged, [](QString key) { settings.setValue(API_KEY, S(::key->assign(S(key)))); });
display->addRow(API_KEY, keyInput);
auto googleCloudInfo = new QLabel(
"<a href=\"https://codelabs.developers.google.com/codelabs/cloud-translation-intro\">https://codelabs.developers.google.com/codelabs/cloud-translation-intro</a>"
);
googleCloudInfo->setOpenExternalLinks(true);
display->addRow(googleCloudInfo);
}
break;
case DLL_PROCESS_DETACH:
{
}
break;
}
return TRUE;
}
std::wstring GetTranslationUri(const std::wstring& text) std::wstring GetTranslationUri(const std::wstring& text)
{ {
// If no TKK available, use this uri. Can't use too much or google will detect unauthorized access // If no TKK available, use this uri. Can't use too much or google will detect unauthorized access
@ -154,6 +135,22 @@ bool IsHash(const std::wstring& result)
std::pair<bool, std::wstring> Translate(const std::wstring& text, SentenceInfo) std::pair<bool, std::wstring> Translate(const std::wstring& text, SentenceInfo)
{ {
if (!key->empty())
{
if (HttpRequest httpRequest{
L"Mozilla/5.0 Textractor",
L"translation.googleapis.com",
L"GET",
FormatString(L"/language/translate/v2?format=text&q=%s&target=%s&key=%s", Escape(text), translateTo->c_str(), key->c_str()).c_str()
})
{
// Response formatted as JSON: starts with "translatedText": " and translation is enclosed in quotes followed by a comma
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"\"translatedText\": \"(.+?)\","))) return { true, results[1] };
return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
}
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
}
if (!TKK) if (!TKK)
if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"translate.google.com", L"GET", L"/" }) if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"translate.google.com", L"GET", L"/" })
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"(\\d{7,})'"))) if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"(\\d{7,})'")))