ASP.NET Core Swagger 漢化
在wwwroot文件夾添加漢化的JS文件

const translations = {
"Failed to load API definition": "API定義加載失敗",
"Errors": "錯(cuò)誤信息",
"Hide": "隱藏",
"Fetch error": "獲取錯(cuò)誤",
"Internal Server Error": "服務(wù)器內(nèi)部錯(cuò)誤",
"Operations": "操作",
"Models": "模型",
"Authorize": "授權(quán)",
"Try it out": "試一試",
"Select a definition": "選擇分支",
"No parameters": "無參數(shù)",
"Name": "名稱",
"Request body": "請(qǐng)求體",
"Details": "詳細(xì)",
"Cancel": "取消",
"Server response": "服務(wù)器響應(yīng)",
"Response body": "響應(yīng)體",
"Response headers": "響應(yīng)頭",
"Request duration": "請(qǐng)求時(shí)間",
"Links": "鏈接",
"No links": "無鏈接",
"Execute": "執(zhí)行",
"Clear": "清除",
"Response": "響應(yīng)",
"Headers": "請(qǐng)求頭",
"Parameters": "參數(shù)",
"Description": "描述",
"Responses": "響應(yīng)",
"Response Class": "響應(yīng)類",
"Response Body": "響應(yīng)體",
"Response Headers": "響應(yīng)頭",
"Status Code": "狀態(tài)碼",
"Example Value": "示例值",
"Model": "模型",
"Schema": "結(jié)構(gòu)",
"Download": "下載",
"Copy": "復(fù)制",
"Expand Operations": "展開操作",
"Collapse Operations": "折疊操作",
"Show/Hide": "顯示/隱藏",
"List Operations": "列表操作",
"Full Screen": "全屏",
"Request URL": "請(qǐng)求 URL",
"Server Response": "服務(wù)器響應(yīng)",
"Code": "代碼",
"Response Content Type": "響應(yīng)內(nèi)容類型",
"Produces": "生產(chǎn)",
"Consumes": "消費(fèi)",
"Summary": "摘要",
"Notes": "備注",
"Implementation Notes": "實(shí)現(xiàn)備注",
"Response Messages": "響應(yīng)消息",
"Response Model": "響應(yīng)模型",
"Request Body": "請(qǐng)求體",
"Request Samples": "請(qǐng)求示例",
"Response Samples": "響應(yīng)示例",
"Security": "安全",
"OAuth2": "OAuth2",
"API Key": "API 密鑰",
"Basic Auth": "基本認(rèn)證",
"Bearer Token": "Bearer 令牌",
"No API Key": "沒有 API 密鑰",
"No Authorization": "沒有授權(quán)",
"No Response": "沒有響應(yīng)",
"No Content": "沒有內(nèi)容",
"No Data": "沒有數(shù)據(jù)",
"No Example": "沒有示例",
"No Model": "沒有模型",
"No Schema": "沒有結(jié)構(gòu)",
"No Description": "沒有描述",
"No Summary": "沒有摘要",
"No Notes": "沒有備注",
"No Implementation Notes": "沒有實(shí)現(xiàn)備注",
"No Response Messages": "沒有響應(yīng)消息",
"No Response Model": "沒有響應(yīng)模型",
"No Request Body": "沒有請(qǐng)求體",
"No Request Samples": "沒有請(qǐng)求示例",
"No Response Samples": "沒有響應(yīng)示例",
"No Security": "沒有安全",
"No OAuth2": "沒有 OAuth2",
"No API Key": "沒有 API 密鑰",
"No Basic Auth": "沒有基本認(rèn)證",
"No Bearer Token": "沒有 Bearer 令牌",
"Warning: Deprecated": "警告:已過時(shí)",
"Status": "狀態(tài)",
"Parameter": "參數(shù)",
"Value": "值",
"Parameter Type": "參數(shù)類型",
"Data Type": "數(shù)據(jù)類型",
"HTTP Status Code": "HTTP狀態(tài)碼",
"Reason": "原因",
"Hide Response": "隱藏響應(yīng)",
"Raw": "原始",
"can't parse JSON. Raw result": "無法解析JSON. 原始結(jié)果",
"Model Schema": "模型架構(gòu)",
"apply": "應(yīng)用",
"Username": "用戶名",
"Password": "密碼",
"Terms of service": "服務(wù)條款",
"Created by": "創(chuàng)建者",
"See more at": "查看更多:",
"Contact the developer": "聯(lián)系開發(fā)者",
"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.": "無法從服務(wù)器讀取。可能沒有正確設(shè)置access-control-origin。",
"Please specify the protocol for": "請(qǐng)指定協(xié)議:",
"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": "服務(wù)器返回"
};
window.SwaggerTranslator = {
translate: function() {
// 翻譯標(biāo)題和按鈕文本
$('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]);
}
}
});
// 翻譯所有文本節(jié)點(diǎn)
$('*').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 已加載,執(zhí)行翻譯
window.SwaggerTranslator.translate();
// 監(jiān)聽動(dòng)態(tài)內(nèi)容變化
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 =>
{
// 修改加載順序和時(shí)機(jī)
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");
});
// 啟用靜態(tài)文件中間件
app.UseStaticFiles();

浙公網(wǎng)安備 33010602011771號(hào)