forked from ddnet/ddnet
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8b6d41
commit fa3bab0
Showing
6 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <game/client/gameclient.h> | ||
#include <game/client/lineinput.h> | ||
#include <engine/shared/json.h> | ||
|
||
#include "translate.h" | ||
|
||
CTranslate::CTranslate() | ||
{ | ||
OnReset(); | ||
} | ||
|
||
void CTranslate::ConTranslate(IConsole::IResult *pResult, void *pUserData) | ||
{ | ||
const char *pName; | ||
if (pResult->NumArguments() == 0) | ||
pName = nullptr; | ||
else | ||
pName = pResult->GetString(0); | ||
|
||
CTranslate *pThis = static_cast<CTranslate *>(pUserData); | ||
pThis->Translate(pName); | ||
|
||
} | ||
|
||
void CTranslate::OnConsoleInit() | ||
{ | ||
Console()->Register("translate", "?r[name]", CFGFLAG_CLIENT, ConTranslate, this, "Translate last message (of a given name)"); | ||
} | ||
|
||
void CTranslate::TranslateLibretranslate(const CChat::CLine *pLine) | ||
{ | ||
static const char FORMAT[] = "{\"q\":\"%s\",\"source\":\"auto\",\"target\":\"%s\",\"format\":\"text\",\"alternatives\":0,\"api-key\":\"%s\"}"; | ||
char aBuf[sizeof(FORMAT) + sizeof(CChat::CLine::m_aText) + 256]; | ||
str_format(aBuf, sizeof(aBuf), FORMAT, pLine->m_aText, "en", "30fc3646-c79e-485f-8db3-74a7c7a52729"); | ||
|
||
std::shared_ptr<CHttpRequest> pGet = HttpGet("https://icosrv.sollybunny.xyz:1623"); | ||
pGet->Timeout(CTimeout{10000, 0, 500, 10}); | ||
|
||
Http()->Run(pGet); | ||
|
||
unsigned char *outBuf; | ||
size_t outBufSize; | ||
pGet->Result(&outBuf, &outBufSize); | ||
printf("%s\n", outBuf); | ||
} | ||
|
||
const CChat::CLine *CTranslate::FindMessage(const char *pName) | ||
{ | ||
// No messages at all | ||
if(GameClient()->m_Chat.m_CurrentLine < 0) | ||
return nullptr; | ||
if(!pName) | ||
return &GameClient()->m_Chat.m_aLines[GameClient()->m_Chat.m_CurrentLine]; | ||
for(int i = GameClient()->m_Chat.m_CurrentLine; i >= 0; --i) | ||
{ | ||
if(str_comp(GameClient()->m_Chat.m_aLines[i].m_aName, pName) == 0) | ||
return &GameClient()->m_Chat.m_aLines[i]; | ||
} | ||
for(int i = GameClient()->m_Chat.m_CurrentLine; i >= 0; --i) | ||
{ | ||
if(str_comp_nocase(GameClient()->m_Chat.m_aLines[i].m_aName, pName) == 0) | ||
return &GameClient()->m_Chat.m_aLines[i]; | ||
} | ||
return nullptr; | ||
|
||
} | ||
|
||
void CTranslate::Translate(const char *pName) | ||
{ | ||
const CChat::CLine *pLine = FindMessage(pName); | ||
if(!pLine) | ||
{ | ||
GameClient()->m_Chat.Echo("No message to translate"); | ||
return; | ||
} | ||
|
||
TranslateLibretranslate(pLine); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef GAME_CLIENT_COMPONENTS_TRANSLATE_H | ||
#define GAME_CLIENT_COMPONENTS_TRANSLATE_H | ||
#include <game/client/component.h> | ||
|
||
class CTranslate : public CComponent | ||
{ | ||
static void ConTranslate(IConsole::IResult *pResult, void *pUserData); | ||
|
||
void TranslateLibretranslate(const CChat::CLine *pLine); | ||
|
||
const CChat::CLine *FindMessage(const char *pName); | ||
void Translate(const char *pName); | ||
|
||
public: | ||
CTranslate(); | ||
virtual int Sizeof() const override { return sizeof(*this); } | ||
|
||
virtual void OnConsoleInit() override; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters