Skip to content

Commit

Permalink
本地化 BTN 状态响应信息
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jan 3, 2025
1 parent fa7a6e2 commit fc5c68a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
15 changes: 8 additions & 7 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.ghostchu.peerbanhelper.database.dao.impl.PeerRecordDao;
import com.ghostchu.peerbanhelper.scriptengine.ScriptEngine;
import com.ghostchu.peerbanhelper.text.Lang;
import com.ghostchu.peerbanhelper.text.TranslationComponent;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.rule.ModuleMatchCache;
import com.ghostchu.simplereloadlib.ReloadResult;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class BtnNetwork implements Reloadable {
private final Map<Class<? extends BtnAbility>, BtnAbility> abilities = new HashMap<>();
private final ScriptEngine scriptEngine;
private final AtomicBoolean configSuccess = new AtomicBoolean(false);
private String configResult;
private TranslationComponent configResult;
private boolean scriptExecute;
@Getter
private ScheduledExecutorService executeService = null;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void configBtnNetwork() {
HttpResponse<String> resp = HTTPUtil.retryableSend(httpClient, MutableRequest.GET(configUrl), HttpResponse.BodyHandlers.ofString()).join();
if (resp.statusCode() != 200) {
log.error(tlUI(Lang.BTN_CONFIG_FAILS, resp.statusCode() + " - " + resp.body(), 600));
configResult = "Unsuccessful HTTP request to " + configUrl + " with status code " + resp.statusCode()+": "+resp.body();
configResult = new TranslationComponent(Lang.BTN_CONFIG_STATUS_UNSUCCESSFUL_HTTP_REQUEST, configUrl, resp.statusCode(), resp.body());
return;
}
statusCode = resp.statusCode();
Expand All @@ -120,12 +121,12 @@ public void configBtnNetwork() {
}
int min_protocol_version = json.get("min_protocol_version").getAsInt();
if (Main.PBH_BTN_PROTOCOL_IMPL_VERSION < min_protocol_version) {
configResult = "Incompatible protocol version: " + Main.PBH_BTN_PROTOCOL_IMPL_VERSION + " < " + min_protocol_version;
throw new IllegalStateException(tlUI(Lang.BTN_INCOMPATIBLE_SERVER));
configResult = new TranslationComponent(Lang.BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_CLIENT, Main.PBH_BTN_PROTOCOL_IMPL_VERSION, min_protocol_version);
throw new IllegalStateException(tlUI(configResult));
}
int max_protocol_version = json.get("max_protocol_version").getAsInt();
if (Main.PBH_BTN_PROTOCOL_IMPL_VERSION > max_protocol_version) {
configResult = "Incompatible protocol version: " + Main.PBH_BTN_PROTOCOL_IMPL_VERSION + " > " + max_protocol_version;
configResult = new TranslationComponent(Lang.BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_SERVER, Main.PBH_BTN_PROTOCOL_IMPL_VERSION, max_protocol_version);
throw new IllegalStateException(tlUI(Lang.BTN_INCOMPATIBLE_SERVER));
}
resetScheduler();
Expand Down Expand Up @@ -161,10 +162,10 @@ public void configBtnNetwork() {
}
});
configSuccess.set(true);
configResult = "Successfully configured";
configResult = new TranslationComponent(Lang.BTN_CONFIG_STATUS_SUCCESSFUL);
} catch (Throwable e) {
log.error(tlUI(Lang.BTN_CONFIG_FAILS, statusCode+" - "+response, 600), e);
configResult = "Failed to configure: "+e.getClass().getName()+": "+e.getMessage();
configResult = new TranslationComponent(Lang.BTN_CONFIG_STATUS_EXCEPTION, e.getClass().getName(), e.getMessage());
configSuccess.set(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void status(Context context) {
}

info.put("configSuccess", btnNetwork.getConfigSuccess());
info.put("configResult", btnNetwork.getConfigResult());
info.put("configResult", tl(locale(context), btnNetwork.getConfigResult()));
var abilities = new ArrayList<>();
for (Map.Entry<Class<? extends BtnAbility>, BtnAbility> entry : btnNetwork.getAbilities().entrySet()) {
Map<String, Object> abilityStatus = new HashMap<>();
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/ghostchu/peerbanhelper/text/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,12 @@ public enum Lang {
GUI_MENU_STATS_BANNED,
GUI_MENU_STATS_DOWNLOADER,
GUI_MENU_QUICK_OPERATIONS,
GUI_MENU_STATS,
GUI_MENU_STATS,
KADEMLIA_STARTUP_ERROR,
PORT_MAPPER_PORT_MAPPED,
PORT_MAPPER_PORT_MAPPING_FAILED,
DECENTRALIZED_PORT_FORWARDED,
LAB_EXPERIMENT_IPFS_TITLE,
LAB_EXPERIMENT_IPFS_TITLE,
LAB_EXPERIMENT_IPFS_DESCRIPTION,
PORT_MAPPER_PORT_MAPPED_BUT_INTERNAL_ADDRESS,
PORT_MAPPER_PORT_MAPPING,
Expand Down Expand Up @@ -493,7 +493,13 @@ public enum Lang {
MATCH_STRING_EQUALS,
MATCH_STRING_REGEX,
STATUS_TEXT_PAUSED,
DOWNLOADER_API_TEST_BYPASS_PAUSED, STATUS_BAR_GLOBAL_PAUSED;
DOWNLOADER_API_TEST_BYPASS_PAUSED,
STATUS_BAR_GLOBAL_PAUSED,
BTN_CONFIG_STATUS_UNSUCCESSFUL_HTTP_REQUEST,
BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_CLIENT,
BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_SERVER,
BTN_CONFIG_STATUS_SUCCESSFUL,
BTN_CONFIG_STATUS_EXCEPTION;

public String getKey() {
return name();
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/lang/en_us/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,9 @@ MATCH_STRING_REGEX: "String Regex: {}"
MODULE_IBL_COMMENT_UNKNOWN: "Not Provided"
JSON_MATCHER_NOT_MET: "Condition not meet: {} on {}"
DOWNLOADER_API_TEST_BYPASS_PAUSED: "Download configuration drafted, please enable later"
STATUS_BAR_GLOBAL_PAUSED: "⏸Global Paused"
STATUS_BAR_GLOBAL_PAUSED: "⏸Global Paused"
BTN_CONFIG_STATUS_UNSUCCESSFUL_HTTP_REQUEST: "Failed to send HTTP request to URL {}, HTTP status code: {}, remote response: {}"
BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_CLIENT: "The current client BTN protocol implementation version is too old, local implementation version: {}, server requires at least: {}"
BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_SERVER: "The current server BTN protocol implementation version is too old, server implementation version: {}, local requires at least: {}"
BTN_CONFIG_STATUS_SUCCESSFUL: "Successfully connected to the BTN server"
BTN_CONFIG_STATUS_EXCEPTION: "An unexpected error occurred while connecting to the BTN server: {}"
7 changes: 6 additions & 1 deletion src/main/resources/lang/messages_fallback.yml
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,9 @@ MATCH_STRING_REGEX: "字符串正则表达式: {}"
MODULE_IBL_COMMENT_UNKNOWN: "未提供"
JSON_MATCHER_NOT_MET: "表达式未满足: {} 于 {} 上"
DOWNLOADER_API_TEST_BYPASS_PAUSED: "下载器配置已暂存,请在稍后启用"
STATUS_BAR_GLOBAL_PAUSED: "⏸全局暂停"
STATUS_BAR_GLOBAL_PAUSED: "⏸全局暂停"
BTN_CONFIG_STATUS_UNSUCCESSFUL_HTTP_REQUEST: "向 URL {} 发送 HTTP 请求失败,HTTP 状态码: {},远端响应:{}"
BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_CLIENT: "当前客户端 BTN 协议实现版本过旧,本地实现版本号: {},服务器要求最低: {}"
BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_SERVER: "当前服务器 BTN 协议实现版本过旧,服务器实现版本号: {},本地要求最低: {}"
BTN_CONFIG_STATUS_SUCCESSFUL: "已成功连接到 BTN 服务器"
BTN_CONFIG_STATUS_EXCEPTION: "配置 BTN 功能时发生意外错误:{}: {}"
7 changes: 6 additions & 1 deletion src/main/resources/lang/zh_cn/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,9 @@ MATCH_STRING_REGEX: "字符串正则表达式: {}"
MODULE_IBL_COMMENT_UNKNOWN: "未提供"
JSON_MATCHER_NOT_MET: "表达式未满足: {} 于 {} 上"
DOWNLOADER_API_TEST_BYPASS_PAUSED: "下载器配置已暂存,请在稍后启用"
STATUS_BAR_GLOBAL_PAUSED: "⏸全局暂停"
STATUS_BAR_GLOBAL_PAUSED: "⏸全局暂停"
BTN_CONFIG_STATUS_UNSUCCESSFUL_HTTP_REQUEST: "向 URL {} 发送 HTTP 请求失败,HTTP 状态码: {},远端响应:{}"
BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_CLIENT: "当前客户端 BTN 协议实现版本过旧,本地实现版本号: {},服务器要求最低: {}"
BTN_CONFIG_STATUS_UNSUCCESSFUL_INCOMPATIBLE_BTN_PROTOCOL_VERSION_SERVER: "当前服务器 BTN 协议实现版本过旧,服务器实现版本号: {},本地要求最低: {}"
BTN_CONFIG_STATUS_SUCCESSFUL: "已成功连接到 BTN 服务器"
BTN_CONFIG_STATUS_EXCEPTION: "配置 BTN 功能时发生意外错误:{}: {}"

0 comments on commit fc5c68a

Please sign in to comment.