Skip to content

Commit

Permalink
Add Content-Language header in localization middlewware (#13479)
Browse files Browse the repository at this point in the history
Fixes #11923
  • Loading branch information
pranavkm committed Aug 27, 2019
1 parent af0a204 commit e9179ba
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/Middleware/Localization/Localization.slnf
@@ -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"
]
}
}
Expand Up @@ -13,6 +13,7 @@ public static partial class ApplicationBuilderExtensions
public partial class 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 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 { } }
Expand Down
10 changes: 8 additions & 2 deletions src/Middleware/Localization/src/RequestLocalizationMiddleware.cs
@@ -1,5 +1,5 @@
// 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.
// 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;
using System.Collections.Generic;
Expand All @@ -13,6 +13,7 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Localization
{
Expand Down Expand Up @@ -146,6 +147,11 @@ public async Task Invoke(HttpContext context)

SetCurrentThreadCulture(requestCulture);

if (_options.ApplyCurrentCultureToResponseHeaders)
{
context.Response.Headers.Add(HeaderNames.ContentLanguage, requestCulture.UICulture.Name);
}

await _next(context);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Middleware/Localization/src/RequestLocalizationOptions.cs
Expand Up @@ -84,6 +84,11 @@ public RequestCulture DefaultRequestCulture
/// </example>
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>
/// The cultures supported by the application. The <see cref="RequestLocalizationMiddleware"/> will only set
/// the current request culture to an entry in this list.
Expand Down
@@ -1,5 +1,5 @@
// 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.
// 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;
using System.Net;
Expand All @@ -14,6 +14,15 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests
{
public class LocalizationTest
{
[Fact]
public Task Localization_ContentLanguageHeader()
{
return RunTest(
typeof(StartupContentLanguageHeader),
"ar-YE",
"True ar-YE");
}

[Fact]
public Task Localization_CustomCulture()
{
Expand Down
@@ -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);
});
}
}
}

0 comments on commit e9179ba

Please sign in to comment.