From f162ce3a1cd7b4c900a92c322b5e3f99e08137f8 Mon Sep 17 00:00:00 2001 From: Half_nothing Date: Wed, 24 Apr 2024 16:26:43 +0800 Subject: [PATCH] feat(I18n): Add i18n for fmxl file * Add i18n for fmxl file (only for test) --- .../java/cn/harryh/arkpets/utils/Logger.java | 24 +++--- core/src/main/resources/lang/I18N.properties | 4 + .../main/resources/lang/I18N_zh_CN.properties | 4 + .../main/resources/lang/I18N_zh_TW.properties | 4 + .../cn/harryh/arkpets/guitasks/ZipTask.java | 4 +- .../cn/harryh/arkpets/utils/FXMLHelper.java | 3 +- .../cn/harryh/arkpets/utils/NetUtils.java | 82 ++++++++++--------- desktop/src/main/resources/UI/RootModule.fxml | 10 +-- 8 files changed, 76 insertions(+), 59 deletions(-) diff --git a/core/src/main/java/cn/harryh/arkpets/utils/Logger.java b/core/src/main/java/cn/harryh/arkpets/utils/Logger.java index 5430410a..6949849c 100644 --- a/core/src/main/java/cn/harryh/arkpets/utils/Logger.java +++ b/core/src/main/java/cn/harryh/arkpets/utils/Logger.java @@ -28,8 +28,8 @@ public class Logger { protected static String logFilePath = null; public static final int ERROR = 40000; - public static final int WARN = 30000; - public static final int INFO = 20000; + public static final int WARN = 30000; + public static final int INFO = 20000; public static final int DEBUG = 10000; protected static final LevelRangeFilter sysOutFilter = new LevelRangeFilter(); @@ -138,38 +138,38 @@ public static Level getLevel() { /** Logs a message with the level {@code DEBUG}. */ - public static void debug(String tag, String message) { + public static void debug(String tag, String message, Object... args) { if (isFileLoggerAvailable) - currentLogger.debug(combine(tag, message)); + currentLogger.debug(combine(tag, message.formatted(args))); } /** Logs a message with the level {@code INFO}. */ - public static void info(String tag, String message) { + public static void info(String tag, String message, Object... args) { if (isFileLoggerAvailable) - currentLogger.info(combine(tag, message)); + currentLogger.info(combine(tag, message.formatted(args))); } /** Logs a message with the level {@code WARN}. */ - public static void warn(String tag, String message) { + public static void warn(String tag, String message, Object... args) { if (isFileLoggerAvailable) - currentLogger.warn(combine(tag, message)); + currentLogger.warn(combine(tag, message.formatted(args))); } /** Logs a message with the level {@code ERROR}. */ - public static void error(String tag, String message) { + public static void error(String tag, String message, Object... args) { if (isFileLoggerAvailable) - currentLogger.error(combine(tag, message)); + currentLogger.error(combine(tag, message.formatted(args))); } /** Logs a message with the level {@code ERROR}, * together with the detailed information (such as stacktrace). */ - public static void error(String tag, String message, Throwable error) { + public static void error(String tag, String message, Throwable error, Object... args) { if (isFileLoggerAvailable) - currentLogger.error(combine(tag, message), error); + currentLogger.error(combine(tag, message.formatted(args)), error); } protected static String combine(String tag, String message) { diff --git a/core/src/main/resources/lang/I18N.properties b/core/src/main/resources/lang/I18N.properties index b1ea04fa..69473116 100644 --- a/core/src/main/resources/lang/I18N.properties +++ b/core/src/main/resources/lang/I18N.properties @@ -1,4 +1,8 @@ app.title = Ark Pet +app.model = Model +app.action = Action +app.option = Option +app.launch = Run button.cancel = Cancel button.confirm = Confirm diff --git a/core/src/main/resources/lang/I18N_zh_CN.properties b/core/src/main/resources/lang/I18N_zh_CN.properties index 2329eb15..b6b7e328 100644 --- a/core/src/main/resources/lang/I18N_zh_CN.properties +++ b/core/src/main/resources/lang/I18N_zh_CN.properties @@ -1,4 +1,8 @@ app.title = Ark Pet +app.model = \u6A21\u578B +app.action = \u884C\u4E3A +app.option = \u9009\u9879 +app.launch = \u542F\u52A8 button.cancel = \u53D6 \u6D88 button.confirm = \u786E \u8BA4 diff --git a/core/src/main/resources/lang/I18N_zh_TW.properties b/core/src/main/resources/lang/I18N_zh_TW.properties index 2b9c87a5..65a61166 100644 --- a/core/src/main/resources/lang/I18N_zh_TW.properties +++ b/core/src/main/resources/lang/I18N_zh_TW.properties @@ -1,4 +1,8 @@ app.title = Ark Pet +app.model = \u6A21\u578B +app.action = \u884C\u70BA +app.option = \u9078\u9805 +app.launch = \u555F\u52D5 button.cancel = \u53D6 \u6D88 button.confirm = \u78BA \u8A8D diff --git a/desktop/src/main/java/cn/harryh/arkpets/guitasks/ZipTask.java b/desktop/src/main/java/cn/harryh/arkpets/guitasks/ZipTask.java index b246841d..650ab858 100644 --- a/desktop/src/main/java/cn/harryh/arkpets/guitasks/ZipTask.java +++ b/desktop/src/main/java/cn/harryh/arkpets/guitasks/ZipTask.java @@ -51,10 +51,10 @@ protected Task getTask() { return new Task<>() { @Override protected Boolean call() throws Exception { - Logger.info("Zip", "Zipping " + contents.size() + " entries into " + zipPath); + Logger.info("Zip", "Zipping %d entries into %s", contents.size(), zipPath); IOUtils.FileUtil.delete(new File(zipPath), false); IOUtils.ZipUtil.zip(zipPath, contents, false); - Logger.info("Zip", "Zipped into " + zipPath + " , finished"); + Logger.info("Zip", "Zipped into %s, finished"); return this.isDone() && !this.isCancelled(); } }; diff --git a/desktop/src/main/java/cn/harryh/arkpets/utils/FXMLHelper.java b/desktop/src/main/java/cn/harryh/arkpets/utils/FXMLHelper.java index 18f5563e..a22aa361 100644 --- a/desktop/src/main/java/cn/harryh/arkpets/utils/FXMLHelper.java +++ b/desktop/src/main/java/cn/harryh/arkpets/utils/FXMLHelper.java @@ -4,6 +4,7 @@ package cn.harryh.arkpets.utils; import cn.harryh.arkpets.controllers.Controller; +import cn.harryh.arkpets.i18n.I18n; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Node; @@ -28,7 +29,7 @@ private FXMLHelper() { */ public static LoadFXMLResult loadFXML( URL location) throws IOException { - FXMLLoader fxml = new FXMLLoader(location); + FXMLLoader fxml = new FXMLLoader(location, I18n.getResourceBundle()); Node content = Objects.requireNonNull(fxml.load()); Controller controller = Objects.requireNonNull(fxml.getController()); return new LoadFXMLResult<>(controller, content); diff --git a/desktop/src/main/java/cn/harryh/arkpets/utils/NetUtils.java b/desktop/src/main/java/cn/harryh/arkpets/utils/NetUtils.java index d34f6220..095408ad 100644 --- a/desktop/src/main/java/cn/harryh/arkpets/utils/NetUtils.java +++ b/desktop/src/main/java/cn/harryh/arkpets/utils/NetUtils.java @@ -4,7 +4,7 @@ package cn.harryh.arkpets.utils; import javax.net.ssl.*; -import java.awt.Desktop; +import java.awt.*; import java.io.IOException; import java.net.*; import java.security.KeyManagementException; @@ -12,7 +12,8 @@ import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.text.DecimalFormat; -import java.util.*; +import java.util.List; +import java.util.Map; public class NetUtils { @@ -21,26 +22,22 @@ public class NetUtils { private static final int delayUpThreshold = 1500; private static final DecimalFormat df = new DecimalFormat("0.0"); - public static final ArrayList ghSources; - static { - ghSources = new ArrayList<>(); - ghSources.add(new GitHubSource("GitHub", - "https://raw.githubusercontent.com/", - "https://github.com/")); - ghSources.add(new GitHubSource("GHProxy", - "https://ghproxy.harryh.cn/https://raw.githubusercontent.com/", - "https://ghproxy.harryh.cn/https://github.com/")); - } - - public static final Map sizeMap; - static { - sizeMap = new HashMap<>(); - sizeMap.put(1L, "B"); - sizeMap.put((long)k, "KB"); - sizeMap.put((long)k * k, "MB"); - sizeMap.put((long)k * k * k, "GB"); - sizeMap.put((long)k * k * k * k, "TB"); - } + public static final List ghSources = List.of( + new GitHubSource("GitHub", + "https://raw.githubusercontent.com/", + "https://github.com/"), + new GitHubSource("GHProxy", + "https://ghproxy.harryh.cn/https://raw.githubusercontent.com/", + "https://ghproxy.harryh.cn/https://github.com/") + ); + + public static final Map sizeMap = Map.of( + 1L, "B", + (long) k, "KB", + (long) k * k, "MB", + (long) k * k * k, "GB", + (long) k * k * k * k, "TB" + ); /** Gets a formatted size string, e.g."{@code 114.5 MB}". * @param byteSize The size value in Byte. @@ -86,7 +83,7 @@ public static int testDelay(String url, int port, int timeoutMillis) { long start = System.currentTimeMillis(); socket.connect(address, timeoutMillis); long stop = System.currentTimeMillis(); - delayMillis = (int)(stop - start); + delayMillis = (int) (stop - start); } catch (IOException ignored) { } try { @@ -108,7 +105,7 @@ public static HttpsURLConnection createHttpsConnection(URL url, int connectTimeo throws IOException { HttpsURLConnection connection = null; try { - connection = (HttpsURLConnection)url.openConnection(); + connection = (HttpsURLConnection) url.openConnection(); if (trustAll) { connection.setSSLSocketFactory(getTrustAnySSLSocketFactory()); connection.setHostnameVerifier(getTrustAnyHostnameVerifier()); @@ -245,20 +242,27 @@ public static class HttpResponseCode { public HttpResponseCode(int code, String message) { this.code = code; this.message = message; - NetUtils.HttpResponseCodeType type; - if (100 <= code && code < 200) - type = NetUtils.HttpResponseCodeType.INFORMATION; - else if (200 <= code && code < 300) - type = NetUtils.HttpResponseCodeType.SUCCESS; - else if (300 <= code && code < 400) - type = NetUtils.HttpResponseCodeType.REDIRECTION; - else if (400 <= code && code < 500) - type = NetUtils.HttpResponseCodeType.CLIENT_ERROR; - else if (500 <= code && code < 600) - type = NetUtils.HttpResponseCodeType.SERVER_ERROR; - else - type = NetUtils.HttpResponseCodeType.UNKNOWN; - this.type = type; + // 100 <= code <= 599 + switch (code / 100) { + case 1: // 100 - 199 + this.type = HttpResponseCodeType.INFORMATION; + break; + case 2: // 200 - 299 + this.type = HttpResponseCodeType.SUCCESS; + break; + case 3: // 300 - 399 + this.type = HttpResponseCodeType.REDIRECTION; + break; + case 4: // 400 - 499 + this.type = HttpResponseCodeType.CLIENT_ERROR; + break; + case 5: // 500 - 599 + this.type = HttpResponseCodeType.SERVER_ERROR; + break; + default: // other + this.type = HttpResponseCodeType.UNKNOWN; + break; + } } public HttpResponseCode(HttpURLConnection connection) @@ -297,7 +301,7 @@ public static class Source { public long lastErrorTime = -1; public Source(String tag, String preUrl) { - this.tag= tag; + this.tag = tag; this.preUrl = preUrl; } diff --git a/desktop/src/main/resources/UI/RootModule.fxml b/desktop/src/main/resources/UI/RootModule.fxml index 6d1def65..bb02c8f5 100644 --- a/desktop/src/main/resources/UI/RootModule.fxml +++ b/desktop/src/main/resources/UI/RootModule.fxml @@ -27,7 +27,7 @@ - @@ -41,7 +41,7 @@ + styleClass="menu-btn" text="%app.model" textAlignment="CENTER"> @@ -54,7 +54,7 @@ + styleClass="menu-btn" text="%app.action" textAlignment="CENTER" GridPane.rowIndex="1"> @@ -67,7 +67,7 @@ + styleClass="menu-btn" text="%app.option" textAlignment="CENTER" GridPane.rowIndex="2"> @@ -81,7 +81,7 @@ + prefHeight="36.0" prefWidth="90.0" text="%app.launch" textAlignment="CENTER">