feat: add Chinese localization for Swagger UI
Some checks failed
CI / Build & Test (push) Failing after 40s

Replace swagger-initializer.js with custom Chinese version that
translates common UI strings and sets document title.
This commit is contained in:
MengStack Dev 2026-10-03 04:20:47 +08:00
parent 493947c987
commit ec352306f1

View File

@ -23,6 +23,15 @@ func SwaggerHandler() gin.HandlerFunc {
}
isCSS := strings.HasSuffix(relPath, "swagger-ui.css")
isInitializer := strings.HasSuffix(relPath, "swagger-initializer.js")
if isInitializer {
c.Header("Content-Type", "application/javascript; charset=utf-8")
c.Writer.WriteHeader(200)
c.Writer.Write([]byte(chineseInitializer))
c.Request.URL.Path = rawPath
return
}
if isCSS {
crw := &captureResponseWriter{
@ -83,6 +92,74 @@ func (w *captureResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
func (w *captureResponseWriter) CloseNotify() <-chan bool { return w.ginWriter.CloseNotify() }
func (w *captureResponseWriter) Flush() {}
const chineseInitializer = `
window.onload = function() {
window.ui = SwaggerUIBundle({
url: "./doc.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
validatorUrl: null,
defaultModelsExpandDepth: 1,
defaultModelExpandDepth: 1,
docExpansion: "list",
filter: true,
showExtensions: true,
showCommonExtensions: true,
withCredentials: false,
persistAuthorization: true,
tryItOutEnabled: false,
requestSnippetsEnabled: true,
displayRequestDuration: true,
syntaxHighlight: {
activate: true,
theme: "agate"
}
});
setTimeout(function() {
const translations = {
'Available authorizations': '可用授权',
'Close': '关闭',
'Scopes for': '作用域',
'Authorize': '授权',
'Cancel': '取消',
'Authorized': '已授权',
'Logout': '登出',
'Edit': '编辑',
'Select a definition': '选择定义',
'Search': '搜索',
'Top': '顶部',
'Parameters': '参数',
'Responses': '响应',
'Try it out': '试一试',
'Clear': '清除',
'Download': '下载',
'Show/Hide': '显示/隐藏',
'List Operations': '列表操作',
'Expand Operations': '展开操作',
'Models': '模型'
};
document.querySelectorAll('span, button, div, label').forEach(function(el) {
const text = el.textContent.trim();
if (translations[text]) {
el.textContent = translations[text];
}
});
document.title = 'MengStack API 文档';
}, 500);
};
`
const brandingCSS = `
/* MengStack API branding — replace Swagger SmartBear logo */
.topbar {