Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

NopSeoUrlCultureProvider.cs

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Nop.Core.Domain.Localization;
using Nop.Core.Infrastructure;
using Nop.Services.Localization;

namespace Nop.Web.Framework.Globalization;

/// 
/// Determines the culture information for a request via the URL
/// 
public partial class NopSeoUrlCultureProvider : RequestCultureProvider
{
    /// 
    /// Implements the provider to determine the culture of the given request
    /// 
    /// HttpContext for the request
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains ProviderCultureResult
    /// 
    public override async Task DetermineProviderCultureResult(HttpContext httpContext)
    {
        var localizationSettings = EngineContext.Current.Resolve();

        if (!localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
            return await NullProviderCultureResult;

        //localized URLs are enabled, so try to get language from the requested page URL
        var (isLocalized, language) = await httpContext.Request.Path.Value.IsLocalizedUrlAsync(httpContext.Request.PathBase, false);
        if (!isLocalized || language is null)
            return await NullProviderCultureResult;

        return new ProviderCultureResult(language.LanguageCulture);
    }
}