diff --git a/assets/lang/en_US.json b/assets/lang/en_US.json index eebe65cf..87d62447 100644 --- a/assets/lang/en_US.json +++ b/assets/lang/en_US.json @@ -431,6 +431,11 @@ "client.module.autoGG.useCustomMessage.desc": "Send a custom message on game finish instead of \"gg\"", "client.module.autoGG.customMessage.name": "Message", "client.module.autoGG.customMessage.desc": "The custom message to send", + "client.module.hiveTranslate.name": "Hive Translate", + "client.module.hiveTranslate.desc": "Translates chat messages on The Hive to the target language", + "client.module.hiveTranslate.targetLanguage.name": "Target Language", + "client.module.hiveTranslate.targetLanguage.desc": "The language to translate messages into", + "client.module.hiveTranslate.translatedFrom": "Translated from", "client.commands.help.desc": "Shows all commands and descriptions.", "client.commands.eject.ejectMsg.name": "Unloaded the client.", "client.commands.eject.desc": "Removes Latite from the game.", diff --git a/src/client/feature/module/ModuleManager.cpp b/src/client/feature/module/ModuleManager.cpp index be3b8343..df244593 100644 --- a/src/client/feature/module/ModuleManager.cpp +++ b/src/client/feature/module/ModuleManager.cpp @@ -18,6 +18,7 @@ #include "modules/game/TextHotkey.h" #include "modules/game/Freelook.h" #include "modules/game/AutoGG.h" +#include "modules/game/HiveTranslate.h" #include "modules/visual/Fullbright.h" #include "modules/visual/MotionBlur.h" @@ -98,6 +99,7 @@ ModuleManager::ModuleManager() { this->items.push_back(std::make_shared()); this->items.push_back(std::make_shared()); this->items.push_back(std::make_shared()); + this->items.push_back(std::make_shared()); this->items.push_back(std::make_shared()); this->items.push_back(std::make_shared()); diff --git a/src/client/feature/module/modules/game/HiveTranslate.cpp b/src/client/feature/module/modules/game/HiveTranslate.cpp new file mode 100644 index 00000000..fccaaaa8 --- /dev/null +++ b/src/client/feature/module/modules/game/HiveTranslate.cpp @@ -0,0 +1,78 @@ +#include "pch.h" +#include "util/Util.h" +#include "HiveTranslate.h" +#include "client/Latite.h" + +#include +#include +#include +#include +#include +#include + +using namespace winrt; +using namespace winrt::Windows::Web::Http; +using namespace winrt::Windows::Foundation; + +auto urlEncode = [](const std::string& str) { + std::string encoded = str; + for (size_t pos = 0; (pos = encoded.find(' ', pos)) != std::string::npos; pos += 3) + encoded.replace(pos, 1, "%20"); + return encoded; +}; + +HiveTranslate::HiveTranslate() : Module("HiveTranslate", LocalizeString::get("client.module.hiveTranslate.name"), + LocalizeString::get("client.module.hiveTranslate.desc"), GAME, nokeybind) { + addSetting("targetLanguage", LocalizeString::get("client.module.hiveTranslate.targetLanguage.name"), + LocalizeString::get("client.module.hiveTranslate.targetLanguage.desc"), this->targetLanguage); + listen(static_cast(&HiveTranslate::onText)); +} + +void HiveTranslate::onText(Event& evG) { + std::string targetLang = util::WStrToStr(std::get(this->targetLanguage).str); + if(targetLang == "") targetLang = "en"; + + std::string message = reinterpret_cast(evG).getMessage(); + + static const std::regex pattern("\u00c2\u00a77\u00c2\u00a7l\u00c2\u00bb \u00c2\u00a7r"); + + std::smatch match; + if (!std::regex_search(message, match, pattern)) return; + + std::string query = urlEncode(message.substr(match.position() + match.length())); + std::string url = "https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=auto&tl=" + targetLang + "&q=" + query; + + try { + winrt::Windows::Foundation::Uri requestUri(std::wstring(url.begin(), url.end())); + HttpRequestMessage request(HttpMethod::Get(), requestUri); + + auto http = HttpClient(); + + http.SendRequestAsync(request).Completed([this, targetLang](const auto& op, auto status) { + try { + if (status != AsyncStatus::Completed) return; + + auto response = op.GetResults(); + + if ((int)response.StatusCode() != 200) return; + + auto buffer = response.Content().ReadAsBufferAsync().get(); + std::string responseBody(reinterpret_cast(buffer.data()), buffer.Length()); + auto json = nlohmann::json::parse(responseBody); + + std::string originLang = json[0][1].get(); + if (originLang == targetLang) return; + + std::string formattedMsg = "\u00c2\u00a77" + + util::WStrToStr(LocalizeString::get("client.module.hiveTranslate.translatedFrom")) + + " " + originLang + ": " + json[0][0].get(); + + Latite::get().getClientMessageQueue().push(formattedMsg); + + } + catch (nlohmann::json::exception const&) {} + catch (winrt::hresult_error const&) {} + }); + + } catch (winrt::hresult_error const&) {} +} \ No newline at end of file diff --git a/src/client/feature/module/modules/game/HiveTranslate.h b/src/client/feature/module/modules/game/HiveTranslate.h new file mode 100644 index 00000000..be0db00c --- /dev/null +++ b/src/client/feature/module/modules/game/HiveTranslate.h @@ -0,0 +1,13 @@ +#pragma once +#include + +class HiveTranslate : public Module { +public: + HiveTranslate(); + virtual ~HiveTranslate() {}; + + void onText(Event&); + +private: + ValueType targetLanguage = TextValue(L"en"); +}; \ No newline at end of file