Skip to content

Commit f3dff3f

Browse files
committed
feat(本地化): 为扩展模块添加本地化支持
添加本地化资源文件和键常量,替换硬编码错误消息为本地化字符串 添加对GameFrameX.Foundation.Localization的项目引用 在多个扩展类中更新异常消息使用本地化服务
1 parent 4490f04 commit f3dff3f

9 files changed

Lines changed: 442 additions & 18 deletions

File tree

GameFrameX.Foundation.Extensions/ByteExtensions.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Buffers.Binary;
22
using System.Net;
33
using System.Text;
4+
using GameFrameX.Foundation.Localization.Core;
5+
using GameFrameX.Foundation.Extensions.Localization;
46

57
namespace GameFrameX.Foundation.Extensions;
68

@@ -94,7 +96,7 @@ public static string ToHex(this byte[] bytes, int offset, int count)
9496

9597
if (offset + count > bytes.Length)
9698
{
97-
throw new ArgumentException("The sum of offset and count is greater than the buffer length.", nameof(count));
99+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Exceptions.OffsetCountExceedBufferLength), nameof(count));
98100
}
99101

100102
var stringBuilder = new StringBuilder();
@@ -135,7 +137,7 @@ public static string ToDefaultString(this byte[] bytes, int index, int count)
135137

136138
if (index + count > bytes.Length)
137139
{
138-
throw new ArgumentException("The sum of index and count is greater than the buffer length.", nameof(count));
140+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Exceptions.IndexCountExceedBufferLength), nameof(count));
139141
}
140142

141143
return Encoding.Default.GetString(bytes, index, count);
@@ -170,7 +172,7 @@ public static string ToUtf8String(this byte[] bytes, int index, int count)
170172

171173
if (index + count > bytes.Length)
172174
{
173-
throw new ArgumentException("The sum of index and count is greater than the buffer length.", nameof(count));
175+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Exceptions.IndexCountExceedBufferLength), nameof(count));
174176
}
175177

176178
return Encoding.UTF8.GetString(bytes, index, count);
@@ -583,7 +585,7 @@ public static unsafe void WriteBytesWithoutLength(this byte[] buffer, byte[] val
583585

584586
if (offset + value.Length > buffer.Length)
585587
{
586-
throw new ArgumentException($"buffer write out of index {offset + value.Length}, {buffer.Length}");
588+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Exceptions.BufferWriteOutOfRange, offset + value.Length, buffer.Length));
587589
}
588590

589591
fixed (byte* ptr = buffer, valPtr = value)
@@ -638,7 +640,7 @@ public static void WriteStringValue(this byte[] buffer, string value, ref int of
638640

639641
if (len > short.MaxValue)
640642
{
641-
throw new ArgumentException($"string length exceed short.MaxValue {len}, {short.MaxValue}");
643+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Exceptions.StringLengthExceedMaxValue, len, short.MaxValue));
642644
}
643645

644646

@@ -1086,7 +1088,7 @@ public static byte[] Xor(this byte[] bytes1, byte[] bytes2)
10861088

10871089
if (bytes1.Length != bytes2.Length)
10881090
{
1089-
throw new ArgumentException("The length of bytes1 and bytes2 must be equal.");
1091+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Exceptions.ByteArrayLengthMustEqual));
10901092
}
10911093

10921094
var result = new byte[bytes1.Length];

GameFrameX.Foundation.Extensions/CollectionExtensions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// GameFrameX 组织下的以及组织衍生的项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
2-
//
2+
//
33
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE 文件。
4-
//
4+
//
55
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
66

7+
using GameFrameX.Foundation.Localization.Core;
8+
using GameFrameX.Foundation.Extensions.Localization;
9+
710
namespace GameFrameX.Foundation.Extensions;
811

