Refactoring
This commit is contained in:
parent
f83c1b1741
commit
2e2d46996a
|
@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Oqtane.Infrastructure;
|
using Oqtane.Infrastructure;
|
||||||
using Oqtane.Infrastructure.Localization;
|
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
using Oqtane.Modules;
|
using Oqtane.Modules;
|
||||||
using Oqtane.Shared;
|
using Oqtane.Shared;
|
||||||
|
@ -21,12 +20,14 @@ namespace Oqtane.Controllers
|
||||||
private readonly IConfigurationRoot _config;
|
private readonly IConfigurationRoot _config;
|
||||||
private readonly IInstallationManager _installationManager;
|
private readonly IInstallationManager _installationManager;
|
||||||
private readonly IDatabaseManager _databaseManager;
|
private readonly IDatabaseManager _databaseManager;
|
||||||
|
private readonly ILocalizationManager _localizationManager;
|
||||||
|
|
||||||
public InstallationController(IConfigurationRoot config, IInstallationManager installationManager, IDatabaseManager databaseManager)
|
public InstallationController(IConfigurationRoot config, IInstallationManager installationManager, IDatabaseManager databaseManager, ILocalizationManager localizationManager)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_installationManager = installationManager;
|
_installationManager = installationManager;
|
||||||
_databaseManager = databaseManager;
|
_databaseManager = databaseManager;
|
||||||
|
_localizationManager = localizationManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST api/<controller>
|
// POST api/<controller>
|
||||||
|
@ -76,7 +77,7 @@ namespace Oqtane.Controllers
|
||||||
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
||||||
|
|
||||||
// Get the satellite assemblies
|
// Get the satellite assemblies
|
||||||
foreach (var culture in LocalizationSettings.SupportedCultures)
|
foreach (var culture in _localizationManager.GetSupportedCultures())
|
||||||
{
|
{
|
||||||
if (culture == Constants.DefaultCulture)
|
if (culture == Constants.DefaultCulture)
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,44 +4,13 @@ using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Oqtane.Infrastructure;
|
using Oqtane.Infrastructure;
|
||||||
using Oqtane.Infrastructure.Localization;
|
|
||||||
|
|
||||||
namespace Oqtane.Extensions
|
namespace Oqtane.Extensions
|
||||||
{
|
{
|
||||||
public static class ApplicationBuilderExtensions
|
public static class ApplicationBuilderExtensions
|
||||||
{
|
{
|
||||||
private static readonly string DefaultCultureKey = "Localization:DefaultCulture";
|
|
||||||
private static readonly string SupportedCulturesKey = "Localization:SupportedCultures";
|
|
||||||
|
|
||||||
public static IApplicationBuilder UseOqtaneLocalization(this IApplicationBuilder app)
|
|
||||||
{
|
|
||||||
var configuration = app.ApplicationServices.GetService<IConfiguration>();
|
|
||||||
var defaultCulture = configuration.GetSection(DefaultCultureKey).Value;
|
|
||||||
var supportedCultures = configuration.GetSection(SupportedCulturesKey).Get<string[]>();
|
|
||||||
if (defaultCulture == CultureInfo.InstalledUICulture.Name)
|
|
||||||
{
|
|
||||||
LocalizationSettings.DefaultCulture = defaultCulture;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (supportedCultures.Length > 0)
|
|
||||||
{
|
|
||||||
LocalizationSettings.SupportedCultures.AddRange(supportedCultures);
|
|
||||||
}
|
|
||||||
|
|
||||||
CultureInfo.CurrentUICulture = new CultureInfo(defaultCulture);
|
|
||||||
|
|
||||||
app.UseRequestLocalization(options => {
|
|
||||||
options.SetDefaultCulture(defaultCulture)
|
|
||||||
.AddSupportedUICultures(supportedCultures)
|
|
||||||
.AddSupportedUICultures(supportedCultures);
|
|
||||||
});
|
|
||||||
|
|
||||||
return app;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IApplicationBuilder ConfigureOqtaneAssemblies(this IApplicationBuilder app, IWebHostEnvironment env)
|
public static IApplicationBuilder ConfigureOqtaneAssemblies(this IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
var startUps = AppDomain.CurrentDomain
|
var startUps = AppDomain.CurrentDomain
|
||||||
|
@ -55,5 +24,22 @@ namespace Oqtane.Extensions
|
||||||
|
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IApplicationBuilder UseOqtaneLocalization(this IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
var localizationManager = app.ApplicationServices.GetService<ILocalizationManager>();
|
||||||
|
var defaultCulture = localizationManager.GetDefaultCulture();
|
||||||
|
var supportedCultures = localizationManager.GetSupportedCultures();
|
||||||
|
|
||||||
|
CultureInfo.CurrentUICulture = new CultureInfo(defaultCulture);
|
||||||
|
|
||||||
|
app.UseRequestLocalization(options => {
|
||||||
|
options.SetDefaultCulture(defaultCulture)
|
||||||
|
.AddSupportedUICultures(supportedCultures)
|
||||||
|
.AddSupportedUICultures(supportedCultures);
|
||||||
|
});
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ using System.Reflection;
|
||||||
using System.Runtime.Loader;
|
using System.Runtime.Loader;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Oqtane.Infrastructure;
|
using Oqtane.Infrastructure;
|
||||||
using Oqtane.Infrastructure.Localization;
|
|
||||||
using Oqtane.Modules;
|
using Oqtane.Modules;
|
||||||
using Oqtane.Services;
|
using Oqtane.Services;
|
||||||
using Oqtane.Shared;
|
using Oqtane.Shared;
|
||||||
|
@ -134,7 +133,10 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
|
|
||||||
AssemblyLoadContext.Default.Resolving += ResolveDependencies;
|
AssemblyLoadContext.Default.Resolving += ResolveDependencies;
|
||||||
|
|
||||||
foreach (var culture in LocalizationSettings.SupportedCultures)
|
using (var serviceScope = ServiceActivator.GetScope())
|
||||||
|
{
|
||||||
|
var localizationManager = serviceScope.ServiceProvider.GetService<ILocalizationManager>();
|
||||||
|
foreach (var culture in localizationManager.GetSupportedCultures())
|
||||||
{
|
{
|
||||||
if (culture == Constants.DefaultCulture)
|
if (culture == Constants.DefaultCulture)
|
||||||
{
|
{
|
||||||
|
@ -167,6 +169,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static Assembly ResolveDependencies(AssemblyLoadContext context, AssemblyName name)
|
private static Assembly ResolveDependencies(AssemblyLoadContext context, AssemblyName name)
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
namespace Oqtane.Infrastructure
|
||||||
|
{
|
||||||
|
public interface ILocalizationManager
|
||||||
|
{
|
||||||
|
string GetDefaultCulture();
|
||||||
|
|
||||||
|
string[] GetSupportedCultures();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
namespace Oqtane.Infrastructure
|
||||||
|
{
|
||||||
|
public class LocalizationOptions
|
||||||
|
{
|
||||||
|
public string DefaultCulture { get; set; }
|
||||||
|
|
||||||
|
public string[] SupportedCultures { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +0,0 @@
|
||||||
using System.Collections.Generic;
|
|
||||||
using Oqtane.Shared;
|
|
||||||
|
|
||||||
namespace Oqtane.Infrastructure.Localization
|
|
||||||
{
|
|
||||||
public static class LocalizationSettings
|
|
||||||
{
|
|
||||||
static LocalizationSettings()
|
|
||||||
{
|
|
||||||
DefaultCulture = Constants.DefaultCulture;
|
|
||||||
SupportedCultures = new List<string> { DefaultCulture };
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string DefaultCulture { get; set; }
|
|
||||||
|
|
||||||
public static List<string> SupportedCultures { get; }
|
|
||||||
}
|
|
||||||
}
|
|
29
Oqtane.Server/Infrastructure/LocalizationManager.cs
Normal file
29
Oqtane.Server/Infrastructure/LocalizationManager.cs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
using System.Collections;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using Oqtane.Shared;
|
||||||
|
|
||||||
|
namespace Oqtane.Infrastructure
|
||||||
|
{
|
||||||
|
public class LocalizationManager : ILocalizationManager
|
||||||
|
{
|
||||||
|
private static readonly string DefaultCulture = Constants.DefaultCulture;
|
||||||
|
private static readonly string[] SupportedCultures = new[] { DefaultCulture };
|
||||||
|
|
||||||
|
private readonly LocalizationOptions _localizationOptions;
|
||||||
|
|
||||||
|
public LocalizationManager(IOptions<LocalizationOptions> localizationOptions)
|
||||||
|
{
|
||||||
|
_localizationOptions = localizationOptions.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetDefaultCulture()
|
||||||
|
=> string.IsNullOrEmpty(_localizationOptions.DefaultCulture)
|
||||||
|
? DefaultCulture
|
||||||
|
: _localizationOptions.DefaultCulture;
|
||||||
|
|
||||||
|
public string[] GetSupportedCultures()
|
||||||
|
=> _localizationOptions.SupportedCultures.IsNullOrEmpty()
|
||||||
|
? SupportedCultures
|
||||||
|
: _localizationOptions.SupportedCultures;
|
||||||
|
}
|
||||||
|
}
|
|
@ -128,6 +128,8 @@ namespace Oqtane
|
||||||
.AddSignInManager()
|
.AddSignInManager()
|
||||||
.AddDefaultTokenProviders();
|
.AddDefaultTokenProviders();
|
||||||
|
|
||||||
|
services.Configure<LocalizationOptions>(Configuration.GetSection("Localization"));
|
||||||
|
|
||||||
services.Configure<IdentityOptions>(options =>
|
services.Configure<IdentityOptions>(options =>
|
||||||
{
|
{
|
||||||
// Password settings
|
// Password settings
|
||||||
|
@ -190,6 +192,7 @@ namespace Oqtane
|
||||||
services.AddTransient<ISettingRepository, SettingRepository>();
|
services.AddTransient<ISettingRepository, SettingRepository>();
|
||||||
services.AddTransient<ILogRepository, LogRepository>();
|
services.AddTransient<ILogRepository, LogRepository>();
|
||||||
services.AddTransient<ILogManager, LogManager>();
|
services.AddTransient<ILogManager, LogManager>();
|
||||||
|
services.AddTransient<ILocalizationManager, LocalizationManager>();
|
||||||
services.AddTransient<IJobRepository, JobRepository>();
|
services.AddTransient<IJobRepository, JobRepository>();
|
||||||
services.AddTransient<IJobLogRepository, JobLogRepository>();
|
services.AddTransient<IJobLogRepository, JobLogRepository>();
|
||||||
services.AddTransient<INotificationRepository, NotificationRepository>();
|
services.AddTransient<INotificationRepository, NotificationRepository>();
|
||||||
|
@ -199,6 +202,9 @@ namespace Oqtane
|
||||||
services.AddTransient<ISqlRepository, SqlRepository>();
|
services.AddTransient<ISqlRepository, SqlRepository>();
|
||||||
services.AddTransient<IUpgradeManager, UpgradeManager>();
|
services.AddTransient<IUpgradeManager, UpgradeManager>();
|
||||||
|
|
||||||
|
// TODO: Check if there's a better way instead of building service provider
|
||||||
|
ServiceActivator.Configure(services.BuildServiceProvider());
|
||||||
|
|
||||||
// load the external assemblies into the app domain, install services
|
// load the external assemblies into the app domain, install services
|
||||||
services.AddOqtane(_runtime);
|
services.AddOqtane(_runtime);
|
||||||
|
|
||||||
|
|
8
Oqtane.Shared/Extensions/EnumerableExtensions.cs
Normal file
8
Oqtane.Shared/Extensions/EnumerableExtensions.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace System.Collections
|
||||||
|
{
|
||||||
|
public static class EnumerableExtensions
|
||||||
|
{
|
||||||
|
public static bool IsNullOrEmpty(this IEnumerable source)
|
||||||
|
=> source == null || source.GetEnumerator().MoveNext() == false;
|
||||||
|
}
|
||||||
|
}
|
22
Oqtane.Shared/Shared/ServiceActivator.cs
Normal file
22
Oqtane.Shared/Shared/ServiceActivator.cs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
using System;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Oqtane.Shared
|
||||||
|
{
|
||||||
|
public static class ServiceActivator
|
||||||
|
{
|
||||||
|
private static IServiceProvider _serviceProvider = null;
|
||||||
|
|
||||||
|
public static void Configure(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
_serviceProvider = serviceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceScope GetScope(IServiceProvider serviceProvider = null)
|
||||||
|
{
|
||||||
|
var provider = serviceProvider ?? _serviceProvider;
|
||||||
|
|
||||||
|
return provider?.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user