|
|
|
@ -9,13 +9,17 @@ using Microsoft.AspNetCore.Localization; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
using Volo.Abp.EventBus.Distributed; |
|
|
|
using Volo.Abp.Localization; |
|
|
|
using Volo.Abp.Settings; |
|
|
|
using Volo.Abp.Threading; |
|
|
|
|
|
|
|
namespace Microsoft.AspNetCore.RequestLocalization; |
|
|
|
|
|
|
|
public class DefaultAbpRequestLocalizationOptionsProvider : IAbpRequestLocalizationOptionsProvider, ISingletonDependency |
|
|
|
public class DefaultAbpRequestLocalizationOptionsProvider : |
|
|
|
IAbpRequestLocalizationOptionsProvider, |
|
|
|
IDistributedEventHandler<LanguageChangedEto>, |
|
|
|
ISingletonDependency |
|
|
|
{ |
|
|
|
private readonly IServiceScopeFactory _serviceProviderFactory; |
|
|
|
private readonly SemaphoreSlim _syncSemaphore; |
|
|
|
@ -28,12 +32,12 @@ public class DefaultAbpRequestLocalizationOptionsProvider : IAbpRequestLocalizat |
|
|
|
_syncSemaphore = new SemaphoreSlim(1, 1); |
|
|
|
} |
|
|
|
|
|
|
|
public void InitLocalizationOptions(Action<RequestLocalizationOptions>? optionsAction = null) |
|
|
|
public virtual void InitLocalizationOptions(Action<RequestLocalizationOptions>? optionsAction = null) |
|
|
|
{ |
|
|
|
_optionsAction = optionsAction; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<RequestLocalizationOptions> GetLocalizationOptionsAsync() |
|
|
|
public virtual async Task<RequestLocalizationOptions> GetLocalizationOptionsAsync() |
|
|
|
{ |
|
|
|
if (_requestLocalizationOptions == null) |
|
|
|
{ |
|
|
|
@ -54,13 +58,11 @@ public class DefaultAbpRequestLocalizationOptionsProvider : IAbpRequestLocalizat |
|
|
|
: 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() |
|
|
|
@ -69,8 +71,8 @@ public class DefaultAbpRequestLocalizationOptionsProvider : IAbpRequestLocalizat |
|
|
|
}; |
|
|
|
|
|
|
|
foreach (var configurator in serviceScope.ServiceProvider |
|
|
|
.GetRequiredService<IOptions<AbpRequestLocalizationOptions>>() |
|
|
|
.Value.RequestLocalizationOptionConfigurators) |
|
|
|
.GetRequiredService<IOptions<AbpRequestLocalizationOptions>>() |
|
|
|
.Value.RequestLocalizationOptionConfigurators) |
|
|
|
{ |
|
|
|
await configurator(serviceScope.ServiceProvider, options); |
|
|
|
} |
|
|
|
@ -96,4 +98,12 @@ public class DefaultAbpRequestLocalizationOptionsProvider : IAbpRequestLocalizat |
|
|
|
var (cultureName, uiCultureName) = LocalizationSettingHelper.ParseLanguageSetting(defaultLanguage); |
|
|
|
return new RequestCulture(cultureName, uiCultureName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task HandleEventAsync(LanguageChangedEto eventData) |
|
|
|
{ |
|
|
|
using (await _syncSemaphore.LockAsync()) |
|
|
|
{ |
|
|
|
_requestLocalizationOptions = null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |