mirror of https://github.com/abpframework/abp.git
22 changed files with 354 additions and 98 deletions
@ -0,0 +1,26 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.AspNetCore.Localization; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Microsoft.AspNetCore.RequestLocalization |
||||
|
{ |
||||
|
public class AbpRequestLocalizationMiddleware : IMiddleware, ITransientDependency |
||||
|
{ |
||||
|
private readonly IAbpRequestLocalizationOptionsProvider _requestLocalizationOptionsProvider; |
||||
|
|
||||
|
public AbpRequestLocalizationMiddleware( |
||||
|
IAbpRequestLocalizationOptionsProvider requestLocalizationOptionsProvider) |
||||
|
{ |
||||
|
_requestLocalizationOptionsProvider = requestLocalizationOptionsProvider; |
||||
|
} |
||||
|
|
||||
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next) |
||||
|
{ |
||||
|
var options = Options.Create(_requestLocalizationOptionsProvider.GetLocalizationOptions()); |
||||
|
var middleware = new RequestLocalizationMiddleware(next, options); |
||||
|
await middleware.Invoke(context); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,80 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Globalization; |
||||
|
using System.Linq; |
||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
using Microsoft.AspNetCore.Localization; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Localization; |
||||
|
using Volo.Abp.Settings; |
||||
|
using Volo.Abp.Threading; |
||||
|
|
||||
|
namespace Microsoft.AspNetCore.RequestLocalization |
||||
|
{ |
||||
|
public class DefaultAbpRequestLocalizationOptionsProvider : IAbpRequestLocalizationOptionsProvider, ISingletonDependency |
||||
|
{ |
||||
|
private readonly IServiceProvider _serviceProvider; |
||||
|
private Lazy<RequestLocalizationOptions> _lazyRequestLocalizationOptions; |
||||
|
|
||||
|
public DefaultAbpRequestLocalizationOptionsProvider(IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
_serviceProvider = serviceProvider; |
||||
|
} |
||||
|
|
||||
|
public void InitLocalizationOptions(Action<RequestLocalizationOptions> optionsAction = null) |
||||
|
{ |
||||
|
_lazyRequestLocalizationOptions = new Lazy<RequestLocalizationOptions>(() => |
||||
|
{ |
||||
|
using (var serviceScope = _serviceProvider.CreateScope()) |
||||
|
{ |
||||
|
var languageProvider = serviceScope.ServiceProvider.GetRequiredService<ILanguageProvider>(); |
||||
|
var settingProvider = serviceScope.ServiceProvider.GetRequiredService<ISettingProvider>(); |
||||
|
|
||||
|
var languages = AsyncHelper.RunSync(languageProvider.GetLanguagesAsync); |
||||
|
var defaultLanguage = AsyncHelper.RunSync(() => |
||||
|
settingProvider.GetOrNullAsync(LocalizationSettingNames.DefaultLanguage)); |
||||
|
|
||||
|
var options = !languages.Any() |
||||
|
? new RequestLocalizationOptions() |
||||
|
: new RequestLocalizationOptions |
||||
|
{ |
||||
|
DefaultRequestCulture = DefaultGetRequestCulture(defaultLanguage, languages), |
||||
|
|
||||
|
SupportedCultures = languages |
||||
|
.Select(l => l.CultureName) |
||||
|
.Distinct() |
||||
|
.Select(c => new CultureInfo(c)) |
||||
|
.ToArray(), |
||||
|
|
||||
|
SupportedUICultures = languages |
||||
|
.Select(l => l.UiCultureName) |
||||
|
.Distinct() |
||||
|
.Select(c => new CultureInfo(c)) |
||||
|
.ToArray() |
||||
|
}; |
||||
|
|
||||
|
optionsAction?.Invoke(options); |
||||
|
return options; |
||||
|
} |
||||
|
}, true); |
||||
|
} |
||||
|
|
||||
|
public RequestLocalizationOptions GetLocalizationOptions() |
||||
|
{ |
||||
|
return _lazyRequestLocalizationOptions.Value; |
||||
|
} |
||||
|
|
||||
|
private static RequestCulture DefaultGetRequestCulture(string defaultLanguage, IReadOnlyList<LanguageInfo> languages) |
||||
|
{ |
||||
|
if (defaultLanguage == null) |
||||
|
{ |
||||
|
var firstLanguage = languages.First(); |
||||
|
return new RequestCulture(firstLanguage.CultureName, firstLanguage.UiCultureName); |
||||
|
} |
||||
|
|
||||
|
var (cultureName, uiCultureName) = LocalizationSettingHelper.ParseLanguageSetting(defaultLanguage); |
||||
|
return new RequestCulture(cultureName, uiCultureName); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
using System; |
||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
|
||||
|
namespace Microsoft.AspNetCore.RequestLocalization |
||||
|
{ |
||||
|
public interface IAbpRequestLocalizationOptionsProvider |
||||
|
{ |
||||
|
void InitLocalizationOptions(Action<RequestLocalizationOptions> optionsAction = null); |
||||
|
|
||||
|
RequestLocalizationOptions GetLocalizationOptions(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.AspNetCore.Mvc.RazorPages; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection |
||||
|
{ |
||||
|
public class AbpAspNetCoreMvcConventionalRegistrar_Tests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Registered_Mvc_Service() |
||||
|
{ |
||||
|
//Arrange
|
||||
|
var services = new ServiceCollection(); |
||||
|
|
||||
|
//Act
|
||||
|
services.AddConventionalRegistrar(new DefaultConventionalRegistrar()); |
||||
|
services.AddConventionalRegistrar(new AbpAspNetCoreMvcConventionalRegistrar()); |
||||
|
|
||||
|
services.AddTypes(typeof(My_Test_PageModel), typeof(My_Test_Controller), typeof(My_Test_ViewComponent)); |
||||
|
|
||||
|
//Assert
|
||||
|
services.ShouldContainTransient(typeof(My_Test_PageModel)); |
||||
|
services.ShouldContainTransient(typeof(My_Test_Controller)); |
||||
|
services.ShouldContainTransient(typeof(My_Test_ViewComponent)); |
||||
|
|
||||
|
var serviceProvider = services.BuildServiceProvider(); |
||||
|
serviceProvider.GetServices<My_Test_PageModel>().Count().ShouldBe(1); |
||||
|
serviceProvider.GetServices<My_Test_Controller>().Count().ShouldBe(1); |
||||
|
serviceProvider.GetServices<My_Test_ViewComponent>().Count().ShouldBe(1); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class My_Test_PageModel : PageModel |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public class My_Test_Controller : Controller |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public class My_Test_ViewComponent : ViewComponent |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.Identity |
||||
|
{ |
||||
|
public interface IIdentityStoreAutoSaveChanges |
||||
|
{ |
||||
|
bool AutoSaveChanges { get; set; } |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue