温馨提示×

如何在Debian上使用Swagger进行API文档国际化

小樊
42
2025-11-15 02:27:25
栏目: 智能运维

在Debian上使用Swagger进行API文档国际化,可以按照以下步骤进行:

1. 安装必要的软件包

首先,确保你的Debian系统已经安装了Swagger和相关的工具。你可以使用以下命令来安装Swagger:

sudo apt update
sudo apt install swagger-ui-express

2. 创建Swagger配置文件

创建一个Swagger配置文件(通常是swagger.json),并定义你的API文档。为了支持国际化,你需要在配置文件中添加多语言支持。

{
  "swagger": "2.0",
  "info": {
    "title": "My API",
    "description": "API documentation with internationalization",
    "version": "1.0.0"
  },
  "host": "api.example.com",
  "basePath": "/v1",
  "schemes": [
    "https"
  ],
  "paths": {
    "/items": {
      "get": {
        "summary": "Get items",
        "description": "This endpoint returns a list of items",
        "responses": {
          "200": {
            "description": "A JSON array of items",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Item"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "Item": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        }
      }
    }
  },
  "tags": [
    {
      "name": "items",
      "description": "Operations pertaining to items"
    }
  ],
  "externalDocs": {
    "description": "Find more info here",
    "url": "https://swagger.io/tools/swagger-ui/"
  }
}

3. 添加多语言支持

为了支持多语言,你可以在Swagger配置文件中使用description字段来提供不同语言的描述。例如:

{
  "paths": {
    "/items": {
      "get": {
        "summary": "Get items",
        "description": {
          "en": "This endpoint returns a list of items",
          "zh": "此端点返回项目列表"
        },
        "responses": {
          "200": {
            "description": {
              "en": "A JSON array of items",
              "zh": "一个项目的JSON数组"
            },
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Item"
              }
            }
          }
        }
      }
    }
  }
}

4. 使用Swagger UI Express

使用Swagger UI Express来展示你的API文档,并支持多语言。你可以使用以下命令来启动Swagger UI Express:

npx swagger-ui-express --swagger-file ./swagger.json --port 3000

5. 配置多语言支持

Swagger UI Express本身不直接支持多语言,但你可以使用第三方库(如i18next)来实现多语言支持。首先,安装i18nexti18next-http-middleware

npm install i18next i18next-http-middleware

然后,在你的Express应用中配置i18next

const express = require('express');
const swaggerUi = require('swagger-ui-express');
const i18n = require('i18next');
const Backend = require('i18next-fs-backend');

i18n.use(Backend).init({
  fallbackLng: 'en',
  debug: true,
  interpolation: {
    escapeValue: false
  }
}, (err, t) => {
  if (err) return console.error(err);

  const app = express();

  app.use('/api-docs', swaggerUi.serve, swaggerUi.setup({
    swaggerOptions: { url: '/swagger.json' }
  }));

  app.listen(3000, () => {
    console.log('Server is running on port 3000');
  });
});

6. 更新Swagger配置文件

更新你的Swagger配置文件,将多语言描述放在单独的文件中,并在Swagger配置文件中引用这些文件。例如:

{
  "swagger": "2.0",
  "info": {
    "title": "My API",
    "description": "API documentation with internationalization",
    "version": "1.0.0"
  },
  "host": "api.example.com",
  "basePath": "/v1",
  "schemes": [
    "https"
  ],
  "paths": {
    "/items": {
      "get": {
        "summary": "Get items",
        "description": {
          "en": "This endpoint returns a list of items",
          "zh": "此端点返回项目列表"
        },
        "responses": {
          "200": {
            "description": {
              "en": "A JSON array of items",
              "zh": "一个项目的JSON数组"
            },
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Item"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "Item": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        }
      }
    }
  },
  "tags": [
    {
      "name": "items",
      "description": "Operations pertaining to items"
    }
  ],
  "externalDocs": {
    "description": "Find more info here",
    "url": "https://swagger.io/tools/swagger-ui/"
  }
}

通过以上步骤,你可以在Debian上使用Swagger进行API文档国际化。

0