在wwwroot文件夹添加汉化的JS文件
 - const translations = {
- "Failed to load API definition": "API定义加载失败",
- "Errors": "错误信息",
- "Hide": "隐藏",
- "Fetch error": "获取错误",
- "Internal Server Error": "服务器内部错误",
- "Operations": "操作",
- "Models": "模型",
- "Authorize": "授权",
- "Try it out": "试一试",
- "Select a definition": "选择分支",
- "No parameters": "无参数",
- "Name": "名称",
- "Request body": "请求体",
- "Details": "详细",
- "Cancel": "取消",
- "Server response": "服务器响应",
- "Response body": "响应体",
- "Response headers": "响应头",
- "Request duration": "请求时间",
- "Links": "链接",
- "No links": "无链接",
- "Execute": "执行",
- "Clear": "清除",
- "Response": "响应",
- "Headers": "请求头",
- "Parameters": "参数",
- "Description": "描述",
- "Responses": "响应",
- "Response Class": "响应类",
- "Response Body": "响应体",
- "Response Headers": "响应头",
- "Status Code": "状态码",
- "Example Value": "示例值",
- "Model": "模型",
- "Schema": "结构",
- "Download": "下载",
- "Copy": "复制",
- "Expand Operations": "展开操作",
- "Collapse Operations": "折叠操作",
- "Show/Hide": "显示/隐藏",
- "List Operations": "列表操作",
- "Full Screen": "全屏",
- "Request URL": "请求 URL",
- "Server Response": "服务器响应",
- "Code": "代码",
- "Response Content Type": "响应内容类型",
- "Produces": "生产",
- "Consumes": "消费",
- "Summary": "摘要",
- "Notes": "备注",
- "Implementation Notes": "实现备注",
- "Response Messages": "响应消息",
- "Response Model": "响应模型",
- "Request Body": "请求体",
- "Request Samples": "请求示例",
- "Response Samples": "响应示例",
- "Security": "安全",
- "OAuth2": "OAuth2",
- "API Key": "API 密钥",
- "Basic Auth": "基本认证",
- "Bearer Token": "Bearer 令牌",
- "No API Key": "没有 API 密钥",
- "No Authorization": "没有授权",
- "No Response": "没有响应",
- "No Content": "没有内容",
- "No Data": "没有数据",
- "No Example": "没有示例",
- "No Model": "没有模型",
- "No Schema": "没有结构",
- "No Description": "没有描述",
- "No Summary": "没有摘要",
- "No Notes": "没有备注",
- "No Implementation Notes": "没有实现备注",
- "No Response Messages": "没有响应消息",
- "No Response Model": "没有响应模型",
- "No Request Body": "没有请求体",
- "No Request Samples": "没有请求示例",
- "No Response Samples": "没有响应示例",
- "No Security": "没有安全",
- "No OAuth2": "没有 OAuth2",
- "No API Key": "没有 API 密钥",
- "No Basic Auth": "没有基本认证",
- "No Bearer Token": "没有 Bearer 令牌",
- "Warning: Deprecated": "警告:已过时",
- "Status": "状态",
- "Parameter": "参数",
- "Value": "值",
- "Parameter Type": "参数类型",
- "Data Type": "数据类型",
- "HTTP Status Code": "HTTP状态码",
- "Reason": "原因",
- "Hide Response": "隐藏响应",
- "Raw": "原始",
- "can't parse JSON. Raw result": "无法解析JSON. 原始结果",
- "Model Schema": "模型架构",
- "apply": "应用",
- "Username": "用户名",
- "Password": "密码",
- "Terms of service": "服务条款",
- "Created by": "创建者",
- "See more at": "查看更多:",
- "Contact the developer": "联系开发者",
- "api version": "api版本",
- "fetching resource": "正在获取资源",
- "fetching resource list": "正在获取资源列表",
- "Explore": "浏览",
- "Show Swagger Petstore Example Apis": "显示 Swagger Petstore 示例 Apis",
- "Can't read from server. It may not have the appropriate access-control-origin settings.": "无法从服务器读取。可能没有正确设置access-control-origin。",
- "Please specify the protocol for": "请指定协议:",
- "Can't read swagger JSON from": "无法读取swagger JSON于",
- "Finished Loading Resource Information. Rendering Swagger UI": "已加载资源信息。正在渲染Swagger UI",
- "Unable to read api": "无法读取api",
- "from path": "从路径",
- "server returned": "服务器返回"
- };
- window.SwaggerTranslator = {
- translate: function() {
- // 翻译标题和按钮文本
- $('h2, h3, h4, .btn, .opblock-summary-description, .opblock-summary-operation-id, .parameter__name, .parameter__type, .response-col_status, .response-col_description').each(function() {
- const text = $(this).text().trim();
- if (translations[text]) {
- $(this).text(translations[text]);
- }
- });
- // 翻译输入框和下拉框的 placeholder
- $('input[placeholder], select[data-placeholder]').each(function() {
- const placeholder = $(this).attr('placeholder') || $(this).data('placeholder');
- if (translations[placeholder]) {
- if ($(this).is('input')) {
- $(this).attr('placeholder', translations[placeholder]);
- } else {
- $(this).data('placeholder', translations[placeholder]);
- }
- }
- });
- // 翻译所有文本节点
- $('*').contents().filter(function() {
- return this.nodeType === Node.TEXT_NODE;
- }).each(function() {
- const text = this.nodeValue.trim();
- if (translations[text]) {
- this.nodeValue = translations[text];
- }
- });
- }
- };
- function waitForSwaggerUI() {
- if ($('.swagger-ui').length) {
- // Swagger UI 已加载,执行翻译
- window.SwaggerTranslator.translate();
-
- // 监听动态内容变化
- const observer = new MutationObserver(function(mutations) {
- window.SwaggerTranslator.translate();
- });
- observer.observe(document.querySelector('.swagger-ui'), {
- childList: true,
- subtree: true
- });
- } else {
- // 如果还没加载完,等待100ms后重试
- setTimeout(waitForSwaggerUI, 100);
- }
- }
- // 等待页面加载完成后开始检查 Swagger UI
- document.addEventListener('DOMContentLoaded', function() {
- waitForSwaggerUI();
- });
复制代码 然后配置- app.UseSwaggerUI(c =>
- {
- // 修改加载顺序和时机
- c.UseRequestInterceptor("(req) => { return req; }");
- c.UseResponseInterceptor("(res) => { return res; }");
- // 确保 jQuery 先加载
- c.InjectJavascript("/swagger/jquery-3.7.1.min.js");
- // 然后加载翻译脚本
- c.InjectJavascript("/swagger/lang/zh-cn.js");
-
- });
- // 启用静态文件中间件
- app.UseStaticFiles();
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |