1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-17 20:58:07 +02:00

allow translations in chat

This commit is contained in:
Laserlicht 2024-05-01 15:00:36 +02:00
parent 426a008506
commit 89b750c749
4 changed files with 38 additions and 9 deletions

View File

@ -260,6 +260,8 @@
"vcmi.battleResultsWindow.applyResultsLabel" : "Accept battle result?",
"vcmi.chat.versus" : "vs.",
"vcmi.tutorialWindow.title" : "Touchscreen Introduction",
"vcmi.tutorialWindow.decription.RightClick" : "Touch and hold the element on which you want to right-click. Touch the free area to close.",
"vcmi.tutorialWindow.decription.MapPanning" : "Touch and drag with one finger to move the map.",

View File

@ -25,6 +25,8 @@
#include "../lib/mapObjects/CArmedInstance.h"
#include "../lib/CConfigHandler.h"
#include "../lib/MetaString.h"
#include "../lib/VCMI_Lib.h"
#include "../lib/CGeneralTextHandler.h"
const std::vector<GameChatMessage> & GameChatHandler::getChatHistory() const
{
@ -61,6 +63,8 @@ void GameChatHandler::onNewLobbyMessageReceived(const std::string & senderName,
auto * lobby = dynamic_cast<CLobbyScreen*>(SEL);
std::string messageTextReplaced = translationReplace(messageText);
// FIXME: when can this happen?
assert(lobby);
assert(lobby->card);
@ -70,14 +74,14 @@ void GameChatHandler::onNewLobbyMessageReceived(const std::string & senderName,
MetaString formatted = MetaString::createFromRawString("[%s] %s: %s");
formatted.replaceRawString(TextOperations::getCurrentFormattedTimeLocal());
formatted.replaceRawString(senderName);
formatted.replaceRawString(messageText);
formatted.replaceRawString(messageTextReplaced);
lobby->card->chat->addNewMessage(formatted.toString());
if (!lobby->card->showChat)
lobby->toggleChat();
}
chatHistory.push_back({senderName, messageText, TextOperations::getCurrentFormattedTimeLocal()});
chatHistory.push_back({senderName, messageTextReplaced, TextOperations::getCurrentFormattedTimeLocal()});
}
void GameChatHandler::onNewGameMessageReceived(PlayerColor sender, const std::string & messageText)
@ -86,22 +90,43 @@ void GameChatHandler::onNewGameMessageReceived(PlayerColor sender, const std::st
std::string timeFormatted = TextOperations::getCurrentFormattedTimeLocal();
std::string playerName = "<UNKNOWN>";
std::string messageTextReplaced = translationReplace(messageText);
if (sender.isValidPlayer())
playerName = LOCPLINT->cb->getStartInfo()->playerInfos.at(sender).name;
if (sender.isSpectator())
playerName = "Spectator"; // FIXME: translate? Provide nickname somewhere?
chatHistory.push_back({playerName, messageText, timeFormatted});
chatHistory.push_back({playerName, messageTextReplaced, timeFormatted});
LOCPLINT->cingconsole->addMessage(timeFormatted, playerName, messageText);
LOCPLINT->cingconsole->addMessage(timeFormatted, playerName, messageTextReplaced);
}
void GameChatHandler::onNewSystemMessageReceived(const std::string & messageText)
{
chatHistory.push_back({"System", messageText, TextOperations::getCurrentFormattedTimeLocal()});
std::string messageTextReplaced = translationReplace(messageText);
chatHistory.push_back({"System", messageTextReplaced, TextOperations::getCurrentFormattedTimeLocal()});
if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
LOCPLINT->cingconsole->addMessage(TextOperations::getCurrentFormattedTimeLocal(), "System", messageText);
LOCPLINT->cingconsole->addMessage(TextOperations::getCurrentFormattedTimeLocal(), "System", messageTextReplaced);
}
std::string GameChatHandler::translationReplace(std::string txt)
{
std::regex expr("~~([\\w\\d\\.]+)~~");
std::string result = "";
std::string tmp_suffix = "";
std::smatch match;
std::string::const_iterator searchStart( txt.cbegin() );
while(std::regex_search(searchStart, txt.cend(), match, expr) )
{
result += match.prefix();
result += VLC->generaltexth->translate(match.str(1));
searchStart = match.suffix().first;
tmp_suffix = match.suffix();
}
return result.empty() ? txt : result + tmp_suffix;
}

View File

@ -23,6 +23,8 @@ struct GameChatMessage
class GameChatHandler : boost::noncopyable
{
std::vector<GameChatMessage> chatHistory;
std::string translationReplace(std::string txt);
public:
/// Returns all message history for current match
const std::vector<GameChatMessage> & getChatHistory() const;

View File

@ -389,15 +389,15 @@ void ApplyOnServerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
switch(pack.action) {
case LobbyPvPAction::COIN:
srv.announceTxt("Coin - " + std::to_string(std::rand()%2));
srv.announceTxt("~~vcmi.lobby.pvp.coin.hover~~ - " + std::to_string(std::rand()%2));
break;
case LobbyPvPAction::RANDOM_TOWN:
if(allowedTowns.size())
srv.announceTxt("Town - " + VLC->townh->getById(randomFaction1[0])->getNameTranslated());
srv.announceTxt("~~core.overview.3~~ - ~~" + VLC->townh->getById(randomFaction1[0])->getNameTextID() + "~~");
break;
case LobbyPvPAction::RANDOM_TOWN_VS:
if(allowedTowns.size())
srv.announceTxt("Towns - " + VLC->townh->getById(randomFaction1[0])->getNameTranslated() + " vs. " + VLC->townh->getById(randomFaction2[0])->getNameTranslated());
srv.announceTxt("~~core.overview.3~~ - ~~" + VLC->townh->getById(randomFaction1[0])->getNameTextID() + "~~ ~~vcmi.chat.versus~~ ~~" + VLC->townh->getById(randomFaction2[0])->getNameTextID() + "~~");
break;
}
result = true;