i18n: localize common grade API errors

This commit is contained in:
biss committed 2026-11-06 21:04:07 +08:00
1 parent 4f6c61a8ea
commit 4e0f81c9a2
2 files changed
+63 -3

No files matched your search

+38 -3
View File
@@ -1942,11 +1942,46 @@ public sealed class GradesController(
}
}
private static readonly Dictionary<string, string> EnglishErrors = new()
{
["平时、期末与所有分项的比例必须合计 100%。"] = "Coursework, final exam and component weights must total 100%.",
["只有已发布或已结课的教学班可以建立成绩单。"] = "Only published or completed teaching classes can have a grade sheet.",
["该教学班已经建立成绩单。"] = "A grade sheet already exists for this teaching class.",
["教学班当前没有有效学生,无法建立成绩单。"] = "The teaching class has no eligible students for a grade sheet.",
["当前状态不能修改成绩构成。"] = "The grade structure cannot be changed in the current state.",
["已有成绩分项完成分批提交,不能再修改成绩构成。"] = "The grade structure cannot be changed after components have been submitted.",
["成绩单提交后不能继续修改。"] = "The grade sheet cannot be changed after submission.",
["包含无效或重复的成绩记录。"] = "The request contains invalid or duplicate grade records.",
["成绩必须在 0—100 分之间。"] = "Grades must be between 0 and 100.",
["分项成绩必须在 0—100 分之间。"] = "Component grades must be between 0 and 100.",
["请选择不重复的学生成绩记录。"] = "Select distinct student grade records.",
["包含不属于当前成绩单的学生记录。"] = "Some student records do not belong to this grade sheet.",
["包含无效或重复的成绩分项。"] = "The request contains invalid or duplicate grade components.",
["请至少选择一个要提交的成绩分项。"] = "Select at least one grade component to submit.",
["退回时必须填写修改意见。"] = "Enter revision comments when returning grades.",
["只有审核通过的成绩单可以发布。"] = "Only approved grade sheets can be published.",
["只有待审核成绩单可以通过审核。"] = "Only grade sheets awaiting review can be approved.",
["只有待审核成绩单可以退回。"] = "Only grade sheets awaiting review can be returned.",
["成绩单提交后不能导入实验成绩。"] = "Lab grades cannot be imported after submission.",
["请选择当前成绩单中的有效分项。"] = "Select a valid component from the current grade sheet.",
["成绩单没有学生记录。"] = "The grade sheet has no student records.",
["当前账号未关联有效学生档案。"] = "The current account is not linked to a valid student record.",
["Excel 中没有可导入的成绩数据。"] = "The Excel file contains no grades to import.",
["定级分数线必须在 0—100 分之间。"] = "Grade thresholds must be between 0 and 100.",
["ABCDE 分数线必须满足 A > B > C > D。"] = "ABCDE thresholds must satisfy A > B > C > D."
};
private static string LocalizeGradeError(string detail) =>
CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en" && EnglishErrors.TryGetValue(detail, out var translated)
? translated : detail;
private ActionResult ValidationProblem(string detail) => base.ValidationProblem(detail: LocalizeGradeError(detail));
private ActionResult ConflictProblem(string detail) =>
Conflict(new ProblemDetails
{
Title = "无法完成成绩操作",
Detail = detail,
Title = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en" ? "Grade operation could not be completed" : "无法完成成绩操作",
Detail = LocalizeGradeError(detail),
Status = StatusCodes.Status409Conflict
});
@@ -1956,7 +1991,7 @@ public sealed class GradesController(
ModelState.AddModelError("file", error);
if (errors.Count > 50)
ModelState.AddModelError("file", $"另有 {errors.Count - 50} 条错误未显示。");
return ValidationProblem(ModelState);
return base.ValidationProblem(ModelState);
}
private static decimal? ParseOptionalDecimal(
@@ -0,0 +1,25 @@
using System.Globalization;
using System.Reflection;
using Jiaowu.Api.Controllers;
namespace Jiaowu.Api.Tests;
public sealed class GradeErrorLocalizationTests
{
[Fact]
public void GradeErrorsFollowRequestCultureAndPreserveUnknownDetails()
{
var method = typeof(GradesController).GetMethod("LocalizeGradeError", BindingFlags.NonPublic | BindingFlags.Static)!;
const string original = "成绩必须在 0—100 分之间。";
var culture = CultureInfo.CurrentUICulture;
try
{
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
Assert.Equal("Grades must be between 0 and 100.", method.Invoke(null, [original]));
Assert.Equal("custom detail", method.Invoke(null, ["custom detail"]));
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
Assert.Equal(original, method.Invoke(null, [original]));
}
finally { CultureInfo.CurrentUICulture = culture; }
}
}