Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assets/lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 2 additions & 0 deletions src/client/feature/module/ModuleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -98,6 +99,7 @@ ModuleManager::ModuleManager() {
this->items.push_back(std::make_shared<CustomCoordinates>());
this->items.push_back(std::make_shared<MovableCoordinates>());
this->items.push_back(std::make_shared<AutoGG>());
this->items.push_back(std::make_shared<HiveTranslate>());
this->items.push_back(std::make_shared<FrameTimeDisplay>());
this->items.push_back(std::make_shared<BlockGame>());

Expand Down
78 changes: 78 additions & 0 deletions src/client/feature/module/modules/game/HiveTranslate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "pch.h"
#include "util/Util.h"
#include "HiveTranslate.h"
#include "client/Latite.h"

#include <regex>
#include <winrt/base.h>
#include <nlohmann/json.hpp>
#include <winrt/Windows.Web.Http.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/windows.storage.streams.h>

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<ChatMessageEvent>(static_cast<EventListenerFunc>(&HiveTranslate::onText));
}

void HiveTranslate::onText(Event& evG) {
std::string targetLang = util::WStrToStr(std::get<TextValue>(this->targetLanguage).str);
if(targetLang == "") targetLang = "en";

std::string message = reinterpret_cast<ChatMessageEvent&>(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<char*>(buffer.data()), buffer.Length());
auto json = nlohmann::json::parse(responseBody);

std::string originLang = json[0][1].get<std::string>();
if (originLang == targetLang) return;

std::string formattedMsg = "\u00c2\u00a77" +
util::WStrToStr(LocalizeString::get("client.module.hiveTranslate.translatedFrom")) +
" " + originLang + ": " + json[0][0].get<std::string>();

Latite::get().getClientMessageQueue().push(formattedMsg);

}
catch (nlohmann::json::exception const&) {}
catch (winrt::hresult_error const&) {}
});

} catch (winrt::hresult_error const&) {}
}
13 changes: 13 additions & 0 deletions src/client/feature/module/modules/game/HiveTranslate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include <client/feature/module/Module.h>

class HiveTranslate : public Module {
public:
HiveTranslate();
virtual ~HiveTranslate() {};

void onText(Event&);

private:
ValueType targetLanguage = TextValue(L"en");
};
Loading