From 86bb6d1ea8fd4bb0c9efbc8a1561bc0b0223d8c4 Mon Sep 17 00:00:00 2001 From: hishamco Date: Sat, 10 Oct 2020 22:19:21 +0300 Subject: [PATCH] Simplify localization settings configurations --- .../Extensions/WebHostBuilderExtensions.cs | 21 +++++++++++++++++++ Oqtane.Server/Program.cs | 1 + Oqtane.Server/Startup.cs | 17 +++++---------- 3 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 Oqtane.Server/Extensions/WebHostBuilderExtensions.cs diff --git a/Oqtane.Server/Extensions/WebHostBuilderExtensions.cs b/Oqtane.Server/Extensions/WebHostBuilderExtensions.cs new file mode 100644 index 00000000..3e9b8fc7 --- /dev/null +++ b/Oqtane.Server/Extensions/WebHostBuilderExtensions.cs @@ -0,0 +1,21 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Oqtane.Infrastructure; + +namespace Microsoft.AspNetCore.Hosting +{ + public static class WebHostBuilderExtensions + { + public static IWebHostBuilder ConfigureLocalizationSettings(this IWebHostBuilder builder) + { + return builder.ConfigureServices((context, services) => + { + var config = context.Configuration; + + services.Configure(config.GetSection("Localization")); + services.AddSingleton(ctx => ctx.GetService>().Value); + services.AddTransient(); + }); + } + } +} diff --git a/Oqtane.Server/Program.cs b/Oqtane.Server/Program.cs index c4b5492f..1ac0797e 100644 --- a/Oqtane.Server/Program.cs +++ b/Oqtane.Server/Program.cs @@ -26,6 +26,7 @@ namespace Oqtane.Server .AddCommandLine(args) .Build()) .UseStartup() + .ConfigureLocalizationSettings() .Build(); } } diff --git a/Oqtane.Server/Startup.cs b/Oqtane.Server/Startup.cs index 976556b7..f5c5e07b 100644 --- a/Oqtane.Server/Startup.cs +++ b/Oqtane.Server/Startup.cs @@ -27,22 +27,23 @@ namespace Oqtane { public class Startup { - private static readonly string[] DefaultSupportedCultures = new[] { Constants.DefaultCulture }; - private string _webRoot; private Runtime _runtime; private bool _useSwagger; private IWebHostEnvironment _env; + private string[] _supportedCultures; public IConfigurationRoot Configuration { get; } - public Startup(IWebHostEnvironment env) + public Startup(IWebHostEnvironment env, ILocalizationManager localizationManager) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); Configuration = builder.Build(); + _supportedCultures = localizationManager.GetSupportedCultures(); + _runtime = (Configuration.GetSection("Runtime").Value == "WebAssembly") ? Runtime.WebAssembly : Runtime.Server; //add possibility to switch off swagger on production. @@ -141,11 +142,6 @@ namespace Oqtane .AddSignInManager() .AddDefaultTokenProviders(); - var localizationSection = Configuration.GetSection("Localization"); - var localizationOptions = localizationSection.Get(); - - services.Configure(localizationSection); - services.Configure(options => { // Password settings @@ -219,10 +215,7 @@ namespace Oqtane services.AddTransient(); // load the external assemblies into the app domain, install services - services.AddOqtane(_runtime, - localizationOptions.SupportedCultures.IsNullOrEmpty() - ? DefaultSupportedCultures - : localizationOptions.SupportedCultures); + services.AddOqtane(_runtime, _supportedCultures); services.AddMvc() .AddNewtonsoftJson()