Add Content-Language header in localization middlewware (#13479)
Fixes https://github.com/aspnet/AspNetCore/issues/11923
This commit is contained in:
parent
af0a2048a2
commit
e9179bacd7
|
|
@ -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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
public partial class RequestLocalizationOptions
|
public partial class RequestLocalizationOptions
|
||||||
{
|
{
|
||||||
public RequestLocalizationOptions() { }
|
public RequestLocalizationOptions() { }
|
||||||
|
public bool ApplyCurrentCultureToResponseHeaders { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
|
||||||
public Microsoft.AspNetCore.Localization.RequestCulture DefaultRequestCulture { get { throw null; } set { } }
|
public Microsoft.AspNetCore.Localization.RequestCulture DefaultRequestCulture { get { throw null; } set { } }
|
||||||
public bool FallBackToParentCultures { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
|
public bool FallBackToParentCultures { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
|
||||||
public bool FallBackToParentUICultures { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
|
public bool FallBackToParentUICultures { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
@ -13,6 +13,7 @@ using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
|
using Microsoft.Net.Http.Headers;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Localization
|
namespace Microsoft.AspNetCore.Localization
|
||||||
{
|
{
|
||||||
|
|
@ -146,6 +147,11 @@ namespace Microsoft.AspNetCore.Localization
|
||||||
|
|
||||||
SetCurrentThreadCulture(requestCulture);
|
SetCurrentThreadCulture(requestCulture);
|
||||||
|
|
||||||
|
if (_options.ApplyCurrentCultureToResponseHeaders)
|
||||||
|
{
|
||||||
|
context.Response.Headers.Add(HeaderNames.ContentLanguage, requestCulture.UICulture.Name);
|
||||||
|
}
|
||||||
|
|
||||||
await _next(context);
|
await _next(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,11 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
/// </example>
|
/// </example>
|
||||||
public bool FallBackToParentUICultures { get; set; } = true;
|
public bool FallBackToParentUICultures { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value that determines if <see cref="CultureInfo.CurrentUICulture" /> is applied to the response <c>Content-Language</c> header.
|
||||||
|
/// </summary>
|
||||||
|
public bool ApplyCurrentCultureToResponseHeaders { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The cultures supported by the application. The <see cref="RequestLocalizationMiddleware"/> will only set
|
/// The cultures supported by the application. The <see cref="RequestLocalizationMiddleware"/> will only set
|
||||||
/// the current request culture to an entry in this list.
|
/// the current request culture to an entry in this list.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
|
@ -14,6 +14,15 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests
|
||||||
{
|
{
|
||||||
public class LocalizationTest
|
public class LocalizationTest
|
||||||
{
|
{
|
||||||
|
[Fact]
|
||||||
|
public Task Localization_ContentLanguageHeader()
|
||||||
|
{
|
||||||
|
return RunTest(
|
||||||
|
typeof(StartupContentLanguageHeader),
|
||||||
|
"ar-YE",
|
||||||
|
"True ar-YE");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public Task Localization_CustomCulture()
|
public Task Localization_CustomCulture()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue