Skip to content

Commit 844e445

Browse files
committed
feat(HTTP): 实现HTTP模块的国际化支持
将HTTP模块中的硬编码字符串替换为通过LocalizationService获取的国际化资源 添加NetWork.Http相关键名和资源文件的中英文翻译 修改HttpStatusMessage中的常量为通过本地化服务获取
1 parent 9fcd668 commit 844e445

5 files changed

Lines changed: 285 additions & 41 deletions

File tree

GameFrameX.Localization/Keys.NetWorkHttp.cs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,152 @@ public static class NetWorkHttp
149149
/// 参数: {0} - 类名, {1} - 要求的后缀
150150
/// </remarks>
151151
public const string ClassMustEndWithSuffix = "NetWork.Http.ClassMustEndWithSuffix";
152+
153+
/// <summary>
154+
/// [HTTPServer] TraceIdentifier:[{0}], 来源[{1}], url:[{2}]
155+
/// </summary>
156+
/// <remarks>
157+
/// 键名: NetWork.Http.RequestLogHeader
158+
/// 用途: HTTP请求的日志头信息
159+
/// 参数: {0} - TraceIdentifier, {1} - IP地址, {2} - URL
160+
/// </remarks>
161+
public const string RequestLogHeader = "NetWork.Http.RequestLogHeader";
162+
163+
/// <summary>
164+
/// {0},请求方式:[{1}]
165+
/// </summary>
166+
/// <remarks>
167+
/// 键名: NetWork.Http.RequestMethod
168+
/// 用途: HTTP请求方法日志
169+
/// 参数: {0} - 日志头, {1} - 请求方法
170+
/// </remarks>
171+
public const string RequestMethod = "NetWork.Http.RequestMethod";
172+
173+
/// <summary>
174+
/// 消息对象编码异常,请检查错误日志
175+
/// </summary>
176+
/// <remarks>
177+
/// 键名: NetWork.Http.MessageEncodingException
178+
/// 用途: 当消息对象编码异常时记录错误
179+
/// </remarks>
180+
public const string MessageEncodingException = "NetWork.Http.MessageEncodingException";
181+
182+
/// <summary>
183+
/// http header content type is null
184+
/// </summary>
185+
/// <remarks>
186+
/// 键名: NetWork.Http.HttpHeaderContentTypeNull
187+
/// 用途: 当HTTP头的Content-Type为null时的错误消息
188+
/// </remarks>
189+
public const string HttpHeaderContentTypeNull = "NetWork.Http.HttpHeaderContentTypeNull";
190+
191+
/// <summary>
192+
/// data verification failed
193+
/// </summary>
194+
/// <remarks>
195+
/// 键名: NetWork.Http.DataVerificationFailed
196+
/// 用途: 当数据验证失败时的错误消息
197+
/// </remarks>
198+
public const string DataVerificationFailed = "NetWork.Http.DataVerificationFailed";
199+
200+
/// <summary>
201+
/// 请求参数
202+
/// </summary>
203+
/// <remarks>
204+
/// 键名: NetWork.Http.SwaggerRequestParameters
205+
/// 用途: Swagger文档中的请求参数描述
206+
/// </remarks>
207+
public const string SwaggerRequestParameters = "NetWork.Http.SwaggerRequestParameters";
208+
209+
/// <summary>
210+
/// 响应状态码
211+
/// </summary>
212+
/// <remarks>
213+
/// 键名: NetWork.Http.SwaggerResponseStatusCode
214+
/// 用途: Swagger文档中的响应状态码描述
215+
/// </remarks>
216+
public const string SwaggerResponseStatusCode = "NetWork.Http.SwaggerResponseStatusCode";
217+
218+
/// <summary>
219+
/// 响应消息
220+
/// </summary>
221+
/// <remarks>
222+
/// 键名: NetWork.Http.SwaggerResponseMessage
223+
/// 用途: Swagger文档中的响应消息描述
224+
/// </remarks>
225+
public const string SwaggerResponseMessage = "NetWork.Http.SwaggerResponseMessage";
226+
227+
/// <summary>
228+
/// 成功响应
229+
/// </summary>
230+
/// <remarks>
231+
/// 键名: NetWork.Http.SwaggerSuccessResponse
232+
/// 用途: Swagger文档中的成功响应描述
233+
/// </remarks>
234+
public const string SwaggerSuccessResponse = "NetWork.Http.SwaggerSuccessResponse";
235+
236+
/// <summary>
237+
/// application/json
238+
/// </summary>
239+
/// <remarks>
240+
/// 键名: NetWork.Http.ApplicationJsonContentType
241+
/// 用途: application/json内容类型
242+
/// </remarks>
243+
public const string ApplicationJsonContentType = "NetWork.Http.ApplicationJsonContentType";
244+
245+
/// <summary>
246+
/// application/x-protobuf
247+
/// </summary>
248+
/// <remarks>
249+
/// 键名: NetWork.Http.ApplicationProtoBufContentType
250+
/// 用途: application/x-protobuf内容类型
251+
/// </remarks>
252+
public const string ApplicationProtoBufContentType = "NetWork.Http.ApplicationProtoBufContentType";
253+
254+
/// <summary>
255+
/// http命令未包含验证参数{0} 和 {1}
256+
/// </summary>
257+
/// <remarks>
258+
/// 键名: NetWork.Http.HttpCommandMissingValidationParameters
259+
/// 用途: 当HTTP命令缺少验证参数时的错误消息
260+
/// 参数: {0} - 第一个参数名, {1} - 第二个参数名
261+
/// </remarks>
262+
public const string HttpCommandMissingValidationParameters = "NetWork.Http.HttpCommandMissingValidationParameters";
263+
264+
/// <summary>
265+
/// undefined command
266+
/// </summary>
267+
/// <remarks>
268+
/// 键名: NetWork.Http.UndefinedCommand
269+
/// 用途: 使用未定义命令时的错误消息
270+
/// </remarks>
271+
public const string UndefinedCommand = "NetWork.Http.UndefinedCommand";
272+
273+
/// <summary>
274+
/// not found command
275+
/// </summary>
276+
/// <remarks>
277+
/// 键名: NetWork.Http.NotFoundCommand
278+
/// 用途: 命令未找到时的错误消息
279+
/// </remarks>
280+
public const string NotFoundCommand = "NetWork.Http.NotFoundCommand";
281+
282+
/// <summary>
283+
/// check failed command
284+
/// </summary>
285+
/// <remarks>
286+
/// 键名: NetWork.Http.CheckFailedCommand
287+
/// 用途: 命令验证失败时的错误消息
288+
/// </remarks>
289+
public const string CheckFailedCommand = "NetWork.Http.CheckFailedCommand";
290+
291+
/// <summary>
292+
/// server error
293+
/// </summary>
294+
/// <remarks>
295+
/// 键名: NetWork.Http.ServerError
296+
/// 用途: 服务器内部错误时的错误消息
297+
/// </remarks>
298+
public const string ServerError = "NetWork.Http.ServerError";
152299
}
153300
}

