|
|
|
@ -1,29 +1,35 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace Volo.Abp.AspNetCore.Mvc.Localization
|
|
|
|
|
{
|
|
|
|
|
public class AbpAspNetCoreMvcQueryStringCultureReplacement : IQueryStringCultureReplacement, ITransientDependency
|
|
|
|
|
{
|
|
|
|
|
public virtual Task<string> ReplaceAsync(QueryStringCultureReplacementContext context)
|
|
|
|
|
protected AbpQueryStringCultureReplacementOptions Options { get; }
|
|
|
|
|
protected IServiceProvider ServiceProvider { get; }
|
|
|
|
|
|
|
|
|
|
public AbpAspNetCoreMvcQueryStringCultureReplacement(
|
|
|
|
|
IOptions<AbpQueryStringCultureReplacementOptions> queryStringCultureReplacementOptions,
|
|
|
|
|
IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
var returnUrl = context.ReturnUrl;
|
|
|
|
|
ServiceProvider = serviceProvider;
|
|
|
|
|
Options = queryStringCultureReplacementOptions.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(returnUrl))
|
|
|
|
|
public virtual async Task ReplaceAsync(QueryStringCultureReplacementContext context)
|
|
|
|
|
{
|
|
|
|
|
using (var scope = ServiceProvider.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
if (returnUrl.Contains("culture=", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
foreach (var provider in Options.QueryStringCultureReplacementProviders)
|
|
|
|
|
{
|
|
|
|
|
returnUrl = Regex.Replace(returnUrl, "culture=[A-Za-z-]+?&", $"culture={context.RequestCulture.Culture}&", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
if (returnUrl.Contains("ui-Culture=", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
returnUrl = Regex.Replace(returnUrl, "ui-culture=[A-Za-z-]+?$", $"ui-culture={context.RequestCulture.UICulture}",RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
// ReSharper disable once PossibleNullReferenceException
|
|
|
|
|
await (scope.ServiceProvider.GetRequiredService(provider) as IQueryStringCultureReplacementProvider)
|
|
|
|
|
.ReplaceAsync(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.FromResult<string>(returnUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|