温馨提示×

Linux Swagger API文档如何进行国际化支持

小樊
70
2025-06-20 21:09:57
栏目: 智能运维

在Linux环境下实现Swagger API文档的国际化支持,可以通过以下几种方法:

1. 使用Swagger UI的多语言支持

Swagger UI本身支持多语言界面,可以通过以下步骤实现:

  • 安装多语言版本的Swagger UI

    npm install swagger-ui-dist --save
    
  • 在HTML中配置

    <script src="node_modules/swagger-ui-dist/swagger-ui-bundle.js"></script>
    <script src="node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js"></script>
    <script>
    window.onload = function() {
      const ui = SwaggerUIBundle({
        url: "your-api-spec.json",
        dom_id: '#swagger-ui',
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout",
        // 设置语言
        locale: "zh-CN" // 可替换为其他语言代码如 en, fr, ja等
      });
    }
    </script>
    

2. API描述信息的国际化

  • 使用多份OpenAPI规范文件: 创建不同语言的API规范文件(如api_spec_en.json, api_spec_zh.json, api_spec_ja.json),在每个文件中使用相应语言的描述信息。

  • 动态生成方式: 在Node.js应用中,根据请求头动态返回不同语言的描述:

    const express = require('express');
    const app = express();
    const swaggerJsdoc = require('swagger-jsdoc');
    app.get('/api-docs', (req, res) => {
      const acceptLanguage = req.headers['accept-language'] || 'en';
      const options = {
        definition: {
          openapi: '3.0.0',
          info: {
            title: getLocalizedTitle(acceptLanguage),
            description: getLocalizedDescription(acceptLanguage),
            version: '1.0.0',
          },
        },
        apis: ['./routes/*.js'],
      };
      const specs = swaggerJsdoc(options);
      res.json(specs);
    });
    
    function getLocalizedTitle(lang) {
      const titles = {
        'en': 'API Documentation',
        'zh': 'API文档',
        'ja': 'APIドキュメント'
      };
      return titles[lang] || titles['en'];
    }
    
    function getLocalizedDescription(lang) {
      const descriptions = {
        'en': 'This is the API documentation.',
        'zh': '这是API文档。',
        'ja': 'これはAPIドキュメントです。'
      };
      return descriptions[lang] || descriptions['en'];
    }
    
  • 使用i18n工具处理描述文本: 集成i18n库(如i18n),配置i18n:

    const i18n = require('i18n');
    i18n.configure({
      locales: ['en', 'zh', 'ja'],
      directory: __dirname + '/locales',
      defaultLocale: 'en'
    });
    
    const swaggerOptions = {
      info: {
        title: i18n.__('API Documentation'),
        description: i18n.__('API description goes here')
      }
    };
    

3. 使用API网关实现国际化

在API网关层(如Nginx, Kong)实现内容协商:

location /api-docs {
  if ($http_accept_language ~* "^zh") {
    rewrite ^ /api-docs/zh last;
  }
  if ($http_accept_language ~* "^ja") {
    rewrite ^ /api-docs/ja last;
  }
  rewrite ^ /api-docs/en last;
}

4. 使用Swagger插件实现

对于Java Spring Boot项目,可以使用springdoc-openapi的国际化支持:

@Bean
public OpenAPI customOpenAPI(LocaleResolver localeResolver) {
    return new OpenAPI()
        .info(new Info()
            .title(getLocalizedTitle(localeResolver))
            .description(getLocalizedDescription(localeResolver)));
}

private String getLocalizedTitle(LocaleResolver localeResolver) {
    Locale locale = localeResolver.resolveLocale(null);
    ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
    return bundle.getString("api.title");
}

5. 使用支持多语言的API文档生成框架

推荐使用Springdoc,这是一个功能强大的开源API文档工具,基于Spring构建,并提供多语言支持。只需在Swagger中定义API接口,Springdoc即可自动生成支持多种语言的详细API文档。

通过以上方法,您可以在Linux环境下轻松实现Swagger API文档的国际化需求。

0