Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 1916693

Browse files
committed
Aggiunta funzionalità multi language
1 parent 8e5cb02 commit 1916693

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.Options;
3+
using Microsoft.Net.Http.Headers;
4+
using Microsoft.OpenApi.Any;
5+
using Microsoft.OpenApi.Models;
6+
using Swashbuckle.AspNetCore.SwaggerGen;
7+
8+
namespace NET6CustomLibrary.MultiLanguage;
9+
10+
public class CultureAwareOperationFilter : IOperationFilter
11+
{
12+
private readonly List<IOpenApiAny> supportedLanguages;
13+
14+
public CultureAwareOperationFilter(IOptions<RequestLocalizationOptions> requestLocalizationOptions)
15+
{
16+
supportedLanguages = requestLocalizationOptions.Value
17+
.SupportedCultures?.Select(c => new OpenApiString(c.TwoLetterISOLanguageName))
18+
.Cast<IOpenApiAny>()
19+
.ToList();
20+
}
21+
22+
public void Apply(OpenApiOperation operation, OperationFilterContext context)
23+
{
24+
if (supportedLanguages.Count > 1)
25+
{
26+
operation.Parameters ??= new List<OpenApiParameter>();
27+
operation.Parameters.Add(new OpenApiParameter
28+
{
29+
Name = HeaderNames.AcceptLanguage,
30+
In = ParameterLocation.Header,
31+
Required = false,
32+
Schema = new OpenApiSchema
33+
{
34+
Type = "String",
35+
Enum = supportedLanguages,
36+
Default = supportedLanguages.First()
37+
}
38+
});
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)