GameFrameX.Localization/Localization/Messages/Resources.resx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,54 @@
263263
<data name="NetWork.Http.ClassMustEndWithSuffix" xml:space="preserve">
264264
<value>{0} must end with {1}</value>
265265
</data>
266+
<data name="NetWork.Http.RequestLogHeader" xml:space="preserve">
267+
<value>[HTTPServer] TraceIdentifier:[{0}], Source[{1}], url:[{2}]</value>
268+
</data>
269+
<data name="NetWork.Http.RequestMethod" xml:space="preserve">
270+
<value>{0}, Request Method:[{1}]</value>
271+
</data>
272+
<data name="NetWork.Http.MessageEncodingException" xml:space="preserve">
273+
<value>Message object encoding exception, please check error logs</value>
274+
</data>
275+
<data name="NetWork.Http.HttpHeaderContentTypeNull" xml:space="preserve">
276+
<value>http header content type is null</value>
277+
</data>
278+
<data name="NetWork.Http.DataVerificationFailed" xml:space="preserve">
279+
<value>data verification failed</value>
280+
</data>
281+
<data name="NetWork.Http.SwaggerRequestParameters" xml:space="preserve">
282+
<value>Request Parameters</value>
283+
</data>
284+
<data name="NetWork.Http.SwaggerResponseStatusCode" xml:space="preserve">
285+
<value>Response Status Code</value>
286+
</data>
287+
<data name="NetWork.Http.SwaggerResponseMessage" xml:space="preserve">
288+
<value>Response Message</value>
289+
</data>
290+
<data name="NetWork.Http.SwaggerSuccessResponse" xml:space="preserve">
291+
<value>Success Response</value>
292+
</data>
293+
<data name="NetWork.Http.ApplicationJsonContentType" xml:space="preserve">
294+
<value>application/json</value>
295+
</data>
296+
<data name="NetWork.Http.ApplicationProtoBufContentType" xml:space="preserve">
297+
<value>application/x-protobuf</value>
298+
</data>
299+
<data name="NetWork.Http.HttpCommandMissingValidationParameters" xml:space="preserve">
300+
<value>http command missing validation parameters {0} and {1}</value>
301+
</data>
302+
<data name="NetWork.Http.UndefinedCommand" xml:space="preserve">
303+
<value>undefined command</value>
304+
</data>
305+
<data name="NetWork.Http.NotFoundCommand" xml:space="preserve">
306+
<value>not found command</value>
307+
</data>
308+
<data name="NetWork.Http.CheckFailedCommand" xml:space="preserve">
309+
<value>check failed command</value>
310+
</data>
311+
<data name="NetWork.Http.ServerError" xml:space="preserve">
312+
<value>server error</value>
313+
</data>
266314

267315
<!-- NetWork.Abstractions module log and error keys -->
268316
<data name="NetWork.Abstractions.MessageIdDuplicate" xml:space="preserve">

