mirror of https://github.com/abpframework/abp.git
7 changed files with 89 additions and 23 deletions
@ -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); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,33 @@ |
|||
using System; |
|||
using System.Text.RegularExpressions; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Localization |
|||
{ |
|||
public class DefaultQueryStringCultureReplacementProvider : IQueryStringCultureReplacementProvider, ITransientDependency |
|||
{ |
|||
public Task ReplaceAsync(QueryStringCultureReplacementContext context) |
|||
{ |
|||
if (!string.IsNullOrWhiteSpace(context.ReturnUrl)) |
|||
{ |
|||
if (context.ReturnUrl.Contains("culture=", StringComparison.OrdinalIgnoreCase) && |
|||
context.ReturnUrl.Contains("ui-Culture=", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
context.ReturnUrl = Regex.Replace( |
|||
context.ReturnUrl, |
|||
"culture=[A-Za-z-]+?&", |
|||
$"culture={context.RequestCulture.Culture}&", |
|||
RegexOptions.Compiled | RegexOptions.IgnoreCase); |
|||
|
|||
context.ReturnUrl = Regex.Replace( |
|||
context.ReturnUrl, "ui-culture=[A-Za-z-]+?$", |
|||
$"ui-culture={context.RequestCulture.UICulture}", |
|||
RegexOptions.Compiled | RegexOptions.IgnoreCase); |
|||
} |
|||
} |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Localization |
|||
{ |
|||
public interface IQueryStringCultureReplacementProvider |
|||
{ |
|||
Task ReplaceAsync(QueryStringCultureReplacementContext context); |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using Volo.Abp.Collections; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Localization |
|||
{ |
|||
public class AbpQueryStringCultureReplacementOptions |
|||
{ |
|||
public ITypeList<IQueryStringCultureReplacementProvider> QueryStringCultureReplacementProviders { get; } |
|||
|
|||
public AbpQueryStringCultureReplacementOptions() |
|||
{ |
|||
QueryStringCultureReplacementProviders = new TypeList<IQueryStringCultureReplacementProvider>(); |
|||
QueryStringCultureReplacementProviders.Add<DefaultQueryStringCultureReplacementProvider>(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue