|
|
|
@ -1,8 +1,6 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Localization;
|
|
|
|
@ -11,37 +9,21 @@ using Microsoft.Extensions.Options;
|
|
|
|
|
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
|
|
|
|
|
public class DefaultAbpRequestLocalizationOptionsProvider : IAbpRequestLocalizationOptionsProvider, ITransientDependency
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceScopeFactory _serviceProviderFactory;
|
|
|
|
|
private readonly SemaphoreSlim _syncSemaphore;
|
|
|
|
|
private Action<RequestLocalizationOptions> _optionsAction;
|
|
|
|
|
private RequestLocalizationOptions _requestLocalizationOptions;
|
|
|
|
|
protected readonly IServiceScopeFactory ServiceProviderFactory;
|
|
|
|
|
|
|
|
|
|
public DefaultAbpRequestLocalizationOptionsProvider(IServiceScopeFactory serviceProviderFactory)
|
|
|
|
|
{
|
|
|
|
|
_serviceProviderFactory = serviceProviderFactory;
|
|
|
|
|
_syncSemaphore = new SemaphoreSlim(1, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InitLocalizationOptions(Action<RequestLocalizationOptions> optionsAction = null)
|
|
|
|
|
{
|
|
|
|
|
_optionsAction = optionsAction;
|
|
|
|
|
ServiceProviderFactory = serviceProviderFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<RequestLocalizationOptions> GetLocalizationOptionsAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_requestLocalizationOptions == null)
|
|
|
|
|
{
|
|
|
|
|
using (await _syncSemaphore.LockAsync())
|
|
|
|
|
{
|
|
|
|
|
if (_requestLocalizationOptions == null)
|
|
|
|
|
{
|
|
|
|
|
using (var serviceScope = _serviceProviderFactory.CreateScope())
|
|
|
|
|
using (var serviceScope = ServiceProviderFactory.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var languageProvider = serviceScope.ServiceProvider.GetRequiredService<ILanguageProvider>();
|
|
|
|
|
var settingProvider = serviceScope.ServiceProvider.GetRequiredService<ISettingProvider>();
|
|
|
|
@ -53,7 +35,7 @@ namespace Microsoft.AspNetCore.RequestLocalization
|
|
|
|
|
? new RequestLocalizationOptions()
|
|
|
|
|
: new RequestLocalizationOptions
|
|
|
|
|
{
|
|
|
|
|
DefaultRequestCulture = DefaultGetRequestCulture(defaultLanguage, languages),
|
|
|
|
|
DefaultRequestCulture = await GetDefaultRequestCultureAsync(defaultLanguage, languages),
|
|
|
|
|
|
|
|
|
|
SupportedCultures = languages
|
|
|
|
|
.Select(l => l.CultureName)
|
|
|
|
@ -75,26 +57,20 @@ namespace Microsoft.AspNetCore.RequestLocalization
|
|
|
|
|
await configurator(serviceScope.ServiceProvider, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_optionsAction?.Invoke(options);
|
|
|
|
|
_requestLocalizationOptions = options;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _requestLocalizationOptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static RequestCulture DefaultGetRequestCulture(string defaultLanguage, IReadOnlyList<LanguageInfo> languages)
|
|
|
|
|
protected virtual Task<RequestCulture> GetDefaultRequestCultureAsync(string defaultLanguage, IReadOnlyList<LanguageInfo> languages)
|
|
|
|
|
{
|
|
|
|
|
if (defaultLanguage == null)
|
|
|
|
|
{
|
|
|
|
|
var firstLanguage = languages.First();
|
|
|
|
|
return new RequestCulture(firstLanguage.CultureName, firstLanguage.UiCultureName);
|
|
|
|
|
var firstLanguage = languages[0];
|
|
|
|
|
return Task.FromResult(new RequestCulture(firstLanguage.CultureName, firstLanguage.UiCultureName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (cultureName, uiCultureName) = LocalizationSettingHelper.ParseLanguageSetting(defaultLanguage);
|
|
|
|
|
return new RequestCulture(cultureName, uiCultureName);
|
|
|
|
|
return Task.FromResult(new RequestCulture(cultureName, uiCultureName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|