Skip to content

Commit

Permalink
feat(I18n): Add i18n for fmxl file
Browse files Browse the repository at this point in the history
* Add i18n for fmxl file (only for test)
  • Loading branch information
half-nothing committed Apr 24, 2024
1 parent b025835 commit f162ce3
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 59 deletions.
24 changes: 12 additions & 12 deletions core/src/main/java/cn/harryh/arkpets/utils/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/resources/lang/I18N.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/resources/lang/I18N_zh_CN.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/resources/lang/I18N_zh_TW.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions desktop/src/main/java/cn/harryh/arkpets/guitasks/ZipTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ protected Task<Boolean> 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();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +29,7 @@ private FXMLHelper() {
*/
public static <T extends Application> LoadFXMLResult<T> loadFXML( URL location)
throws IOException {
FXMLLoader fxml = new FXMLLoader(location);
FXMLLoader fxml = new FXMLLoader(location, I18n.getResourceBundle());
Node content = Objects.requireNonNull(fxml.load());
Controller<T> controller = Objects.requireNonNull(fxml.getController());
return new LoadFXMLResult<>(controller, content);
Expand Down
82 changes: 43 additions & 39 deletions desktop/src/main/java/cn/harryh/arkpets/utils/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
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;
import java.security.NoSuchAlgorithmException;
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 {
Expand All @@ -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<Source> 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<Long, String> 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<Source> 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<Long, String> 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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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());
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions desktop/src/main/resources/UI/RootModule.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<Pane id="Sidebar" fx:id="sidebar" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="376.0" prefWidth="140.0" styleClass="shadowed"
AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" StackPane.alignment="TOP_LEFT">
<Text id="Title" layoutX="12.0" layoutY="55.0" text="ArkPets" textAlignment="CENTER"
<Text id="Title" layoutX="12.0" layoutY="55.0" text="%app.title" textAlignment="CENTER"
wrappingWidth="117.0">
</Text>
<Line endX="128.0" layoutY="66.0" startX="12.0" stroke="#0000009e"/>
Expand All @@ -41,7 +41,7 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
</rowConstraints>
<JFXButton fx:id="menuBtn1" mnemonicParsing="false" prefHeight="40.0" prefWidth="140.0"
styleClass="menu-btn" text="模型" textAlignment="CENTER">
styleClass="menu-btn" text="%app.model" textAlignment="CENTER">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</GridPane.margin>
Expand All @@ -54,7 +54,7 @@
</graphic>
</JFXButton>
<JFXButton fx:id="menuBtn2" mnemonicParsing="false" prefHeight="40.0" prefWidth="140.0"
styleClass="menu-btn" text="行为" textAlignment="CENTER" GridPane.rowIndex="1">
styleClass="menu-btn" text="%app.action" textAlignment="CENTER" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</GridPane.margin>
Expand All @@ -67,7 +67,7 @@
</graphic>
</JFXButton>
<JFXButton fx:id="menuBtn3" mnemonicParsing="false" prefHeight="40.0" prefWidth="140.0"
styleClass="menu-btn" text="选项" textAlignment="CENTER" GridPane.rowIndex="2">
styleClass="menu-btn" text="%app.option" textAlignment="CENTER" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</GridPane.margin>
Expand All @@ -81,7 +81,7 @@
</JFXButton>
</GridPane>
<JFXButton id="Launch-btn" fx:id="launchBtn" layoutX="25.0" layoutY="310.0" mnemonicParsing="false"
prefHeight="36.0" prefWidth="90.0" text="启动" textAlignment="CENTER">
prefHeight="36.0" prefWidth="90.0" text="%app.launch" textAlignment="CENTER">
<graphic>
<AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="24.0" prefWidth="2.0"
translateX="-20.0" translateY="1.0">
Expand Down

0 comments on commit f162ce3

Please sign in to comment.