Skip to content

Commit

Permalink
🐳 #376 Partial i18n of business framework
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Oct 2, 2024
1 parent 7b970dc commit 54e28ca
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class BarSkeletonSetting {
boolean printDataCodec = true;

/** true runners 日志打印 */
boolean printRunners = true;
boolean printRunners = false;

/** inOut 的 in 。 true 开启 */
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.iohao.game.action.skeleton.core.codec.DataCodec;
import com.iohao.game.action.skeleton.core.flow.ActionMethodInOut;
import com.iohao.game.action.skeleton.core.runner.Runners;
import com.iohao.game.action.skeleton.i18n.MessageKey;
import com.iohao.game.action.skeleton.i18n.Bundle;
import com.iohao.game.action.skeleton.toy.IoGameBanner;
import com.iohao.game.common.kit.ArrayKit;
import com.iohao.game.common.kit.StrKit;
Expand Down Expand Up @@ -78,7 +80,9 @@ private static void extractedRunners(BarSkeleton barSkeleton) {
List<String> nameList = runners.listRunnerName();
String title = "@|CYAN ======================== Runners ========================= |@";
IoGameBanner.println(Ansi.ansi().render(title));
IoGameBanner.println("如果需要关闭打印, 查看 BarSkeletonBuilder#setting#printRunners");

var printActionKitClose = Bundle.getMessage(MessageKey.printActionKitPrintClose);
IoGameBanner.println(printActionKitClose + " BarSkeletonBuilder.setting.printRunners");

for (String name : nameList) {
String info = String.format("@|BLUE %s |@", name);
Expand All @@ -94,7 +98,9 @@ private static void extractedRunners(BarSkeleton barSkeleton) {
void printInout(List<ActionMethodInOut> inOuts) {
String title = "@|CYAN ======================== InOut ========================= |@";
IoGameBanner.println(Ansi.ansi().render(title));
IoGameBanner.println("如果需要关闭打印, 查看 BarSkeletonBuilder#setting#printInout");

var printActionKitClose = Bundle.getMessage(MessageKey.printActionKitPrintClose);
IoGameBanner.println(printActionKitClose + " BarSkeletonBuilder.setting.printInout");

for (ActionMethodInOut inOut : inOuts) {
String info = String.format("@|BLUE %s |@", inOut.getClass());
Expand All @@ -103,15 +109,17 @@ void printInout(List<ActionMethodInOut> inOuts) {
}

void printHandler(List<Handler> handlers) {
String iohaoTitle = "@|CYAN ======================== 业务框架 iohao ========================= |@";
String iohaoTitle = "@|CYAN ======================== iohao ========================= |@";
IoGameBanner.println(Ansi.ansi().render(iohaoTitle));
IoGameBanner.println(IoGameVersion.VERSION);
String colorStr = "@|BLACK BLACK|@ @|RED RED|@ @|GREEN GREEN|@ @|YELLOW YELLOW|@ @|BLUE BLUE|@ @|MAGENTA MAGENTA|@ @|CYAN CYAN|@ @|WHITE WHITE|@ @|DEFAULT DEFAULT|@";
IoGameBanner.println(Ansi.ansi().render(colorStr));

String title = "@|CYAN ======================== Handler ========================= |@";
IoGameBanner.println(Ansi.ansi().render(title));
IoGameBanner.println("如果需要关闭打印, 查看 BarSkeletonBuilder#setting#printHandler");

var printActionKitClose = Bundle.getMessage(MessageKey.printActionKitPrintClose);
IoGameBanner.println(printActionKitClose + " BarSkeletonBuilder.setting.printHandler");

for (Handler handler : handlers) {
String info = String.format("@|BLUE %s |@", handler.getClass());
Expand All @@ -123,11 +131,13 @@ void printActionCommand(ActionCommand[][] behaviors, boolean shortName) {
String title = "@|CYAN ======================== action ========================= |@";
IoGameBanner.println(Ansi.ansi().render(title));

String tip = """
如果需要关闭打印, 查看 BarSkeletonBuilder#setting#printAction;
如需要打印(class method params return)完整的包名, 查看 BarSkeletonBuilder#setting#printActionShort;
""";
IoGameBanner.print(tip);
var printActionKitClose = Bundle.getMessage(MessageKey.printActionKitPrintClose);
IoGameBanner.println(printActionKitClose + " BarSkeletonBuilder.setting.printAction");

var printActionKitPrintFull = Bundle.getMessage(MessageKey.printActionKitPrintFull);
IoGameBanner.println(printActionKitPrintFull + " BarSkeletonBuilder.setting.printActionShort");

String cmdName = Bundle.getMessage(MessageKey.cmdName);

for (int cmd = 0; cmd < behaviors.length; cmd++) {
ActionCommand[] subBehaviors = behaviors[cmd];
Expand Down Expand Up @@ -179,7 +189,7 @@ void printActionCommand(ActionCommand[][] behaviors, boolean shortName) {
params.put("actionSimpleName", subBehavior.getActionControllerClazz().getSimpleName());
params.put("lineNumber", subBehavior.actionCommandDoc.getLineNumber());

String routeCell = Color.red.format("路由: {cmd} - {subCmd}", params);
String routeCell = Color.red.format(cmdName + ": {cmd} - {subCmd}", params);
String actionCell = Color.white.wrap("--- action :");
String actionNameCell = Color.blue.format("{actionName}", params);
String methodNameCell = Color.blue.format("{methodName}", params);
Expand All @@ -206,12 +216,14 @@ void printActionCommand(ActionCommand[][] behaviors, boolean shortName) {
}

void printDataCodec() {
DataCodec dataCodec = DataCodecKit.dataCodec;

String title = "@|CYAN ======================== 当前使用的编解码器 ========================= |@";
var printActionKitDataCodec = Bundle.getMessage(MessageKey.printActionKitDataCodec);
String title = "@|CYAN ======================== %s ========================= |@".formatted(printActionKitDataCodec);
IoGameBanner.println(Ansi.ansi().render(title));
IoGameBanner.println("如果需要关闭打印, 查看 BarSkeletonBuilder#setting#printDataCodec");

var printActionKitClose = Bundle.getMessage(MessageKey.printActionKitPrintClose);
IoGameBanner.println(printActionKitClose + " BarSkeletonBuilder.setting.printDataCodec");

DataCodec dataCodec = DataCodecKit.dataCodec;
String info = String.format("@|BLUE %s - %s |@", dataCodec.codecName(), dataCodec.getClass().getName());
IoGameBanner.println(Ansi.ansi().render(info));

Expand All @@ -232,7 +244,9 @@ private void shortName(Map<String, Object> params, boolean shortName) {
private void checkReturnType(final Class<?> returnTypeClazz) {
if (Set.class.isAssignableFrom(returnTypeClazz) || Map.class.isAssignableFrom(returnTypeClazz)) {
// 参数的不支持不写逻辑了,这里告诉一下就行了。看之后的需要在考虑是否支持吧
ThrowKit.ofRuntimeException("action 返回值和参数不支持 set、map 和 基础类型!");
// Action return values and parameters do not support set, map and basic types!
var printActionKitCheckReturnType = Bundle.getMessage(MessageKey.printActionKitCheckReturnType);
ThrowKit.ofRuntimeException(printActionKitCheckReturnType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.iohao.game.action.skeleton.core.flow.ActionMethodInOut;
import com.iohao.game.action.skeleton.core.flow.FlowContext;
import com.iohao.game.action.skeleton.core.flow.attr.FlowAttr;
import com.iohao.game.action.skeleton.i18n.Bundle;
import com.iohao.game.action.skeleton.i18n.MessageKey;
import com.iohao.game.action.skeleton.protocol.HeadMetadata;
import com.iohao.game.action.skeleton.protocol.ResponseMessage;
import com.iohao.game.action.skeleton.protocol.wrapper.ByteValueList;
Expand Down Expand Up @@ -173,13 +175,35 @@ public void fuckOut(final FlowContext flowContext) {

ResponseMessage responseMessage = flowContext.getResponse();

extractedI18n(paramMap);

if (responseMessage.hasError()) {
this.printValidate(flowContext, paramMap);
} else {
this.printNormal(flowContext, paramMap);
}
}

private static void extractedI18n(Map<String, Object> paramMap) {
String debugInOutThreadName = Bundle.getMessage(MessageKey.debugInOutThreadName);
paramMap.put(MessageKey.debugInOutThreadName, debugInOutThreadName);

String debugInOutParamName = Bundle.getMessage(MessageKey.debugInOutParamName);
paramMap.put(MessageKey.debugInOutParamName, debugInOutParamName);

String debugInOutReturnData = Bundle.getMessage(MessageKey.debugInOutReturnData);
paramMap.put(MessageKey.debugInOutReturnData, debugInOutReturnData);

String debugInOutErrorCode = Bundle.getMessage(MessageKey.debugInOutErrorCode);
paramMap.put(MessageKey.debugInOutErrorCode, debugInOutErrorCode);

String debugInOutErrorMsg = Bundle.getMessage(MessageKey.debugInOutErrorMsg);
paramMap.put(MessageKey.debugInOutErrorMsg, debugInOutErrorMsg);

String debugInOutTime = Bundle.getMessage(MessageKey.debugInOutTime);
paramMap.put(MessageKey.debugInOutTime, debugInOutTime);
}

private static void extractedTraceId(FlowContext flowContext, Map<String, Object> paramMap) {
HeadMetadata headMetadata = flowContext.getHeadMetadata();
String traceId = headMetadata.getTraceId();
Expand All @@ -197,10 +221,12 @@ private static void extractedJoin(FlowContext flowContext, Map<String, Object> p
HeadMetadata headMetadata = flowContext.getHeadMetadata();
int stick = headMetadata.getStick();

String connectionWay = Bundle.getMessage(MessageKey.connectionWay);

String str = switch (stick) {
case 1 -> " [连接方式:TCP] ";
case 2 -> " [连接方式:WebSocket] ";
case 3 -> " [连接方式:UDP] ";
case 1 -> " [%s:TCP] ".formatted(connectionWay);
case 2 -> " [%s:WebSocket] ".formatted(connectionWay);
case 3 -> " [%s:UDP] ".formatted(connectionWay);
default -> "";
};

Expand All @@ -218,13 +244,13 @@ private void printValidate(FlowContext flowContext, Map<String, Object> paramMap
}

String template = """
┏━━错误━━━ Debug. [({className}.java:{lineNumber}).{actionMethodName}] ━━━ {cmdInfo} ━━━ [{logicServerTag}] ━━━ [id:{logicServerId}]
┏━━━━━ Error. [({className}.java:{lineNumber}).{actionMethodName}] ━━━ {cmdInfo} ━━━ [{logicServerTag}] ━━━ [id:{logicServerId}]
┣ userId: {userId}
参数: {paramName} : {paramData}
错误码: {errorCode}
错误信息: {validatorMsg}
时间: {time} ms (业务方法总耗时)
┗━━━━━ [ioGame:{ioGameVersion}] ━━━━━ [线程:{threadName}] ━━━━━{joinName}━━━━━{traceId}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
{debugInOutParamName}: {paramName} : {paramData}
{debugInOutErrorCode}: {errorCode}
{debugInOutErrorMsg}: {validatorMsg}
{debugInOutTime}: {time} ms
┗━━━━━ [ioGame:{ioGameVersion}] ━━━━━ [{debugInOutThreadName}:{threadName}] ━━━━━{joinName}━━━━━{traceId}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""";

String message = StrKit.format(template, paramMap);
Expand All @@ -243,10 +269,10 @@ private void printNormal(FlowContext flowContext, Map<String, Object> paramMap)
String template = """
┏━━━━━ Debug. [({className}.java:{lineNumber}).{actionMethodName}] ━━━━━ {cmdInfo} ━━━━━ [{logicServerTag}] ━━━━━ [id:{logicServerId}]
┣ userId: {userId}
参数: {paramName} : {paramData}
响应: {returnData}
时间: {time} ms (业务方法总耗时)
┗━━━━━ [ioGame:{ioGameVersion}] ━━━━━ [线程:{threadName}] ━━━━━{joinName}━━━━━{traceId}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
{debugInOutParamName}: {paramName} : {paramData}
{debugInOutReturnData}: {returnData}
{debugInOutTime}: {time} ms
┗━━━━━ [ioGame:{ioGameVersion}] ━━━━━ [{debugInOutThreadName}:{threadName}] ━━━━━{joinName}━━━━━{traceId}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""";

String message = StrKit.format(template, paramMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public Runners() {
public void addRunner(Runner runner) {

if (this.onStart.get()) {
ThrowKit.ofRuntimeException("运行中,不能添加 Runner 了");
// 运行中,不能添加 Runner 了。
ThrowKit.ofRuntimeException("Cannot add Runner while running.");
}

Objects.requireNonNull(runner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ void register(Object eventBusSubscriber, SubscriberInvokeCreator subscriberInvok
Class<?> clazz = eventBusSubscriber.getClass();

if (!eventBusSubscriberSet.add(clazz)) {
ThrowKit.ofRuntimeException("已经存在 " + clazz);
ThrowKit.ofRuntimeException("Already exists : " + clazz);
}

// 方法访问器: 获取类中自己定义的方法
Expand Down Expand Up @@ -528,7 +528,8 @@ final class DefaultEventBus implements EventBus {
public void register(Object eventBusSubscriber) {

if (status != EventBusStatus.register) {
ThrowKit.ofRuntimeException("运行中不允许注册订阅者,请在 EventRunner.registerEventBus 方法中注册。 ");
// 运行中不允许注册订阅者,请在 EventRunner.registerEventBus 方法中注册。
ThrowKit.ofRuntimeException("Subscriber registration is not allowed during running. Please register in EventRunner.registerEventBus method.");
}

// 注册
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 ([email protected][email protected]) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.action.skeleton.i18n;

import lombok.experimental.UtilityClass;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.Objects;
import java.util.ResourceBundle;

/**
* @author 渔民小镇
* @date 2024-10-02
* @since 21.18
*/
@UtilityClass
public final class Bundle {
final String baseName = "iohao";
ResourceBundle bundle;

ResourceBundle getBundle() {
if (Objects.isNull(bundle)) {
bundle = ResourceBundle.getBundle(baseName, Locale.getDefault());
}

return bundle;
}

public String getMessage(String key) {
return getBundle().getString(key);
}

public String getMessage(String key, Object... args) {
var keyPattern = getBundle().getString(key);
return MessageFormat.format(keyPattern, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 ([email protected][email protected]) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General License for more details.
*
* You should have received a copy of the GNU Affero General License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.action.skeleton.i18n;

/**
* @author 渔民小镇
* @date 2024-10-02
* @since 21.18
*/
public interface MessageKey {
String cmdName = "cmdName";
String gameExternalServer = "gameExternalServer";
/** ExternalJoinEnum */
String connectionWay = "connectionWay";

/* see PrintActionKit.java */
String printActionKitPrintClose = "printActionKitPrintClose";
String printActionKitPrintFull = "printActionKitPrintFull";
String printActionKitDataCodec = "printActionKitDataCodec";
String printActionKitCheckReturnType = "printActionKitCheckReturnType";

/* see DebugInOut.java */
String debugInOutThreadName = "debugInOutThreadName";
String debugInOutParamName = "debugInOutParamName";
String debugInOutReturnData = "debugInOutReturnData";
String debugInOutErrorCode = "debugInOutErrorCode";
String debugInOutErrorMsg = "debugInOutErrorMsg";
String debugInOutTime = "debugInOutTime";
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ protected void trick() {
protected void broadcast() {
boolean emptyUser = CollKit.isEmpty(this.userIds);
if (checkEmptyUser && emptyUser) {
ThrowKit.ofRuntimeException("没有添加消息推送人");
// 请添加消息推送人
ThrowKit.ofRuntimeException("Please add a message sender");
}

// 推送响应(广播消息)给指定的用户列表
Expand Down
17 changes: 17 additions & 0 deletions common/common-core/src/main/resources/iohao.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# common
gameExternalServer=GameExternalServer
cmdName=Routing
# ExternalJoinEnum
connectionWay=Connection way
# see PrintActionKit.java
printActionKitPrintClose=To turn off printing, see
printActionKitPrintFull=Print the full package name, see
printActionKitDataCodec=DataCodec
printActionKitCheckReturnType=Action return values and parameters do not support set, map and basic types!
# see DebugInOut.java
debugInOutThreadName=Thread
debugInOutParamName=RequestParam
debugInOutReturnData=ResponseData
debugInOutErrorCode=ErrorCode
debugInOutErrorMsg=ErrorMsg
debugInOutTime=ExecutionTime
17 changes: 17 additions & 0 deletions common/common-core/src/main/resources/iohao_zh_CN.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# common
gameExternalServer=\u6E38\u620F\u5BF9\u5916\u670D
cmdName=\u8DEF\u7531
# ExternalJoinEnum
connectionWay=\u8FDE\u63A5\u65B9\u5F0F
# see PrintActionKit.java
printActionKitPrintClose=\u5173\u95ED\u6253\u5370, \u67E5\u770B
printActionKitPrintFull=\u6253\u5370\u5B8C\u6574\u5305\u540D, \u67E5\u770B
printActionKitDataCodec=\u5F53\u524D\u4F7F\u7528\u7684\u7F16\u89E3\u7801\u5668
printActionKitCheckReturnType=action \u8FD4\u56DE\u503C\u548C\u53C2\u6570\u4E0D\u652F\u6301 set\u3001map \u548C \u57FA\u7840\u7C7B\u578B!
# see DebugInOut.java
debugInOutThreadName=\u7EBF\u7A0B
debugInOutParamName=\u53C2\u6570
debugInOutReturnData=\u54CD\u5E94
debugInOutErrorCode=\u9519\u8BEF\u7801
debugInOutErrorMsg=\u9519\u8BEF\u4FE1\u606F
debugInOutTime=\u8017\u65F6
Loading

0 comments on commit 54e28ca

Please sign in to comment.