From aa6edfd0f01ea0ff9f0d530e8d8e392f9a679b70 Mon Sep 17 00:00:00 2001 From: Carlos <28845529+mesacarlos@users.noreply.github.com> Date: Wed, 28 Aug 2019 02:32:06 +0200 Subject: [PATCH] Added Chinese to client and Spanish to server --- client/index.html | 14 +++--- client/scripts/WebConsoleJqueryHandler.js | 2 +- client/scripts/WebConsoleLanguage.js | 1 - phrases.properties | 33 ++++++++++++++ phrases_en.properties | 33 ++++++++++++++ phrases_es.properties | 33 ++++++++++++++ plugin.yml | 2 +- pom.xml | 3 +- simplelogger.properties | 7 --- src/com/mesacarlos/webconsole/WebConsole.java | 9 +++- .../webconsole/util/Internationalization.java | 35 +++++++++++++++ .../webconsole/websocket/WSServer.java | 43 +++++++++---------- .../websocket/command/CpuUsageCommand.java | 3 +- .../websocket/command/ExecCommand.java | 3 +- .../websocket/command/LogInCommand.java | 11 ++--- .../websocket/command/PlayersCommand.java | 12 +++++- .../websocket/command/RamUsageCommand.java | 8 +++- 17 files changed, 201 insertions(+), 51 deletions(-) create mode 100644 phrases.properties create mode 100644 phrases_en.properties create mode 100644 phrases_es.properties delete mode 100644 simplelogger.properties create mode 100644 src/com/mesacarlos/webconsole/util/Internationalization.java diff --git a/client/index.html b/client/index.html index b0cc703..894af76 100644 --- a/client/index.html +++ b/client/index.html @@ -211,7 +211,7 @@ @@ -221,11 +221,11 @@ - - - - - - + + + + + + diff --git a/client/scripts/WebConsoleJqueryHandler.js b/client/scripts/WebConsoleJqueryHandler.js index db14d3c..b10463c 100644 --- a/client/scripts/WebConsoleJqueryHandler.js +++ b/client/scripts/WebConsoleJqueryHandler.js @@ -9,8 +9,8 @@ */ $(document).ready(function() { $("#serverContainer").hide(); - updateServerList(); setLanguage(persistenceManager.getLanguage()); + updateServerList(); }); /** diff --git a/client/scripts/WebConsoleLanguage.js b/client/scripts/WebConsoleLanguage.js index 0cd74c6..7e31add 100644 --- a/client/scripts/WebConsoleLanguage.js +++ b/client/scripts/WebConsoleLanguage.js @@ -172,7 +172,6 @@ function setLanguage(locale){ document.getElementById("home_description").textContent = lang.home_description; document.getElementById("serversDropdown").textContent = lang.server_dropdown; document.getElementById("add_server").textContent = lang.add_server; - document.getElementById("noServersAdded").textContent = lang.noServersAdded; document.getElementById("lang_dropdown").textContent = lang.lang_dropdown; document.getElementById("addServerModalLongTitle").textContent = lang.addServerModalLongTitle; document.getElementById("addServerModalSvName").textContent = lang.addServerModalSvName; diff --git a/phrases.properties b/phrases.properties new file mode 100644 index 0000000..c587e0e --- /dev/null +++ b/phrases.properties @@ -0,0 +1,33 @@ +# WebConsole.java +boot-error = Error occured while starting WebSocket Server. + +# WSServer.java +connection-resumed-message = Connected. Already logged in, welcome back! +connection-resumed-console = [WebConsole] Connected and resumed session from {0} +connection-login-message = Connection started, waiting login +connection-login-console = [WebConsole] Connected and waiting login from {0} +unknown-command-message = Unknown command +unknown-command-console = [WebConsole] Signal '{0}' was not processed since is not valid. Is your plugin/web interface up to date? +forbidden-message = Forbidden +forbidden-console = [WebConsole] {0} tried to run '{1}' while not logged in! +closed-connection = [WebConsole] Closed connection and logged out from {0} +error-on-connection = [WebConsole] Error occured on connection {0}: {1} +started-websocket = [WebConsole] WebSocket Server started successfully. + +# CpuUsageCommand.java +cpu-usage-message = "Usage is {0}%" + +# ExecCommand.java +cmd-executed-console = [WebConsole] {0} executed '{1}'. + +#LogInCommand.java +login-sucessful-message = Logged in +login-sucessful-console = [WebConsole] Successfully logged in from {0} +login-failed-message = Incorrect password, try again +login-failed-console = [WebConsole] Password incorrect while login from {0} + +# PlayersCommand.java +players-message = Connected {0} players for a maximum of {1} + +# RamUsageCommand.java +ram-usage-message = {0} free, {1} used, {2} maximum memory \ No newline at end of file diff --git a/phrases_en.properties b/phrases_en.properties new file mode 100644 index 0000000..77d1fff --- /dev/null +++ b/phrases_en.properties @@ -0,0 +1,33 @@ +# WebConsole.java +boot-error = Error occured while starting WebSocket Server. + +# WSServer.java +connection-resumed-message = Connected. Already logged in, welcome back! +connection-resumed-console = [WebConsole] Connected and resumed session from {0} +connection-login-message = Connection started, waiting login +connection-login-console = [WebConsole] Connected and waiting login from {0} +unknown-command-message = Unknown command +unknown-command-console = [WebConsole] Signal '{0}' was not processed since is not valid. Is your plugin/web interface up to date? +forbidden-message = Forbidden +forbidden-console = [WebConsole] {0} tried to run '{1}' while not logged in! +closed-connection = [WebConsole] Closed connection and logged out from {0} +error-on-connection = [WebConsole] Error occured on connection {0}: {1} +started-websocket = [WebConsole] WebSocket Server started successfully. + +# CpuUsageCommand.java +cpu-usage-message = "Usage is {0}%" + +# ExecCommand.java +cmd-executed-console = [WebConsole] {0} executed '{1}'. + +#LogInCommand.java +login-sucessful-message = Logged in +login-sucessful-console = [WebConsole] Successfully logged in from {0} +login-failed-message = Incorrect password, try again +login-failed-console = [WebConsole] Password incorrect while login from {0} + +# PlayersCommand.java +players-message = Actualmente conectados {0} jugadores de un máximo de {1} + +# RamUsageCommand.java +ram-usage-message = {0} free, {1} used, {2} maximum memory \ No newline at end of file diff --git a/phrases_es.properties b/phrases_es.properties new file mode 100644 index 0000000..2332b17 --- /dev/null +++ b/phrases_es.properties @@ -0,0 +1,33 @@ +# WebConsole.java +boot-error = Ocurrió un error al intentar iniciar el servidor WebSocket + +# WSServer.java +connection-resumed-message = Conectado. Ya habías iniciado sesión, bienvenido de vuelta! +connection-resumed-console = [WebConsole] Conectado y resumiendo sesión desde {0} +connection-login-message = Conexión iniciada, esperando login. +connection-login-console = [WebConsole] Conectado y esperando login desde {0} +unknown-command-message = Comando desconocido +unknown-command-console = [WebConsole] La señal '{0}' no se ha procesado porque no es válida. ¿Tienes la ultima versión del plugin y de la interfaz web? +forbidden-message = Prohibido +forbidden-console = [WebConsole] {0} intento ejecutar '{1}' sin tener una sesión iniciada! +closed-connection = [WebConsole] Sesión cerrada y desconectado desde {0} +error-on-connection = [WebConsole] Ocurrió un error en la conexión con {0}: {1} +started-websocket = [WebConsole] Servidor WebSocket iniciado con éxito. + +# CpuUsageCommand.java +cpu-usage-message = "En uso {0}%" + +# ExecCommand.java +cmd-executed-console = [WebConsole] {0} ejecutó '{1}'. + +#LogInCommand.java +login-sucessful-message = Sesión iniciada +login-sucessful-console = [WebConsole] Sesión iniciada con éxito desde {0} +login-failed-message = Contraña incorrecta, inténtalo de nuevo +login-failed-console = [WebConsole] Contraseña incorrecta al intentar iniciar sesión desde {0} + +# PlayersCommand.java +players-message = Actualmente conectados {0} jugadores de un máximo de {1} + +# RamUsageCommand.java +ram-usage-message = Memoria: {0} libre, {1} usada, {2} maxima \ No newline at end of file diff --git a/plugin.yml b/plugin.yml index a57a196..382761b 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,7 +1,7 @@ name: WebConsole main: com.mesacarlos.webconsole.WebConsole api-version: 1.13 -version: 1.3 +version: 1.4 description: WebSockets-based web console author: Carlos Mesa commands: diff --git a/pom.xml b/pom.xml index 97bbb4a..d189ba3 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 WebConsole WebConsole - 1.3 + 1.4 src @@ -38,6 +38,7 @@ . **/*.yml + **/*.properties diff --git a/simplelogger.properties b/simplelogger.properties deleted file mode 100644 index bc9272e..0000000 --- a/simplelogger.properties +++ /dev/null @@ -1,7 +0,0 @@ -org.slf4j.simpleLogger.logFile=System.out -org.slf4j.simpleLogger.defaultLogLevel=off -org.slf4j.simpleLogger.showDateTime=true -org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS -org.slf4j.simpleLogger.showThreadName=false -org.slf4j.simpleLogger.showLogName=false -org.slf4j.simpleLogger.levelInBrackets=true \ No newline at end of file diff --git a/src/com/mesacarlos/webconsole/WebConsole.java b/src/com/mesacarlos/webconsole/WebConsole.java index 71efa8b..cbe9d9b 100644 --- a/src/com/mesacarlos/webconsole/WebConsole.java +++ b/src/com/mesacarlos/webconsole/WebConsole.java @@ -18,6 +18,7 @@ import org.bukkit.plugin.java.JavaPlugin; import org.java_websocket.server.DefaultSSLWebSocketServerFactory; import com.mesacarlos.webconsole.minecraft.WebConsoleCommand; +import com.mesacarlos.webconsole.util.Internationalization; import com.mesacarlos.webconsole.util.LogFilter; import com.mesacarlos.webconsole.websocket.WSServer; @@ -32,11 +33,14 @@ public class WebConsole extends JavaPlugin { public void onEnable() { createConfig(); + //Change language to user-specified language. + Internationalization.setCurrentLocale(config.getString("language")); + //Start WebSocket Server try { startWS(); } catch (Exception e) { - Bukkit.getLogger().warning("Error occured while starting WebSocket Server."); + Bukkit.getLogger().warning(Internationalization.getPhrase("boot-error")); e.printStackTrace(); } @@ -73,6 +77,9 @@ public class WebConsole extends JavaPlugin { config.addDefault("host", "localhost"); config.addDefault("port", 8080); config.addDefault("password", 1234); + + // Language config + config.addDefault("language", "en"); config.options().copyDefaults(true); saveConfig(); diff --git a/src/com/mesacarlos/webconsole/util/Internationalization.java b/src/com/mesacarlos/webconsole/util/Internationalization.java new file mode 100644 index 0000000..3afd79c --- /dev/null +++ b/src/com/mesacarlos/webconsole/util/Internationalization.java @@ -0,0 +1,35 @@ +package com.mesacarlos.webconsole.util; + +import java.text.MessageFormat; +import java.util.Locale; +import java.util.ResourceBundle; + +public class Internationalization { + private static ResourceBundle messages = ResourceBundle.getBundle("phrases", new Locale("en")); + + /** + * Changes locale of this application + * @param locale String representing language + */ + public static void setCurrentLocale(String locale) { + messages = ResourceBundle.getBundle("phrases", new Locale(locale)); + } + + /** + * Get current locale + * @return Current locale + */ + public static Locale getCurrentLocale() { + return messages.getLocale(); + } + + /** + * Gets translated message + * @return The desired message in user's language + */ + public static String getPhrase(String phrase, Object... params) { + String msg = messages.getString(phrase); + msg = MessageFormat.format(msg, params); + return msg; + } +} \ No newline at end of file diff --git a/src/com/mesacarlos/webconsole/websocket/WSServer.java b/src/com/mesacarlos/webconsole/websocket/WSServer.java index 6cbdf10..96305e4 100644 --- a/src/com/mesacarlos/webconsole/websocket/WSServer.java +++ b/src/com/mesacarlos/webconsole/websocket/WSServer.java @@ -10,6 +10,7 @@ import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; import com.mesacarlos.webconsole.WebConsole; +import com.mesacarlos.webconsole.util.Internationalization; import com.mesacarlos.webconsole.util.LoginManager; import com.mesacarlos.webconsole.websocket.command.CommandFactory; import com.mesacarlos.webconsole.websocket.command.WSCommand; @@ -30,11 +31,13 @@ public class WSServer extends WebSocketServer { @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { - if(LoginManager.getInstance().isLoggedIn(conn.getRemoteSocketAddress())) - sendToClient(conn, new Processed("Connected. Already logged in, welcome back!")); - else - sendToClient(conn, new LoginRequired("Connection started, waiting login")); - Bukkit.getLogger().info("[WebConsole] Connected and waiting login from " + conn.getRemoteSocketAddress()); + if (LoginManager.getInstance().isLoggedIn(conn.getRemoteSocketAddress())) { + sendToClient(conn, new Processed(Internationalization.getPhrase("connection-resumed-message"))); + Bukkit.getLogger().info(Internationalization.getPhrase("connection-resumed-console", conn.getRemoteSocketAddress())); + } else { + sendToClient(conn, new LoginRequired(Internationalization.getPhrase("connection-login-message"))); + Bukkit.getLogger().info(Internationalization.getPhrase("connection-login-console", conn.getRemoteSocketAddress())); + } } @Override @@ -49,16 +52,14 @@ public class WSServer extends WebSocketServer { WSCommand cmd = commands.get(wsCommand); if (cmd == null) { - //Command does not exist - sendToClient(conn, new UnknownCommand("Unknown command", message)); - Bukkit.getLogger().info( - "[WebConsole] Signal '" + message + "' was not processed since is not valid. Is your plugin/web interface up to date?"); + // Command does not exist + sendToClient(conn, new UnknownCommand(Internationalization.getPhrase("unknown-command-message"), message)); + Bukkit.getLogger().info(Internationalization.getPhrase("unknown-command-console", message)); } else if (!LoginManager.getInstance().isLoggedIn(conn.getRemoteSocketAddress()) && !wsCommand.equals("LOGIN")) { - //User is not authorised. DO NOTHING, IMPORTANT! - sendToClient(conn, new LoginRequired("Forbidden")); - Bukkit.getLogger().warning("[WebConsole] " + conn.getRemoteSocketAddress() - + " tried to run '" + message + "' while not logged in!"); + // User is not authorised. DO NOTHING, IMPORTANT! + sendToClient(conn, new LoginRequired(Internationalization.getPhrase("forbidden-message"))); + Bukkit.getLogger().warning(Internationalization.getPhrase("forbidden-console", conn.getRemoteSocketAddress(), message)); } else { cmd.execute(this, conn, wsCommandParams); } @@ -67,21 +68,19 @@ public class WSServer extends WebSocketServer { @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { LoginManager.getInstance().logOut(conn.getRemoteSocketAddress()); - Bukkit.getLogger() - .info("[WebConsole] Closed WS connection " + conn.getRemoteSocketAddress()); + Bukkit.getLogger().info(Internationalization.getPhrase("closed-connection", conn.getRemoteSocketAddress())); } @Override public void onError(WebSocket conn, Exception ex) { - Bukkit.getLogger() - .warning("[WebConsole] Error occured on connection " + conn.getRemoteSocketAddress() + ":" + ex); + Bukkit.getLogger().warning(Internationalization.getPhrase("error-on-connection", conn.getRemoteSocketAddress(), ex)); } @Override public void onStart() { - Bukkit.getLogger().info("[WebConsole] WebSocket Server started successfully"); + Bukkit.getLogger().info(Internationalization.getPhrase("started-websocket")); } - + /** * Returns main class * @return Main plugin class @@ -89,7 +88,7 @@ public class WSServer extends WebSocketServer { public WebConsole getMainClass() { return plugin; } - + /** * Sends the message to all connected AND logged-in users */ @@ -100,10 +99,10 @@ public class WSServer extends WebSocketServer { sendToClient(connection, new ConsoleOutput(line)); } } - + /** * Sends this JSONOutput to client - * @param conn Connection to client + * @param conn Connection to client * @param content JSONOutput object */ public void sendToClient(WebSocket conn, JSONOutput content) { diff --git a/src/com/mesacarlos/webconsole/websocket/command/CpuUsageCommand.java b/src/com/mesacarlos/webconsole/websocket/command/CpuUsageCommand.java index a373531..be3a84b 100644 --- a/src/com/mesacarlos/webconsole/websocket/command/CpuUsageCommand.java +++ b/src/com/mesacarlos/webconsole/websocket/command/CpuUsageCommand.java @@ -9,6 +9,7 @@ import javax.management.ObjectName; import org.java_websocket.WebSocket; +import com.mesacarlos.webconsole.util.Internationalization; import com.mesacarlos.webconsole.websocket.WSServer; import com.mesacarlos.webconsole.websocket.response.CpuUsage; @@ -18,7 +19,7 @@ public class CpuUsageCommand implements WSCommand { public void execute(WSServer wsServer, WebSocket conn, String params) { try { double usage = getProcessCpuLoad(); - wsServer.sendToClient(conn, new CpuUsage("Usage is " + usage + "%", usage)); + wsServer.sendToClient(conn, new CpuUsage(Internationalization.getPhrase("cpu-usage-message", usage), usage)); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/com/mesacarlos/webconsole/websocket/command/ExecCommand.java b/src/com/mesacarlos/webconsole/websocket/command/ExecCommand.java index c993c7a..c9947e2 100644 --- a/src/com/mesacarlos/webconsole/websocket/command/ExecCommand.java +++ b/src/com/mesacarlos/webconsole/websocket/command/ExecCommand.java @@ -6,6 +6,7 @@ import org.bukkit.Bukkit; import org.bukkit.command.ConsoleCommandSender; import org.java_websocket.WebSocket; +import com.mesacarlos.webconsole.util.Internationalization; import com.mesacarlos.webconsole.websocket.WSServer; public class ExecCommand implements WSCommand { @@ -13,7 +14,7 @@ public class ExecCommand implements WSCommand { @Override public void execute(WSServer wsServer, WebSocket conn, String command) { - Bukkit.getLogger().info("[WebConsole] " + conn.getRemoteSocketAddress() + " executed '" + command + "'"); + Bukkit.getLogger().info(Internationalization.getPhrase("cmd-executed-console", conn.getRemoteSocketAddress(), command)); ConsoleCommandSender sender = Bukkit.getServer().getConsoleSender(); try { diff --git a/src/com/mesacarlos/webconsole/websocket/command/LogInCommand.java b/src/com/mesacarlos/webconsole/websocket/command/LogInCommand.java index 4933a63..a442f2f 100644 --- a/src/com/mesacarlos/webconsole/websocket/command/LogInCommand.java +++ b/src/com/mesacarlos/webconsole/websocket/command/LogInCommand.java @@ -3,6 +3,7 @@ package com.mesacarlos.webconsole.websocket.command; import org.bukkit.Bukkit; import org.java_websocket.WebSocket; +import com.mesacarlos.webconsole.util.Internationalization; import com.mesacarlos.webconsole.util.LoginManager; import com.mesacarlos.webconsole.websocket.WSServer; import com.mesacarlos.webconsole.websocket.response.LoginRequired; @@ -22,12 +23,12 @@ public class LogInCommand implements WSCommand { if (receivedPassword.equals(password)) { // Password is correct, logging in LoginManager.getInstance().logIn(conn.getRemoteSocketAddress()); - wsServer.sendToClient(conn, new Processed("Logged In", "LOGIN ********")); - Bukkit.getLogger().info("[WebConsole] Successfully logged in from " + conn.getRemoteSocketAddress()); + wsServer.sendToClient(conn, new Processed(Internationalization.getPhrase("login-sucessful-message"), "LOGIN ********")); + Bukkit.getLogger().info(Internationalization.getPhrase("login-sucessful-console", conn.getRemoteSocketAddress())); } else { - wsServer.sendToClient(conn, new LoginRequired("Incorrect password, try again")); - Bukkit.getLogger() - .info("[WebConsole] Password incorrect while login from " + conn.getRemoteSocketAddress()); + // Password was incorrect + wsServer.sendToClient(conn, new LoginRequired(Internationalization.getPhrase("login-failed-message"))); + Bukkit.getLogger().info(Internationalization.getPhrase("login-failed-console", conn.getRemoteSocketAddress())); } } diff --git a/src/com/mesacarlos/webconsole/websocket/command/PlayersCommand.java b/src/com/mesacarlos/webconsole/websocket/command/PlayersCommand.java index 78d3389..1db5312 100644 --- a/src/com/mesacarlos/webconsole/websocket/command/PlayersCommand.java +++ b/src/com/mesacarlos/webconsole/websocket/command/PlayersCommand.java @@ -7,6 +7,7 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.java_websocket.WebSocket; +import com.mesacarlos.webconsole.util.Internationalization; import com.mesacarlos.webconsole.websocket.WSServer; import com.mesacarlos.webconsole.websocket.response.Players; @@ -18,10 +19,17 @@ public class PlayersCommand implements WSCommand{ for(Player player : Bukkit.getOnlinePlayers()) { connectedPlayersList.add(player.getName()); } - int connectedPlayers = Bukkit.getOnlinePlayers().size(); + + int connectedPlayers = connectedPlayersList.size(); int maxPlayers = Bukkit.getMaxPlayers(); - wsServer.sendToClient(conn, new Players("Currently " + connectedPlayers + " connected players for a maximum of " + maxPlayers, connectedPlayers, maxPlayers, connectedPlayersList)); + wsServer.sendToClient(conn, + new Players( + Internationalization.getPhrase("players-message", connectedPlayers, maxPlayers), + connectedPlayers, + maxPlayers, + connectedPlayersList + )); } } \ No newline at end of file diff --git a/src/com/mesacarlos/webconsole/websocket/command/RamUsageCommand.java b/src/com/mesacarlos/webconsole/websocket/command/RamUsageCommand.java index 1901e3d..9cb9629 100644 --- a/src/com/mesacarlos/webconsole/websocket/command/RamUsageCommand.java +++ b/src/com/mesacarlos/webconsole/websocket/command/RamUsageCommand.java @@ -2,6 +2,7 @@ package com.mesacarlos.webconsole.websocket.command; import org.java_websocket.WebSocket; +import com.mesacarlos.webconsole.util.Internationalization; import com.mesacarlos.webconsole.websocket.WSServer; import com.mesacarlos.webconsole.websocket.response.RamUsage; @@ -16,7 +17,12 @@ public class RamUsageCommand implements WSCommand { long used = max - free; wsServer.sendToClient(conn, - new RamUsage(free + " free, " + used + " used, " + max + " maximum memory", free, used, max)); + new RamUsage( + Internationalization.getPhrase("ram-usage-message", free, used, max), + free, + used, + max + )); } } \ No newline at end of file