This repository was archived by the owner on Apr 17, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
src/NET6CustomLibrary/MultiLanguage Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments