Skip to content

Commit

Permalink
Merge pull request #7011 from abpframework/maliming/AbpRequestLocaliz…
Browse files Browse the repository at this point in the history
…ationOptions

Introduce AbpRequestLocalizationOptions
  • Loading branch information
hikalkan authored Jan 7, 2021
2 parents 79575ff + 1d761eb commit 9c1e24e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public AbpRequestLocalizationMiddleware(

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{

var middleware = new RequestLocalizationMiddleware(
next,
new OptionsWrapper<RequestLocalizationOptions>(await _requestLocalizationOptionsProvider.GetLocalizationOptionsAsync()), _loggerFactory
new OptionsWrapper<RequestLocalizationOptions>(await _requestLocalizationOptionsProvider.GetLocalizationOptionsAsync()),
_loggerFactory
);

await middleware.Invoke(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AspNetCore.RequestLocalization
{
public class AbpRequestLocalizationOptions
{
public List<Func<IServiceProvider, RequestLocalizationOptions, Task>> RequestLocalizationOptionConfigurators { get; }

public AbpRequestLocalizationOptions()
{
RequestLocalizationOptionConfigurators = new List<Func<IServiceProvider, RequestLocalizationOptions, Task>>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Localization;
using Volo.Abp.Settings;
Expand Down Expand Up @@ -67,6 +68,13 @@ public async Task<RequestLocalizationOptions> GetLocalizationOptionsAsync()
.ToArray()
};

foreach (var configurator in serviceScope.ServiceProvider
.GetRequiredService<IOptions<AbpRequestLocalizationOptions>>()
.Value.RequestLocalizationOptionConfigurators)
{
await configurator(serviceScope.ServiceProvider, options);
}

_optionsAction?.Invoke(options);
_requestLocalizationOptions = options;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Globalization;
using Microsoft.AspNetCore.Mvc;

namespace Volo.Abp.AspNetCore.Mvc.Localization
{
[Route("api/LocalizationTestController")]
public class LocalizationTestController : AbpController
{
[HttpGet]
public string Culture()
{
return CultureInfo.CurrentCulture.Name + ":" + CultureInfo.CurrentUICulture.Name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.RequestLocalization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
using Shouldly;
using Xunit;

namespace Volo.Abp.AspNetCore.Mvc.Localization
{
public class LocalizationTestController_Tests : AspNetCoreMvcTestBase
{
class TestRequestCultureProvider : RequestCultureProvider
{
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
return Task.FromResult(new ProviderCultureResult((StringSegment) "tr", (StringSegment) "hu"));
}
}

protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
services.Configure<AbpRequestLocalizationOptions>(options =>
{
options.RequestLocalizationOptionConfigurators.Add((serviceProvider, localizationOptions) =>
{
localizationOptions.RequestCultureProviders.Insert(0, new TestRequestCultureProvider());
return Task.CompletedTask;
});
});
}

[Fact]
public async Task TestRequestCultureProvider_Test()
{
var response = await GetResponseAsync("api/LocalizationTestController", HttpStatusCode.OK);
var resultAsString = await response.Content.ReadAsStringAsync();
resultAsString.ToLower().ShouldBe("tr:hu");
}
}
}

0 comments on commit 9c1e24e

Please sign in to comment.