Add support of language files (#1273)

* Basic support of language file
Only mapped main part of MCC.
* Translations function imporve
* Change translation file naming
* Fix default translation file naming
* Complete translation file mapping for main part
Command and ChatBot not done yet
* Complete translation mapping for commands
Except Entitycmd
* Complete translation mapping for ChatBots
* Add new method for replacing translation key
Just for Entitycmd. Be proud of yourself. We have a convenient method now.
* Complete all translation mapping
* Add default config and translation file to resource
* Remove untranslatable messages from default translation file
This commit is contained in:
ReinforceZwei 2020-10-17 19:41:31 +08:00 committed by GitHub
parent 0c88c18ea0
commit 2017d5d652
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
54 changed files with 1658 additions and 660 deletions

View file

@ -202,7 +202,7 @@ namespace MinecraftClient
client = ProxyHandler.newTcpClient(host, port);
client.ReceiveBufferSize = 1024 * 1024;
handler = Protocol.ProtocolHandler.GetProtocolHandler(client, protocolversion, forgeInfo, this);
Console.WriteLine("Version is supported.\nLogging in...");
Translations.WriteLine("mcc.version_supported");
try
{
@ -211,7 +211,7 @@ namespace MinecraftClient
if (singlecommand)
{
handler.SendChatMessage(command);
ConsoleIO.WriteLineFormatted("§7Command §8" + command + "§7 sent.");
ConsoleIO.WriteLineFormatted(Translations.Get("mcc.single_cmd", command));
Thread.Sleep(5000);
handler.Disconnect();
Thread.Sleep(1000);
@ -222,9 +222,7 @@ namespace MinecraftClient
BotLoad(bot, false);
botsOnHold.Clear();
Console.WriteLine("Server was successfully joined.\nType '"
+ (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar)
+ "quit' to leave the server.");
Translations.WriteLine("mcc.joined", (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar));
cmdprompt = new Thread(new ThreadStart(CommandPrompt));
cmdprompt.Name = "MCC Command prompt";
@ -237,21 +235,21 @@ namespace MinecraftClient
}
else
{
Console.WriteLine("Failed to login to this server.");
Translations.WriteLine("error.login_failed");
retry = true;
}
}
catch (Exception e)
{
ConsoleIO.WriteLineFormatted("§8" + e.GetType().Name + ": " + e.Message);
Console.WriteLine("Failed to join this server.");
Translations.WriteLine("error.join");
retry = true;
}
}
catch (SocketException e)
{
ConsoleIO.WriteLineFormatted("§8" + e.Message);
Console.WriteLine("Failed to connect to this IP.");
Translations.WriteLine("error.connect");
retry = true;
}
@ -259,7 +257,7 @@ namespace MinecraftClient
{
if (ReconnectionAttemptsLeft > 0)
{
ConsoleIO.WriteLogLine("Waiting 5 seconds (" + ReconnectionAttemptsLeft + " attempts left)...");
ConsoleIO.WriteLogLine(Translations.Get("mcc.reconnect", ReconnectionAttemptsLeft));
Thread.Sleep(5000);
ReconnectionAttemptsLeft--;
Program.Restart();
@ -340,7 +338,7 @@ namespace MinecraftClient
{
if (lastKeepAlive.AddSeconds(30) < DateTime.Now)
{
OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "Connection Timeout");
OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, Translations.Get("error.timeout"));
return;
}
}
@ -367,15 +365,15 @@ namespace MinecraftClient
string help_cmdname = Command.getArgs(command)[0].ToLower();
if (help_cmdname == "help")
{
response_msg = "help <cmdname>: show brief help about a command.";
response_msg = Translations.Get("icmd.help");
}
else if (cmds.ContainsKey(help_cmdname))
{
response_msg = cmds[help_cmdname].CMDDesc;
response_msg = cmds[help_cmdname].GetCmdDescTranslated();
}
else response_msg = "Unknown command '" + command_name + "'. Use 'help' for command list.";
else response_msg = Translations.Get("icmd.unknown", command_name);
}
else response_msg = "help <cmdname>. Available commands: " + String.Join(", ", cmd_names.ToArray()) + ". For server help, use '" + Settings.internalCmdChar + "send /help' instead.";
else response_msg = Translations.Get("icmd.list", String.Join(", ", cmd_names.ToArray()), Settings.internalCmdChar);
}
else if (cmds.ContainsKey(command_name))
{
@ -390,7 +388,7 @@ namespace MinecraftClient
{
if (!(e is ThreadAbortException))
{
ConsoleIO.WriteLogLine("OnInternalCommand: Got error from " + bot.ToString() + ": " + e.ToString());
ConsoleIO.WriteLogLine(Translations.Get("icmd.error", bot.ToString(), e.ToString()));
}
else throw; //ThreadAbortException should not be caught
}
@ -398,7 +396,7 @@ namespace MinecraftClient
}
else
{
response_msg = "Unknown command '" + command_name + "'. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help.";
response_msg = Translations.Get("icmd.unknown", command_name);
return false;
}
@ -419,8 +417,8 @@ namespace MinecraftClient
try
{
Command cmd = (Command)Activator.CreateInstance(type);
cmds[cmd.CMDName.ToLower()] = cmd;
cmd_names.Add(cmd.CMDName.ToLower());
cmds[cmd.CmdName.ToLower()] = cmd;
cmd_names.Add(cmd.CmdName.ToLower());
foreach (string alias in cmd.getCMDAliases())
cmds[alias.ToLower()] = cmd;
}
@ -439,21 +437,7 @@ namespace MinecraftClient
/// </summary>
public void Disconnect()
{
foreach (ChatBot bot in bots.ToArray())
{
try
{
bot.OnDisconnect(ChatBot.DisconnectReason.UserLogout, "");
}
catch (Exception e)
{
if (!(e is ThreadAbortException))
{
ConsoleIO.WriteLogLine("OnDisconnect: Got error from " + bot.ToString() + ": " + e.ToString());
}
else throw; //ThreadAbortException should not be caught
}
}
DispatchBotEvent(bot => bot.OnDisconnect(ChatBot.DisconnectReason.UserLogout, ""));
botsOnHold.Clear();
botsOnHold.AddRange(bots);
@ -498,22 +482,22 @@ namespace MinecraftClient
switch (reason)
{
case ChatBot.DisconnectReason.ConnectionLost:
message = "Connection has been lost.";
message = Translations.Get("mcc.disconnect.lost");
ConsoleIO.WriteLine(message);
break;
case ChatBot.DisconnectReason.InGameKick:
ConsoleIO.WriteLine("Disconnected by Server :");
Translations.WriteLine("mcc.disconnect.server");
ConsoleIO.WriteLineFormatted(message);
break;
case ChatBot.DisconnectReason.LoginRejected:
ConsoleIO.WriteLine("Login failed :");
ConsoleIO.WriteLine("mcc.disconnect.login");
ConsoleIO.WriteLineFormatted(message);
break;
case ChatBot.DisconnectReason.UserLogout:
throw new InvalidOperationException("User-initiated logout should be done by calling Disconnect()");
throw new InvalidOperationException(Translations.Get("exception.user_logout"));
}
foreach (ChatBot bot in bots.ToArray())
@ -605,7 +589,7 @@ namespace MinecraftClient
/// <param name="cmdDesc">Description/usage of the command</param>
/// <param name="callback">Method for handling the command</param>
/// <returns>True if successfully registered</returns>
public bool RegisterCommand(string cmdName, string cmdDesc, ChatBot.CommandRunner callback)
public bool RegisterCommand(string cmdName, string cmdDesc, string cmdUsage, ChatBot.CommandRunner callback)
{
if (cmds.ContainsKey(cmdName.ToLower()))
{
@ -613,7 +597,7 @@ namespace MinecraftClient
}
else
{
Command cmd = new ChatBot.ChatBotCommand(cmdName, cmdDesc, callback);
Command cmd = new ChatBot.ChatBotCommand(cmdName, cmdDesc, cmdUsage, callback);
cmds.Add(cmdName.ToLower(), cmd);
cmd_names.Add(cmdName.ToLower());
return true;
@ -1550,7 +1534,7 @@ namespace MinecraftClient
{
inventoryHandlingRequested = false;
inventoryHandlingEnabled = true;
ConsoleIO.WriteLogLine("Inventory handling is now enabled.");
Translations.WriteLogLine("extra.inventory_enabled");
}
ClearInventories();
@ -1567,7 +1551,7 @@ namespace MinecraftClient
{
terrainAndMovementsEnabled = true;
terrainAndMovementsRequested = false;
ConsoleIO.WriteLogLine("Terrain and Movements is now enabled.");
Translations.WriteLogLine("extra.terrainandmovement_enabled");
}
if (terrainAndMovementsEnabled)
@ -1665,7 +1649,7 @@ namespace MinecraftClient
case Direction.South:
break;
default:
throw new ArgumentException("Unknown direction", "direction");
throw new ArgumentException(Translations.Get("exception.unknown_direction"), "direction");
}
UpdateLocation(location, yaw, pitch);
@ -1696,7 +1680,7 @@ namespace MinecraftClient
if (Settings.DisplayChatLinks)
foreach (string link in links)
ConsoleIO.WriteLogLine("Link: " + link, false);
ConsoleIO.WriteLogLine(Translations.Get("mcc.link", link), false);
DispatchBotEvent(bot => bot.GetText(text));
DispatchBotEvent(bot => bot.GetText(text, json));
@ -1724,8 +1708,8 @@ namespace MinecraftClient
if (inventoryID != 0)
{
ConsoleIO.WriteLogLine("Inventory # " + inventoryID + " opened: " + inventory.Title);
ConsoleIO.WriteLogLine("Use /inventory to interact with it.");
ConsoleIO.WriteLogLine(Translations.Get("extra.inventory_open", inventoryID, inventory.Title));
Translations.WriteLogLine("extra.inventory_interact");
DispatchBotEvent(bot => bot.OnInventoryOpen(inventoryID));
}
}
@ -1741,7 +1725,7 @@ namespace MinecraftClient
if (inventoryID != 0)
{
ConsoleIO.WriteLogLine("Inventory # " + inventoryID + " closed.");
ConsoleIO.WriteLogLine(Translations.Get("extra.inventory_close", inventoryID));
DispatchBotEvent(bot => bot.OnInventoryClose(inventoryID));
}
}
@ -2073,12 +2057,12 @@ namespace MinecraftClient
{
if (Settings.AutoRespawn)
{
ConsoleIO.WriteLogLine("You are dead. Automatically respawning after 1 second.");
Translations.WriteLogLine("mcc.player_dead_respawn");
respawnTicks = 10;
}
else
{
ConsoleIO.WriteLogLine("You are dead. Type /respawn to respawn.");
Translations.WriteLogLine("mcc.player_dead");
}
DispatchBotEvent(bot => bot.OnDeath());
}