Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Content-Language header in localization middlewware (#13479)
Fixes #11923
- Loading branch information
Showing
6 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"solution": { | ||
"path": "..\\Middleware.sln", | ||
"projects": [ | ||
"Localization.Routing\\src\\Microsoft.AspNetCore.Localization.Routing.csproj", | ||
"Localization.Routing\\test\\Microsoft.AspNetCore.Localization.Routing.Tests.csproj", | ||
"Localization\\sample\\LocalizationSample.csproj", | ||
"Localization\\src\\Microsoft.AspNetCore.Localization.csproj", | ||
"Localization\\test\\FunctionalTests\\Microsoft.AspNetCore.Localization.FunctionalTests.csproj", | ||
"Localization\\test\\UnitTests\\Microsoft.AspNetCore.Localization.Tests.csproj", | ||
"Localization\\testassets\\LocalizationWebsite\\LocalizationWebsite.csproj", | ||
"Localization\\testassets\\ResourcesClassLibraryNoAttribute\\ResourcesClassLibraryNoAttribute.csproj", | ||
"Localization\\testassets\\ResourcesClassLibraryWithAttribute\\ResourcesClassLibraryWithAttribute.csproj" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Middleware/Localization/testassets/LocalizationWebsite/StartupContentLanguageHeader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Localization; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace LocalizationWebsite | ||
{ | ||
public class StartupContentLanguageHeader | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddLocalization(); | ||
} | ||
|
||
public void Configure( | ||
IApplicationBuilder app) | ||
{ | ||
app.UseRequestLocalization(new RequestLocalizationOptions | ||
{ | ||
DefaultRequestCulture = new RequestCulture("en-US"), | ||
SupportedCultures = new List<CultureInfo>() | ||
{ | ||
new CultureInfo("ar-YE") | ||
}, | ||
SupportedUICultures = new List<CultureInfo>() | ||
{ | ||
new CultureInfo("ar-YE") | ||
}, | ||
ApplyCurrentCultureToResponseHeaders = true | ||
}); | ||
|
||
app.Run(async (context) => | ||
{ | ||
var hasContentLanguageHeader = context.Response.Headers.ContainsKey(HeaderNames.ContentLanguage); | ||
var contentLanguage = context.Response.Headers[HeaderNames.ContentLanguage].ToString(); | ||
await context.Response.WriteAsync(hasContentLanguageHeader.ToString()); | ||
await context.Response.WriteAsync(" "); | ||
await context.Response.WriteAsync(contentLanguage); | ||
}); | ||
} | ||
} | ||
} |