我有一个.NET Core Web API,它使用OpenAPI/Swagger来生成API端点.页面是否可以支持不同的语言(例如,我希望我的页面是法语、英语和西班牙语).

我在我的创业公司里有这个

在我的服务中

services.AddSwaggerGen();

在我的应用程序构建器中

app.UseSwagger();
app.UseSwaggerUI(options =>
{
   var path = string.IsNullOrWhiteSpace(options.RoutePrefix) ? "." : "..";
   options.SwaggerEndpoint($"{path}/swagger/v1/swagger.json", "API v1");
});

我希望生成的页面能够有整个页面翻译.到目前为止,只有英语在翻译中.

我已try 将路由添加到控制器,如此链接

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/640

它确实会生成不同的文档,但实际上没有翻译

推荐答案

你可以在你的应用程序中为多种不同的语言生成多个swagger文档.下面是一个简单的例子:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1-en", new OpenApiInfo { Title = "API (English)", Version = "v1" });
    c.SwaggerDoc("v1-fr", new OpenApiInfo { Title = "API (French)", Version = "v1" });
    c.SwaggerDoc("v1-es", new OpenApiInfo { Title = "API (Spanish)", Version = "v1" });
});

您可以做的另一件事可能是定制Swagger用户界面以允许语言 Select .下面是一个基本的例子,说明如何做到这一点:

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1-en/swagger.json", "English");
    c.SwaggerEndpoint("/swagger/v1-fr/swagger.json", "French");
    c.SwaggerEndpoint("/swagger/v1-es/swagger.json", "Spanish");
});

有关更多信息,请参阅Swagger Github Documentation,还有其他选项,如翻译文本、使用浏览器语言设置等.

编辑(基于 comments ):

如果您想更改静态内容(按钮、标签等)的语言,那么您将不得不做更多的事情.

  1. You can create a custom swagger ui and translate that content. This will involve hosting your own swagger ui and replacing the static text with translations. You can use JS 或 any client-side library to switch between languages dynamically.

  1. use the browsers language settings to serve the swagger ui in the user's preferred language. you can detect the browser's language and load the c或responding translated swagger ui.

  1. This is the answer I think you are looking f或. You can translate texts in the swagger ui dynamically using JS. Here is a basic example:

Include a JavaScript Library f或 Translation:

使用像i18Next这样的库来处理转换. 添加转换文件:

Create JSON files containing the translations f或 all static texts in the Swagger UI. Initialize the Translation Library:

Initialize the translation library and load the appropriate language file based on the user’s selection 或 browser’s language setting. Translate Texts Dynamically:

使用翻译库将Swagger UI中的静态文本替换为它们的翻译.

使用i18Next的基本示例:

<!-- Include i18next and i18nextXHRBackend libraries -->
<script src="https://unpkg.com/i18next@11.6.0/i18next.min.js"></script>
<script src="https://unpkg.com/i18next-xhr-backend@3.2.2/i18nextXHRBackend.min.js"></script>

<script type="text/javascript">
    i18next
        .use(i18nextXHRBackend)
        .init({
            lng: 'en', // set the default language
            fallbackLng: 'en',
            backend: {
                loadPath: '/locales/{{lng}}/translation.json' // path to the translation files
            }
        }, function(err, t) {
            // translate the texts
            document.getElementById('title').innerHTML = i18next.t('title');
            // ... translate other texts
        });
</script>

In this example, the i18next library is used to translate texts dynamically. You need to create translation files (e.g., en.json, fr.json, es.json) containing the translations f或 all static texts in the Swagger UI.

Csharp相关问答推荐

如何使用FastEndpoints和.NET 8 WebAppliationBuilder进行集成测试?

Microsoft.AspNetCore.Mvc. Controller Base.用户:属性或索引器Controller Base.用户无法分配给--它是只读的

无法将blob发送到Azure -缺少HTTP标头异常

找不到网址:https://localhost:7002/Category/Add?区域= Admin.为什么我的URL是这样生成的?area = Admin而不是/Admin/

在多对多关系上不删除实体

通过条件列表删除/更新EF Core 7中的实体的有效方法

在C#中使用类中的对象值

如何测量在使用UTF8而不是C#中的UTF16编码字符串时内存使用量的增长

C# CompareTo()和Compare()可以返回除-1和1以外的整数吗?

JsonSchema.Net删除假阳性判断结果

是否由DI容器自动处理由ActivatorUilties.CreateInstance()创建的服务?

如何在.NET Maui中将事件与MVVM一起使用?

WinUI 3中DoubleCollection崩溃应用程序类型的依赖属性

源代码生成器:CS8795分部方法';Class1.GetS2(字符串)';必须有实现部分,因为它有可访问性修饰符?

如何在Akka.NET中重新启动执行元时清除邮箱

C#LINQ子字符串

如何使用.NET Aspire从Blazor应用程序与GRPC API通信?

如何正确处置所有动态控件?

如何在绑定到数据库的datagridview中向上或向下移动行

用于分钟和秒验证的MudTextfield的正则表达式掩码