912
/// <summary>
@@ -168,7 +171,7 @@ public static T Random<T>(this List<T> list)
168171
ArgumentNullException.ThrowIfNull(list, nameof(list));
169172
if (list.Count == 0)
170173
{
171-
throw new ArgumentException("List cannot be empty.", nameof(list));
174+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Exceptions.ListCannotBeEmpty), nameof(list));
172175
}
173176

174177
var n = list.Count;
@@ -192,7 +195,7 @@ public static T Random<T>(this List<T> list, Random random)
192195

193196
if (list.Count == 0)
194197
{
195-
throw new ArgumentException("List cannot be empty.", nameof(list));
198+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Exceptions.ListCannotBeEmpty), nameof(list));
196199
}
197200

198201
return list[random.Next(list.Count)];

GameFrameX.Foundation.Extensions/GameFrameX.Foundation.Extensions.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
3434
<OutputPath>..\bin\app</OutputPath>
3535
</PropertyGroup>
36+
<ItemGroup>
37+
<Folder Include="Localization\Messages\" />
38+
</ItemGroup>
39+
<ItemGroup>
40+
<ProjectReference Include="..\GameFrameX.Foundation.Localization\GameFrameX.Foundation.Localization.csproj" />
41+
</ItemGroup>
3642
<ItemGroup>
3743
<None Include="..\logo.png">
3844
<Pack>True</Pack>
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
// GameFrameX 组织下的以及组织衍生的项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
2+
//
3+
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE 文件。
4+
//
5+
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
6+
7+
using System;
8+
9+
namespace GameFrameX.Foundation.Extensions.Localization;
10+
11+
/// <summary>
12+
/// Extensions 模块本地化资源键常量定义
13+
/// </summary>
14+
/// <remarks>
15+
/// 这个类定义了 Extensions 模块中所有可本地化字符串的键常量。
16+
/// 使用常量可以避免字符串硬编码,提高代码的可维护性和类型安全性。
17+
/// </remarks>
18+
/// <example>
19+
/// <code>
20+
/// // 在代码中使用本地化键常量
21+
/// throw new ArgumentException(
22+
/// nameof(buffer),
23+
/// LocalizationService.GetString(LocalizationKeys.Exceptions.OffsetCountExceedBufferLength));
24+
/// </code>
25+
/// </example>
26+
public static class LocalizationKeys
27+
{
28+
/// <summary>
29+
/// 异常消息资源键
30+
/// </summary>
31+
public static class Exceptions
32+
{
33+
/// <summary>
34+
/// 偏移量和计数超出缓冲区长度的错误消息
35+
/// </summary>
36+
/// <remarks>
37+
/// 键名: Extensions.Exceptions.OffsetCountExceedBufferLength
38+
/// 用途: 当尝试从缓冲区读取超出范围的字节时使用
39+
/// 参数: {0} - 缓冲区长度, {1} - 偏移量, {2} - 计数
40+
/// </remarks>
41+
public const string OffsetCountExceedBufferLength = "Extensions.Exceptions.OffsetCountExceedBufferLength";
42+
43+
/// <summary>
44+
/// 索引和计数超出缓冲区长度的错误消息
45+
/// </summary>
46+
/// <remarks>
47+
/// 键名: Extensions.Exceptions.IndexCountExceedBufferLength
48+
/// 用途: 当尝试从缓冲区索引超出范围时使用
49+
/// 参数: {0} - 缓冲区长度, {1} - 索引, {2} - 计数
50+
/// </remarks>
51+
public const string IndexCountExceedBufferLength = "Extensions.Exceptions.IndexCountExceedBufferLength";
52+
53+
/// <summary>
54+
/// 缓冲区写入索引超出范围的错误消息
55+
/// </summary>
56+
/// <remarks>
57+
/// 键名: Extensions.Exceptions.BufferWriteOutOfRange
58+
/// 用途: 当尝试写入超出缓冲区范围的索引时使用
59+
/// 参数: {0} - 缓冲区长度, {1} - 写入位置
60+
/// </remarks>
61+
public const string BufferWriteOutOfRange = "Extensions.Exceptions.BufferWriteOutOfRange";
62+
63+
/// <summary>
64+
/// 字符串长度超出最大值的错误消息
65+
/// </summary>
66+
/// <remarks>
67+
/// 键名: Extensions.Exceptions.StringLengthExceedMaxValue
68+
/// 用途: 当字符串长度超过允许的最大值时使用
69+
/// 参数: {0} - 实际长度, {1} - 最大长度
70+
/// </remarks>
71+
public const string StringLengthExceedMaxValue = "Extensions.Exceptions.StringLengthExceedMaxValue";
72+
73+
/// <summary>
74+
/// 字节数组长度必须相等的错误消息
75+
/// </summary>
76+
/// <remarks>
77+
/// 键名: Extensions.Exceptions.ByteArrayLengthMustEqual
78+
/// 用途: 当两个字节数组长度不匹配时使用
79+
/// 参数: {0} - 第一个数组长度, {1} - 第二个数组长度
80+
/// </remarks>
81+
public const string ByteArrayLengthMustEqual = "Extensions.Exceptions.ByteArrayLengthMustEqual";
82+
83+
/// <summary>
84+
/// 列表不能为空的错误消息
85+
/// </summary>
86+
/// <remarks>
87+
/// 键名: Extensions.Exceptions.ListCannotBeEmpty
88+
/// 用途: 当需要非空列表但传入空列表时使用
89+
/// </remarks>
90+
public const string ListCannotBeEmpty = "Extensions.Exceptions.ListCannotBeEmpty";
91+
92+
/// <summary>
93+
/// 目标类型必须是接口类型的错误消息
94+
/// </summary>
95+
/// <remarks>
96+
/// 键名: Extensions.Exceptions.TargetTypeMustBeInterface
97+
/// 用途: 当类型转换的目标类型不是接口时使用
98+
/// 参数: {0} - 目标类型名称
99+
/// </remarks>
100+
public const string TargetTypeMustBeInterface = "Extensions.Exceptions.TargetTypeMustBeInterface";
101+
102+
/// <summary>
103+
/// 对象类型不匹配的错误消息
104+
/// </summary>
105+
/// <remarks>
106+
/// 键名: Extensions.Exceptions.ObjectTypeMismatch
107+
/// 用途: 当对象无法转换为目标类型时使用
108+
/// 参数: {0} - 对象类型, {1} - 目标类型
109+
/// </remarks>
110+
public const string ObjectTypeMismatch = "Extensions.Exceptions.ObjectTypeMismatch";
111+
}
112+
113+
/// <summary>
114+
/// 日志消息资源键
115+
/// </summary>
116+
public static class Logs
117+
{
118+
/// <summary>
119+
/// 集合处理完成的日志消息
120+
/// </summary>
121+
/// <remarks>
122+
/// 键名: Extensions.Logs.CollectionProcessed
123+
/// 用途: 记录集合处理操作完成
124+
/// 参数: {0} - 集合类型, {1} - 元素数量
125+
/// </remarks>
126+
public const string CollectionProcessed = "Extensions.Logs.CollectionProcessed";
127+
128+
/// <summary>
129+
/// 字节转换完成的日志消息
130+
/// </summary>
131+
/// <remarks>
132+
/// 键名: Extensions.Logs.ByteConversionCompleted
133+
/// 用途: 记录字节转换操作完成
134+
/// 参数: {0} - 源格式, {1} - 目标格式, {2} - 字节数
135+
/// </remarks>
136+
public const string ByteConversionCompleted = "Extensions.Logs.ByteConversionCompleted";
137+
138+
/// <summary>
139+
/// 类型转换完成的日志消息
140+
/// </summary>
141+
/// <remarks>
142+
/// 键名: Extensions.Logs.TypeConversionCompleted
143+
/// 用途: 记录类型转换操作完成
144+
/// 参数: {0} - 源类型, {1} - 目标类型
145+
/// </remarks>
146+
public const string TypeConversionCompleted = "Extensions.Logs.TypeConversionCompleted";
147+
}
148+
149+
/// <summary>
150+
/// 状态消息资源键
151+
/// </summary>
152+
public static class Status
153+
{
154+
/// <summary>
155+
/// 扩展方法初始化成功的状态消息
156+
/// </summary>
157+
/// <remarks>
158+
/// 键名: Extensions.Status.ExtensionsInitialized
159+
/// 用途: 表示扩展方法模块初始化完成
160+
/// 参数: {0} - 模块名称
161+
/// </remarks>
162+
public const string ExtensionsInitialized = "Extensions.Status.ExtensionsInitialized";
163+
164+
/// <summary>
165+
/// 缓冲区操作成功的状态消息
166+
/// </summary>
167+
/// <remarks>
168+
/// 键名: Extensions.Status.BufferOperationSuccessful
169+
/// 用途: 表示缓冲区操作成功完成
170+
/// 参数: {0} - 操作类型, {1} - 字节数
171+
/// </remarks>
172+
public const string BufferOperationSuccessful = "Extensions.Status.BufferOperationSuccessful";
173+
}
174+
175+
/// <summary>
176+
/// 验证消息资源键
177+
/// </summary>
178+
public static class Validation
179+
{
180+
/// <summary>
181+
/// 参数不能为null的验证消息
182+
/// </summary>
183+
/// <remarks>
184+
/// 键名: Extensions.Validation.NotNullRequired
185+
/// 用途: 当参数不允许为null但传入了null时使用
186+
/// 参数: {0} - 参数名称
187+
/// </remarks>
188+
public const string NotNullRequired = "Extensions.Validation.NotNullRequired";
189+
190+
/// <summary>
191+
/// 范围验证失败的验证消息
192+
/// </summary>
193+
/// <remarks>
194+
/// 键名: Extensions.Validation.RangeCheckFailed
195+
/// 用途: 当数值超出允许范围时使用
196+
/// 参数: {0} - 值, {1} - 最小值, {2} - 最大值
197+
/// </remarks>
198+
public const string RangeCheckFailed = "Extensions.Validation.RangeCheckFailed";
199+
200+
/// <summary>
201+
/// 类型验证失败的验证消息
202+
/// </summary>
203+
/// <remarks>
204+
/// 键名: Extensions.Validation.TypeCheckFailed
205+
/// 用途: 当对象不是期望类型时使用
206+
/// 参数: {0} - 实际类型, {1} - 期望类型
207+
/// </remarks>
208+
public const string TypeCheckFailed = "Extensions.Validation.TypeCheckFailed";
209+
}
210+
211+
/// <summary>
212+
/// 性能消息资源键
213+
/// </summary>
214+
public static class Performance
215+
{
216+
/// <summary>
217+
/// 操作执行时间的性能消息
218+
/// </summary>
219+
/// <remarks>
220+
/// 键名: Extensions.Performance.OperationExecutionTime
221+
/// 用途: 记录操作执行时间
222+
/// 参数: {0} - 操作名称, {1} - 执行时间(毫秒)
223+
/// </remarks>
224+
public const string OperationExecutionTime = "Extensions.Performance.OperationExecutionTime";
225+
226+
/// <summary>
227+
/// 内存使用情况的性能消息
228+
/// </summary>
229+
/// <remarks>
230+
/// 键名: Extensions.Performance.MemoryUsage
231+
/// 用途: 记录内存使用情况
232+
/// 参数: {0} - 操作名称, {1} - 内存使用量(字节)
233+
/// </remarks>
234+
public const string MemoryUsage = "Extensions.Performance.MemoryUsage";
235+
}
236+
}

0 commit comments

Comments
 (0)