better logging for translation extensions

This commit is contained in:
Akash Mozumdar 2020-03-17 22:01:16 -06:00
parent bc01179626
commit e4d9b70040
4 changed files with 18 additions and 24 deletions

View File

@ -89,6 +89,6 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
})
// Response formatted as JSON: translation starts with text":" and ends with ","to
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"text\":\"(.+)\",\"t"))) return { true, results[1] };
else return { false, TRANSLATION_ERROR };
else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
}

View File

@ -6,7 +6,7 @@ extern const wchar_t* TRANSLATION_ERROR;
extern const char* API_KEY;
extern QFormLayout* display;
extern QSettings* settings;
extern QSettings settings;
extern Synchronized<std::wstring> translateTo;
const char* TRANSLATION_PROVIDER = "Google Cloud Translate";
@ -82,9 +82,9 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
{
case DLL_PROCESS_ATTACH:
{
auto keyInput = new QLineEdit(settings->value(API_KEY).toString());
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)))); });
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>"
@ -113,7 +113,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
{
// 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, TRANSLATION_ERROR };
return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
}
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
}

View File

@ -169,7 +169,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
if (!IsHash(results[1])) translation += std::wstring(results[1]) + L" ";
if (!translation.empty()) return { true, translation };
}
return { false, FormatString(L"%s (TKK=%u)", TRANSLATION_ERROR, _InterlockedExchange(&TKK, 0)) };
return { false, FormatString(L"%s (TKK=%u): %s", TRANSLATION_ERROR, _InterlockedExchange(&TKK, 0), httpRequest.response) };
}
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
}

View File

@ -5,7 +5,6 @@
#include <map>
#include <fstream>
#include <QComboBox>
#include <QTimer>
extern const char* NATIVE_LANGUAGE;
extern const char* TRANSLATE_TO;
@ -19,7 +18,7 @@ const char* LANGUAGE = u8"Language";
const std::string TRANSLATION_CACHE_FILE = FormatString("%s Cache.txt", TRANSLATION_PROVIDER);
QFormLayout* display;
QSettings* settings;
QSettings settings = openSettings();
Synchronized<std::wstring> translateTo = L"en";
Synchronized<std::map<std::wstring, std::wstring>> translationCache;
@ -40,21 +39,20 @@ public:
Window() :
QDialog(nullptr, Qt::WindowMinMaxButtonsHint)
{
display = &layout;
::settings = &settings;
languageBox.addItems(languages);
display = new QFormLayout(this);
settings.beginGroup(TRANSLATION_PROVIDER);
int language = -1;
if (settings.contains(LANGUAGE)) language = languageBox.findText(settings.value(LANGUAGE).toString(), Qt::MatchEndsWith);
if (language < 0) language = languageBox.findText(NATIVE_LANGUAGE, Qt::MatchStartsWith);
if (language < 0) language = languageBox.findText("English", Qt::MatchStartsWith);
languageBox.setCurrentIndex(language);
saveLanguage(languageBox.currentText());
connect(&languageBox, &QComboBox::currentTextChanged, this, &Window::saveLanguage);
layout.addRow(TRANSLATE_TO, &languageBox);
auto languageBox = new QComboBox(this);
languageBox->addItems(languages);
int language = -1;
if (settings.contains(LANGUAGE)) language = languageBox->findText(settings.value(LANGUAGE).toString(), Qt::MatchEndsWith);
if (language < 0) language = languageBox->findText(NATIVE_LANGUAGE, Qt::MatchStartsWith);
if (language < 0) language = languageBox->findText("English", Qt::MatchStartsWith);
languageBox->setCurrentIndex(language);
saveLanguage(languageBox->currentText());
connect(languageBox, &QComboBox::currentTextChanged, this, &Window::saveLanguage);
display->addRow(TRANSLATE_TO, languageBox);
setWindowTitle(TRANSLATION_PROVIDER);
QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection);
@ -80,10 +78,6 @@ private:
{
settings.setValue(LANGUAGE, S(translateTo->assign(S(language.split(": ")[1]))));
}
QFormLayout layout{ this };
QComboBox languageBox{ this };
QSettings settings{ openSettings(this) };
} window;
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)