mirror of https://github.com/abpframework/abp.git
4 changed files with 17 additions and 76 deletions
@ -1,35 +1,33 @@ |
|||||
using System; |
using System; |
||||
|
using System.Text.RegularExpressions; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using Microsoft.Extensions.DependencyInjection; |
|
||||
using Microsoft.Extensions.Options; |
|
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
namespace Volo.Abp.AspNetCore.Mvc.Localization |
namespace Volo.Abp.AspNetCore.Mvc.Localization |
||||
{ |
{ |
||||
public class AbpAspNetCoreMvcQueryStringCultureReplacement : IQueryStringCultureReplacement, ITransientDependency |
public class AbpAspNetCoreMvcQueryStringCultureReplacement : IQueryStringCultureReplacement, ITransientDependency |
||||
{ |
{ |
||||
protected AbpQueryStringCultureReplacementOptions Options { get; } |
public virtual Task ReplaceAsync(QueryStringCultureReplacementContext context) |
||||
protected IServiceProvider ServiceProvider { get; } |
|
||||
|
|
||||
public AbpAspNetCoreMvcQueryStringCultureReplacement( |
|
||||
IOptions<AbpQueryStringCultureReplacementOptions> queryStringCultureReplacementOptions, |
|
||||
IServiceProvider serviceProvider) |
|
||||
{ |
|
||||
ServiceProvider = serviceProvider; |
|
||||
Options = queryStringCultureReplacementOptions.Value; |
|
||||
} |
|
||||
|
|
||||
public virtual async Task ReplaceAsync(QueryStringCultureReplacementContext context) |
|
||||
{ |
{ |
||||
using (var scope = ServiceProvider.CreateScope()) |
if (!string.IsNullOrWhiteSpace(context.ReturnUrl)) |
||||
{ |
{ |
||||
foreach (var provider in Options.QueryStringCultureReplacementProviders) |
if (context.ReturnUrl.Contains("culture=", StringComparison.OrdinalIgnoreCase) && |
||||
|
context.ReturnUrl.Contains("ui-Culture=", StringComparison.OrdinalIgnoreCase)) |
||||
{ |
{ |
||||
// ReSharper disable once PossibleNullReferenceException
|
context.ReturnUrl = Regex.Replace( |
||||
await (scope.ServiceProvider.GetRequiredService(provider) as IQueryStringCultureReplacementProvider) |
context.ReturnUrl, |
||||
.ReplaceAsync(context); |
"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; |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,33 +0,0 @@ |
|||||
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; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,9 +0,0 @@ |
|||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace Volo.Abp.AspNetCore.Mvc.Localization |
|
||||
{ |
|
||||
public interface IQueryStringCultureReplacementProvider |
|
||||
{ |
|
||||
Task ReplaceAsync(QueryStringCultureReplacementContext context); |
|
||||
} |
|
||||
} |
|
||||
@ -1,15 +0,0 @@ |
|||||
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