GameFrameX.Localization/Localization/Messages/Resources.zh-CN.resx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,54 @@
295295
<data name="NetWork.Http.ClassMustEndWithSuffix" xml:space="preserve">
296296
<value>{0} 必须以{1}结尾</value>
297297
</data>
298+
<data name="NetWork.Http.RequestLogHeader" xml:space="preserve">
299+
<value>[HTTPServer] TraceIdentifier:[{0}], 来源[{1}], url:[{2}]</value>
300+
</data>
301+
<data name="NetWork.Http.RequestMethod" xml:space="preserve">
302+
<value>{0},请求方式:[{1}]</value>
303+
</data>
304+
<data name="NetWork.Http.MessageEncodingException" xml:space="preserve">
305+
<value>消息对象编码异常,请检查错误日志</value>
306+
</data>
307+
<data name="NetWork.Http.HttpHeaderContentTypeNull" xml:space="preserve">
308+
<value>http header content type is null</value>
309+
</data>
310+
<data name="NetWork.Http.DataVerificationFailed" xml:space="preserve">
311+
<value>data verification failed</value>
312+
</data>
313+
<data name="NetWork.Http.SwaggerRequestParameters" xml:space="preserve">
314+
<value>请求参数</value>
315+
</data>
316+
<data name="NetWork.Http.SwaggerResponseStatusCode" xml:space="preserve">
317+
<value>响应状态码</value>
318+
</data>
319+
<data name="NetWork.Http.SwaggerResponseMessage" xml:space="preserve">
320+
<value>响应消息</value>
321+
</data>
322+
<data name="NetWork.Http.SwaggerSuccessResponse" xml:space="preserve">
323+
<value>成功响应</value>
324+
</data>
325+
<data name="NetWork.Http.ApplicationJsonContentType" xml:space="preserve">
326+
<value>application/json</value>
327+
</data>
328+
<data name="NetWork.Http.ApplicationProtoBufContentType" xml:space="preserve">
329+
<value>application/x-protobuf</value>
330+
</data>
331+
<data name="NetWork.Http.HttpCommandMissingValidationParameters" xml:space="preserve">
332+
<value>http命令未包含验证参数{0} 和 {1}</value>
333+
</data>
334+
<data name="NetWork.Http.UndefinedCommand" xml:space="preserve">
335+
<value>未定义的命令</value>
336+
</data>
337+
<data name="NetWork.Http.NotFoundCommand" xml:space="preserve">
338+
<value>未找到的命令</value>
339+
</data>
340+
<data name="NetWork.Http.CheckFailedCommand" xml:space="preserve">
341+
<value>验证失败的命令</value>
342+
</data>
343+
<data name="NetWork.Http.ServerError" xml:space="preserve">
344+
<value>服务器错误</value>
345+
</data>
298346

299347
<!-- NetWork.Abstractions模块日志和错误键名 -->
300348
<data name="NetWork.Abstractions.MessageIdDuplicate" xml:space="preserve">

GameFrameX.NetWork.HTTP/HttpHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public static async Task HandleRequest(HttpContext context, Func<string, BaseHtt
6464
var ip = context.Connection.RemoteIpAddress?.ToString();
6565
string url = context.Request.PathBase + context.Request.Path;
6666
var command = context.Request.Path.ToString().Substring(GlobalSettings.CurrentSetting.HttpUrl.Length);
67-
var logHeader = $"[HTTPServer] TraceIdentifier:[{context.TraceIdentifier}], 来源[{ip}], url:[{url}]";
68-
LogHelper.Debug($"{logHeader},请求方式:[{context.Request.Method}]");
67+
var logHeader = LocalizationService.GetString(GameFrameX.Localization.Keys.NetWorkHttp.RequestLogHeader, context.TraceIdentifier, ip, url);
68+
LogHelper.Debug(LocalizationService.GetString(GameFrameX.Localization.Keys.NetWorkHttp.RequestMethod, logHeader, context.Request.Method));
6969

7070
try
7171
{
@@ -85,7 +85,7 @@ public static async Task HandleRequest(HttpContext context, Func<string, BaseHtt
8585
var headContentType = context.Request.ContentType;
8686
if (headContentType.IsNullOrWhiteSpace())
8787
{
88-
await context.Response.WriteAsync("http header content type is null");
88+
await context.Response.WriteAsync(LocalizationService.GetString(GameFrameX.Localization.Keys.NetWorkHttp.HttpHeaderContentTypeNull));
8989
return;
9090
}
9191

@@ -204,7 +204,7 @@ public static async Task HandleRequest(HttpContext context, Func<string, BaseHtt
204204
}
205205
catch (Exception e)
206206
{
207-
LogHelper.Error("消息对象编码异常,请检查错误日志");
207+
LogHelper.Error(LocalizationService.GetString(GameFrameX.Localization.Keys.NetWorkHttp.MessageEncodingException));
208208
LogHelper.Error(e);
209209
}
210210
}
@@ -238,7 +238,7 @@ public static async Task HandleRequest(HttpContext context, Func<string, BaseHtt
238238
}
239239
else
240240
{
241-
await context.Response.WriteAsync(HttpJsonResult.ErrorString(400, "data verification failed"));
241+
await context.Response.WriteAsync(HttpJsonResult.ErrorString(400, LocalizationService.GetString(GameFrameX.Localization.Keys.NetWorkHttp.DataVerificationFailed)));
242242
}
243243
}
244244
}

0 commit comments

Comments
 (0)