i18n: expand grade API validation translations
This commit is contained in:
1 parent
4e0f81c9a2
commit
1d75c9d4e8
2 files changed
+87
-24
No files matched your search
@@ -589,12 +589,12 @@ public sealed class GradesController(
|
||||
GradeScoreComponentType.GradeItemScore,
|
||||
scoreEntry.GradeItemId) &&
|
||||
existing.Score != scoreEntry.Score)
|
||||
return ConflictProblem($"已提交的“{gradeItem.Name}”成绩不能修改。");
|
||||
return ConflictProblem(GradeMessage($"已提交的“{gradeItem.Name}”成绩不能修改。", $"Submitted grades for “{gradeItem.Name}” cannot be changed."));
|
||||
if (gradeItem.SourceType ==
|
||||
GradeItemSourceType.ExperimentSummary &&
|
||||
existing.Score != scoreEntry.Score)
|
||||
return ConflictProblem(
|
||||
$"“{gradeItem.Name}”来自实验成绩快照,不能手工修改;请重新导入实验成绩。");
|
||||
GradeMessage($"“{gradeItem.Name}”来自实验成绩快照,不能手工修改;请重新导入实验成绩。", $"“{gradeItem.Name}” comes from a lab grade snapshot and cannot be edited manually; import lab grades again."));
|
||||
existing.Score = scoreEntry.Score;
|
||||
}
|
||||
}
|
||||
@@ -651,11 +651,11 @@ public sealed class GradesController(
|
||||
if (request.RegularScore &&
|
||||
record.ExamStatus == GradeExamStatus.Normal &&
|
||||
sheet.RegularWeight > 0 && !record.RegularScore.HasValue)
|
||||
return ConflictProblem($"{record.Student?.Name ?? "所选学生"}缺少平时成绩。");
|
||||
return ConflictProblem(GradeMessage($"{record.Student?.Name ?? "所选学生"}缺少平时成绩。", $"{record.Student?.Name ?? "The selected student"} is missing a coursework grade."));
|
||||
if (request.FinalScore &&
|
||||
record.ExamStatus == GradeExamStatus.Normal &&
|
||||
sheet.FinalWeight > 0 && !record.FinalScore.HasValue)
|
||||
return ConflictProblem($"{record.Student?.Name ?? "所选学生"}缺少期末成绩。");
|
||||
return ConflictProblem(GradeMessage($"{record.Student?.Name ?? "所选学生"}缺少期末成绩。", $"{record.Student?.Name ?? "The selected student"} is missing a final exam grade."));
|
||||
foreach (var gradeItemId in gradeItemIds)
|
||||
{
|
||||
var gradeItem = sheet.Items.Single(item => item.Id == gradeItemId);
|
||||
@@ -663,12 +663,12 @@ public sealed class GradesController(
|
||||
if (record.ExamStatus == GradeExamStatus.Normal &&
|
||||
gradeItem.Weight > 0 && !score.Score.HasValue)
|
||||
return ConflictProblem(
|
||||
$"{record.Student?.Name ?? "所选学生"}缺少“{gradeItem.Name}”成绩。");
|
||||
GradeMessage($"{record.Student?.Name ?? "所选学生"}缺少“{gradeItem.Name}”成绩。", $"{record.Student?.Name ?? "The selected student"} is missing a grade for “{gradeItem.Name}”."));
|
||||
}
|
||||
if (request.FinalEvaluation &&
|
||||
record.ExamStatus == GradeExamStatus.Normal &&
|
||||
!GradeCalculator.IsResultValid(sheet.EvaluationMethod, record.Result))
|
||||
return ConflictProblem($"{record.Student?.Name ?? "所选学生"}缺少最终评价。");
|
||||
return ConflictProblem(GradeMessage($"{record.Student?.Name ?? "所选学生"}缺少最终评价。", $"{record.Student?.Name ?? "The selected student"} is missing a final evaluation."));
|
||||
}
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
@@ -735,11 +735,11 @@ public sealed class GradesController(
|
||||
if (sheet.EvaluationMethod == GradeEvaluationMethod.Percentage &&
|
||||
record.ExamStatus == GradeExamStatus.Normal &&
|
||||
!record.TotalScore.HasValue)
|
||||
return ConflictProblem($"{record.Student?.Name ?? "所选学生"}仍缺少必填成绩分项。");
|
||||
return ConflictProblem(GradeMessage($"{record.Student?.Name ?? "所选学生"}仍缺少必填成绩分项。", $"{record.Student?.Name ?? "The selected student"} is missing a required grade component."));
|
||||
if (sheet.EvaluationMethod != GradeEvaluationMethod.Percentage &&
|
||||
record.ExamStatus == GradeExamStatus.Normal &&
|
||||
!GradeCalculator.IsResultValid(sheet.EvaluationMethod, record.Result))
|
||||
return ConflictProblem($"{record.Student?.Name ?? "所选学生"}仍未完成最终评价。");
|
||||
return ConflictProblem(GradeMessage($"{record.Student?.Name ?? "所选学生"}仍未完成最终评价。", $"{record.Student?.Name ?? "The selected student"} has no final evaluation."));
|
||||
}
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
@@ -1109,9 +1109,9 @@ public sealed class GradesController(
|
||||
var scope = currentUserDataScope.Current;
|
||||
if (scope.Scope == DataScope.College &&
|
||||
sheet.TeachingTask!.Course!.CollegeId != scope.RestrictedCollegeId)
|
||||
return ConflictProblem(
|
||||
$"该课程属于{sheet.TeachingTask.Course.College!.Name}," +
|
||||
"您只能审核本学院课程的成绩单。");
|
||||
return ConflictProblem(GradeMessage(
|
||||
$"该课程属于{sheet.TeachingTask.Course.College!.Name},您只能审核本学院课程的成绩单。",
|
||||
$"This course belongs to {sheet.TeachingTask.Course.College!.Name}; you can only review grade sheets for your own college."));
|
||||
|
||||
if (sheet.Status != GradeSheetStatus.Submitted)
|
||||
return ConflictProblem("只有待审核成绩单可以通过审核。");
|
||||
@@ -1153,9 +1153,9 @@ public sealed class GradesController(
|
||||
var scope = currentUserDataScope.Current;
|
||||
if (scope.Scope == DataScope.College &&
|
||||
sheet.TeachingTask!.Course!.CollegeId != scope.RestrictedCollegeId)
|
||||
return ConflictProblem(
|
||||
$"该课程属于{sheet.TeachingTask.Course.College!.Name}," +
|
||||
"您只能审核本学院课程的成绩单。");
|
||||
return ConflictProblem(GradeMessage(
|
||||
$"该课程属于{sheet.TeachingTask.Course.College!.Name},您只能审核本学院课程的成绩单。",
|
||||
$"This course belongs to {sheet.TeachingTask.Course.College!.Name}; you can only review grade sheets for your own college."));
|
||||
|
||||
if (sheet.Status != GradeSheetStatus.Submitted)
|
||||
return ConflictProblem("只有待审核成绩单可以退回。");
|
||||
@@ -1394,12 +1394,12 @@ public sealed class GradesController(
|
||||
var studentNumber = row["学号"]?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(studentNumber))
|
||||
{
|
||||
errors.Add($"第 {row.RowNumber} 行:学号不能为空。");
|
||||
errors.Add(GradeMessage($"第 {row.RowNumber} 行:学号不能为空。", $"Row {row.RowNumber}: student number is required."));
|
||||
continue;
|
||||
}
|
||||
if (!studentMap.TryGetValue(studentNumber, out var record))
|
||||
{
|
||||
errors.Add($"第 {row.RowNumber} 行:学号“{studentNumber}”不在本成绩单中。");
|
||||
errors.Add(GradeMessage($"第 {row.RowNumber} 行:学号“{studentNumber}”不在本成绩单中。", $"Row {row.RowNumber}: student number “{studentNumber}” is not on this grade sheet."));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1435,7 +1435,7 @@ public sealed class GradesController(
|
||||
else if (examStatusText == "免修") examStatus = GradeExamStatus.Exempt;
|
||||
else if (examStatusText != "正常")
|
||||
{
|
||||
errors.Add($"第 {row.RowNumber} 行:考试状态“{examStatusText}”无效,请填写正常、缺考、缓考或免修。");
|
||||
errors.Add(GradeMessage($"第 {row.RowNumber} 行:考试状态“{examStatusText}”无效,请填写正常、缺考、缓考或免修。", $"Row {row.RowNumber}: exam status “{examStatusText}” is invalid. Enter 正常, 缺考, 缓考 or 免修."));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -1462,7 +1462,7 @@ public sealed class GradesController(
|
||||
record.ItemScores.First(existing =>
|
||||
existing.GradeItemId == itemScore.GradeItemId).Score != itemScore.Score))
|
||||
{
|
||||
errors.Add($"第 {row.RowNumber} 行:包含已提交或已发布的成绩,不能覆盖修改。");
|
||||
errors.Add(GradeMessage($"第 {row.RowNumber} 行:包含已提交或已发布的成绩,不能覆盖修改。", $"Row {row.RowNumber}: submitted or published grades cannot be overwritten."));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1953,16 +1953,42 @@ public sealed class GradesController(
|
||||
["成绩单提交后不能继续修改。"] = "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.",
|
||||
["合格制成绩必须选择“合格”或“不合格”。"] = "For pass/fail grading, select Pass or Fail.",
|
||||
["等级制成绩必须选择 A、B、C、D 或 E。"] = "For letter grading, select A, B, C, D or E.",
|
||||
["百分制成绩不能填写等级结果。"] = "Letter results cannot be entered for percentage grading.",
|
||||
["已有分项提交的学生不能修改考试状态。"] = "Exam status cannot be changed after a component is submitted.",
|
||||
["已提交的平时成绩不能修改。"] = "Submitted coursework grades cannot be changed.",
|
||||
["已提交的期末成绩不能修改。"] = "Submitted final exam grades cannot be changed.",
|
||||
["已提交的最终评价不能修改。"] = "Submitted final evaluations cannot be changed.",
|
||||
["已进入审核发布流程的学生成绩备注不能修改。"] = "Notes cannot be changed after grades enter the review and publication process.",
|
||||
["分项成绩必须在 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.",
|
||||
["只有录入中或已退回的成绩单可以分批提交。"] = "Only draft or returned grade sheets can be submitted in batches.",
|
||||
["百分制成绩没有单独的最终评价分项。"] = "Percentage grading has no separate final evaluation component.",
|
||||
["当前成绩单状态不能提交部分学生审核。"] = "Selected student grades cannot be submitted in the current state.",
|
||||
["所选学生中包含已发布成绩,不能重复提交。"] = "Selected students include published grades that cannot be resubmitted.",
|
||||
["所选学生已有成绩批次正在审核或已经发布。"] = "A selected student's grade batch is already under review or published.",
|
||||
["您只能审核本学院课程的成绩批次。"] = "You can only review grade batches for your college's courses.",
|
||||
["只有待审核的部分成绩批次可以通过审核。"] = "Only partial grade batches awaiting review can be approved.",
|
||||
["只有待审核的部分成绩批次可以退回。"] = "Only partial grade batches awaiting review can be returned.",
|
||||
["只有审核通过的部分成绩批次可以发布。"] = "Only approved partial grade batches can be published.",
|
||||
["只有草稿或已退回成绩单可以提交。"] = "Only draft or returned grade sheets can be submitted.",
|
||||
["仍有部分学生成绩批次正在审核或等待发布,请处理完成后再提交整单。"] = "Some student grade batches are still under review or awaiting publication. Complete them before submitting the full sheet.",
|
||||
["仍有学生缺少必填成绩分项,请补充后再提交。"] = "Some students are missing required grade components. Complete them before submitting.",
|
||||
["仍有学生未完成最终评价,请先使用定级工具或逐一选择结果。"] = "Some students lack a final evaluation. Use the grading tool or select each result first.",
|
||||
["只有草稿或已退回成绩单可以使用定级工具。"] = "The grading tool is only available for draft or returned grade sheets.",
|
||||
["百分制成绩不需要定级工具。"] = "Percentage grading does not require the grading tool.",
|
||||
["只有草稿或已退回成绩单可以导入成绩。"] = "Grades can only be imported into draft or returned grade sheets.",
|
||||
["成绩单已存在,或关联学生与教学班已发生变化。"] = "The grade sheet already exists, or its students or teaching class have changed.",
|
||||
["退回时必须填写修改意见。"] = "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.",
|
||||
["该教学任务尚无已发布的实验项目成绩,无法导入。"] = "There are no published lab project grades to import for this teaching task.",
|
||||
["请选择当前成绩单中的有效分项。"] = "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.",
|
||||
@@ -1971,6 +1997,9 @@ public sealed class GradesController(
|
||||
["ABCDE 分数线必须满足 A > B > C > D。"] = "ABCDE thresholds must satisfy A > B > C > D."
|
||||
};
|
||||
|
||||
private static string GradeMessage(string chinese, string english) =>
|
||||
CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en" ? english : chinese;
|
||||
|
||||
private static string LocalizeGradeError(string detail) =>
|
||||
CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en" && EnglishErrors.TryGetValue(detail, out var translated)
|
||||
? translated : detail;
|
||||
@@ -1990,7 +2019,7 @@ public sealed class GradesController(
|
||||
foreach (var error in errors.Take(50))
|
||||
ModelState.AddModelError("file", error);
|
||||
if (errors.Count > 50)
|
||||
ModelState.AddModelError("file", $"另有 {errors.Count - 50} 条错误未显示。");
|
||||
ModelState.AddModelError("file", GradeMessage($"另有 {errors.Count - 50} 条错误未显示。", $"Another {errors.Count - 50} errors are not shown."));
|
||||
return base.ValidationProblem(ModelState);
|
||||
}
|
||||
|
||||
@@ -2011,9 +2040,9 @@ public sealed class GradesController(
|
||||
result >= minimum &&
|
||||
result <= maximum)
|
||||
return result;
|
||||
errors.Add(
|
||||
$"第 {row.RowNumber} 行:" +
|
||||
$"“{header}”请填写 {minimum:0}—{maximum:0} 的数值或留空。");
|
||||
errors.Add(GradeMessage(
|
||||
$"第 {row.RowNumber} 行:“{header}”请填写 {minimum:0}—{maximum:0} 的数值或留空。",
|
||||
$"Row {row.RowNumber}: enter a number from {minimum:0} to {maximum:0} for “{header}”, or leave it blank."));
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -2034,13 +2063,13 @@ public sealed class GradesController(
|
||||
{
|
||||
if (value is "合格" or "通过") return GradeResult.Pass;
|
||||
if (value is "不合格" or "未通过") return GradeResult.Fail;
|
||||
errors.Add($"第 {row} 行:最终评价请填写合格或不合格。");
|
||||
errors.Add(GradeMessage($"第 {row} 行:最终评价请填写合格或不合格。", $"Row {row}: enter 合格 or 不合格 for the final evaluation."));
|
||||
return null;
|
||||
}
|
||||
if (Enum.TryParse<GradeResult>(value, true, out var result) &&
|
||||
result is >= GradeResult.A and <= GradeResult.E)
|
||||
return result;
|
||||
errors.Add($"第 {row} 行:最终评价请填写 A、B、C、D 或 E。");
|
||||
errors.Add(GradeMessage($"第 {row} 行:最终评价请填写 A、B、C、D 或 E。", $"Row {row}: enter A, B, C, D or E for the final evaluation."));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using Jiaowu.Api.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Jiaowu.Api.Tests;
|
||||
|
||||
@@ -22,4 +23,37 @@ public sealed class GradeErrorLocalizationTests
|
||||
}
|
||||
finally { CultureInfo.CurrentUICulture = culture; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParameterizedMessagesPreserveNamesAndFollowCulture()
|
||||
{
|
||||
var method = typeof(GradesController).GetMethod("GradeMessage", BindingFlags.NonPublic | BindingFlags.Static)!;
|
||||
var original = CultureInfo.CurrentUICulture;
|
||||
try
|
||||
{
|
||||
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
|
||||
Assert.Equal("Row 12: 张三 is missing a grade.", method.Invoke(null, ["第 12 行:张三缺少成绩。", "Row 12: 张三 is missing a grade."]));
|
||||
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
|
||||
Assert.Equal("第 12 行:张三缺少成绩。", method.Invoke(null, ["第 12 行:张三缺少成绩。", "Row 12: 张三 is missing a grade."]));
|
||||
}
|
||||
finally { CultureInfo.CurrentUICulture = original; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConflictResponseKeepsStatusAndLocalizesProblemDetails()
|
||||
{
|
||||
var controller = new GradesController(null!, null!);
|
||||
var method = typeof(GradesController).GetMethod("ConflictProblem", BindingFlags.NonPublic | BindingFlags.Instance)!;
|
||||
var original = CultureInfo.CurrentUICulture;
|
||||
try
|
||||
{
|
||||
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
|
||||
var response = Assert.IsType<ConflictObjectResult>(method.Invoke(controller, ["该教学班已经建立成绩单。"]));
|
||||
var details = Assert.IsType<ProblemDetails>(response.Value);
|
||||
Assert.Equal(409, response.StatusCode);
|
||||
Assert.Equal(409, details.Status);
|
||||
Assert.Equal("A grade sheet already exists for this teaching class.", details.Detail);
|
||||
}
|
||||
finally { CultureInfo.CurrentUICulture = original; }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user