Files
Umbraco-CMS/src/Umbraco.Web.Common/Localization/UmbracoPublishedContentCultureProvider.cs
Mole bf41c2eeaa Netcore: Align namespaces (#9801)
* Rename Umbraco.Core namespace to Umbraco.Cms.Core

* Move extension methods in core project to Umbraco.Extensions

* Move extension methods in core project to Umbraco.Extensions

* Rename Umbraco.Examine namespace to Umbraco.Cms.Examine

* Move examine extensions to Umbraco.Extensions namespace

* Reflect changed namespaces in Builder and fix unit tests

* Adjust namespace in Umbraco.ModelsBuilder.Embedded

* Adjust namespace in Umbraco.Persistence.SqlCe

* Adjust namespace in Umbraco.PublishedCache.NuCache

* Align namespaces in Umbraco.Web.BackOffice

* Align namespaces in Umbraco.Web.Common

* Ensure that SqlCeSupport is still enabled after changing the namespace

* Align namespaces in Umbraco.Web.Website

* Align namespaces in Umbraco.Web.UI.NetCore

* Align namespaces in Umbraco.Tests.Common

* Align namespaces in Umbraco.Tests.UnitTests

* Align namespaces in Umbraco.Tests.Integration

* Fix errors caused by changed namespaces

* Fix integration tests

* Undo the Umbraco.Examine.Lucene namespace change

This breaks integration tests on linux, since the namespace wont exists there because it's only used on windows.

* Fix merge

* Fix Merge
2021-02-18 11:06:02 +01:00

63 lines
2.8 KiB
C#

using System;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Primitives;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Web.Common.Routing;
namespace Umbraco.Cms.Web.Common.Localization
{
/// <summary>
/// Sets the request culture to the culture of the <see cref="IPublishedRequest"/> if one is found in the request
/// </summary>
public class UmbracoPublishedContentCultureProvider : RequestCultureProvider
{
private readonly RequestLocalizationOptions _localizationOptions;
private readonly object _locker = new object();
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoPublishedContentCultureProvider"/> class.
/// </summary>
public UmbracoPublishedContentCultureProvider(RequestLocalizationOptions localizationOptions) => _localizationOptions = localizationOptions;
/// <inheritdoc/>
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
UmbracoRouteValues routeValues = httpContext.Features.Get<UmbracoRouteValues>();
if (routeValues != null)
{
string culture = routeValues.PublishedRequest?.Culture;
if (culture != null)
{
lock (_locker)
{
// We need to dynamically change the supported cultures since we won't ever know what languages are used since
// they are dynamic within Umbraco.
// This code to check existence is borrowed from aspnetcore to avoid creating a CultureInfo
// https://github.com/dotnet/aspnetcore/blob/b795ac3546eb3e2f47a01a64feb3020794ca33bb/src/Middleware/Localization/src/RequestLocalizationMiddleware.cs#L165
CultureInfo existingCulture = _localizationOptions.SupportedCultures.FirstOrDefault(supportedCulture =>
StringSegment.Equals(supportedCulture.Name, culture, StringComparison.OrdinalIgnoreCase));
if (existingCulture == null)
{
// add this as a supporting culture
var ci = CultureInfo.GetCultureInfo(culture);
_localizationOptions.SupportedCultures.Add(ci);
_localizationOptions.SupportedUICultures.Add(ci);
}
}
return Task.FromResult(new ProviderCultureResult(culture));
}
}
return NullProviderCultureResult;
}
}
}