Cursor rule
.cursor/rules/i18n.mdcCodeSpirit 多语言国际化规范 - 资源文件、本地化、前后端多语言支持
Cursor rules
Quality
50/100
Scores the file, not the repository.Length
346 words
16 headings · 10 code blocksRepository
56
— · pushed 134 days agoLast changed
3 days ago
First indexed 3 days ago.1234567891011121314# 多语言国际化规范1516## 支持语言17- **简体中文** (zh-CN) - 默认语言18- **英文** (en)1920## 资源文件命名规范2122### 共享资源23```24CodeSpirit.Localization/Resources/25 ├── SharedResources.cs # 资源类26 ├── Shared.resx # 中文(默认)27 ├── Shared.en.resx # 英文28 ├── Display.resx / Display.en.resx # 显示文本资源29 ├── Validation.resx / Validation.en.resx # 验证消息资源30```3132### 服务特定资源33```34CodeSpirit.ExamApi/Resources/35 ├── ExamDisplayResources.cs # 资源类36 ├── ExamDisplay.resx # 中文(默认)37 └── ExamDisplay.en.resx # 英文38```3940## 资源键命名规范41- **通用**: `Common.{Key}` (如 `Common.Save`, `Common.Delete`)42- **错误**: `Errors.{Key}` (如 `Errors.NotFound`, `Errors.InvalidInput`)43- **验证**: `Validation.{Rule}` (如 `Validation.Required`, `Validation.StringLengthMax`)44- **DTO描述**: `{EntityName}.{PropertyName}.Description` (如 `Question.Content.Description`)4546示例资源文件(Display.resx):47```xml48<data name="Common.Save" xml:space="preserve">49 <value>保存</value>50</data>51<data name="Common.Delete" xml:space="preserve">52 <value>删除</value>53</data>54<data name="Question.Content.Description" xml:space="preserve">55 <value>请输入题目的具体内容</value>56</data>57```5859## Controller 中使用本地化6061```csharp62using CodeSpirit.Localization.Resources;63using Microsoft.Extensions.Localization;6465public class QuestionsController : ApiControllerBase66{67 private readonly IStringLocalizer<SharedResources> _localizer;6869 public QuestionsController(IStringLocalizer<SharedResources> localizer)70 {71 _localizer = localizer;72 }7374 [HttpPost]75 public async Task<ActionResult<ApiResponse>> Create(CreateQuestionDto dto)76 {77 await _service.CreateAsync(dto);78 return SuccessResponse(message: _localizer["Common.Save"].Value);79 }80}81```8283## DTO 验证特性多语言8485### Display 特性86```csharp87using CodeSpirit.Localization.Resources;8889public class CreateQuestionDto90{91 [Display(Name = "Content", ResourceType = typeof(DisplayResources))]92 [Required(ErrorMessageResourceType = typeof(ValidationResources),93 ErrorMessageResourceName = "Required")]94 [StringLength(2000,95 ErrorMessageResourceType = typeof(ValidationResources),96 ErrorMessageResourceName = "StringLengthMax")]97 public string Content { get; set; } = string.Empty;9899 [Display(Name = "Score", ResourceType = typeof(DisplayResources))]100 [Range(0, 100,101 ErrorMessageResourceType = typeof(ValidationResources),102 ErrorMessageResourceName = "Range")]103 public decimal Score { get; set; }104}105```106107### 描述信息多语言108```csharp109using CodeSpirit.Localization.Attributes;110111public class CreateQuestionDto112{113 [Display(Name = "Content", ResourceType = typeof(DisplayResources))]114 [LocalizedDescription("Question.Content.Description", typeof(ExamDisplayResources))]115 public string Content { get; set; } = string.Empty;116}117```118119## 本地化异常120121```csharp122// 使用资源键123throw new BusinessException("Errors.InvalidStartTime");124125// 带参数(使用占位符 {0}, {1})126throw new ValidationException("Errors.NotFound", resourceId);127```128129资源文件定义:130```xml131<data name="Errors.NotFound" xml:space="preserve">132 <value>资源 {0} 未找到</value>133</data>134```135136## 枚举多语言137```csharp138public enum QuestionType139{140 [Display(Name = "SingleChoice", ResourceType = typeof(DisplayResources))]141 SingleChoice = 1,142143 [Display(Name = "MultipleChoice", ResourceType = typeof(DisplayResources))]144 MultipleChoice = 2,145146 [Display(Name = "TrueFalse", ResourceType = typeof(DisplayResources))]147 TrueFalse = 3148}149```150151资源文件:152```xml153<data name="SingleChoice" xml:space="preserve">154 <value>单选题</value>155</data>156<data name="MultipleChoice" xml:space="preserve">157 <value>多选题</value>158</data>159```160161## 注意事项162163### 强制要求(必须遵守)164- ✅ 所有面向用户的文本必须支持多语言165- ✅ 资源文件必须同时提供中文和英文版本166- ✅ 验证消息、异常消息全部使用本地化167168### 最佳实践(推荐遵守)169- 💡 新开发的 DTO、枚举、控制器应使用多语言写法170- 💡 逐步迁移现有硬编码文本到资源文件171- 💡 定期检查资源文件是否有缺失的翻译172173### 迁移指南174对于现有代码,建议采用渐进式迁移:1751. 新功能必须使用多语言写法1762. 修改现有功能时,同步迁移为多语言写法1773. 定期批量迁移高频使用的文本178179
Also in xin-lai/CodeSpirit
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| xin-lai/CodeSpirit.cursor/rules/js.mdc · 56 | Cursor rules | apidocs | 46/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/ai-development.mdc · 56 | Cursor rules | api | 46/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/all.mdc · 56 | Cursor rules | testing-strategyapi | 50/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/amis-cards.mdc · 56 | Cursor rules | no sections | 54/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/api-design.mdc · 56 | Cursor rules | no sections | 54/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/controller.mdc · 56 | Cursor rules | api | 54/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/cs.mdc · 56 | Cursor rules | no sections | 25/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/csproj.mdc · 56 | Cursor rules | api | 54/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/css.mdc · 56 | Cursor rules | ui | 54/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/database.mdc · 56 | Cursor rules | no sections | 74/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/dependency-injection.mdc · 56 | Cursor rules | api | 54/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/dto.mdc · 56 | Cursor rules | no sections | 50/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/enum.mdc · 56 | Cursor rules | no sections | 50/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/naming-conventions.mdc · 56 | Cursor rules | no sections | 50/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/package-management.mdc · 56 | Cursor rules | no sections | 74/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/performance.mdc · 56 | Cursor rules | no sections | 54/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/project-structure.mdc · 56 | Cursor rules | api | 54/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/security.mdc · 56 | Cursor rules | database | 54/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/service.mdc · 56 | Cursor rules | no sections | 50/100 | 3 days ago | |
| xin-lai/CodeSpirit.cursor/rules/startup-framework.mdc · 56 | Cursor rules | api | 54/100 | 3 days ago |
Diff against .cursor/rules/js.mdc Diff against .cursor/rules/ai-development.mdc Diff against .cursor/rules/all.mdc Diff against .cursor/rules/amis-cards.mdc Diff against .cursor/rules/api-design.mdc Diff against .cursor/rules/controller.mdc Diff against .cursor/rules/cs.mdc Diff against .cursor/rules/csproj.mdc Diff against .cursor/rules/css.mdc Diff against .cursor/rules/database.mdc Diff against .cursor/rules/dependency-injection.mdc Diff against .cursor/rules/dto.mdc Diff against .cursor/rules/enum.mdc Diff against .cursor/rules/naming-conventions.mdc Diff against .cursor/rules/package-management.mdc Diff against .cursor/rules/performance.mdc Diff against .cursor/rules/project-structure.mdc Diff against .cursor/rules/security.mdc Diff against .cursor/rules/service.mdc Diff against .cursor/rules/startup-framework.mdc
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| hiromaily/go-crypto-wallet.cursor/rules/typescript.mdc · 126 | Cursor rules | setupbuildtestlint-format+6 | 100/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/10-hermes-api.mdc · 45 | Cursor rules | testlint-formatstylearch+5 | 100/100 | 3 days ago | |
| markstev/mark-starter.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+6 | 99/100 | 3 days ago | |
| Allymahmoud/case-intake-platform.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| deifos/clipmira-subtitles.cursor/rules/frontend.mdc · 1 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| dodgecfr/combatfilms-webapp.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| langflow-ai/langflow.cursor/rules/docs_development.mdc · 153k | Cursor rules | setupbuildtestlint-format+7 | 97/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/20-hermes-api-tests.mdc · 45 | Cursor rules | teststyletesting-strategysecurity+3 | 97/100 | 3 days ago |
