Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Middleware/Localization/Localization.slnf
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"
]
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
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);
});
}
}
}