Read supported cultures from appsettings.json
This commit is contained in:
		@ -1,14 +1,43 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Globalization;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Reflection;
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Microsoft.AspNetCore.Hosting;
 | 
			
		||||
using Microsoft.Extensions.Configuration;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
using Oqtane.Infrastructure;
 | 
			
		||||
using Oqtane.Infrastructure.Localization;
 | 
			
		||||
 | 
			
		||||
namespace Oqtane.Extensions
 | 
			
		||||
{
 | 
			
		||||
    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 (supportedCultures.Length > 0)
 | 
			
		||||
            {
 | 
			
		||||
                LocalizationSettings.SupportedCultures.AddRange(supportedCultures);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            LocalizationSettings.DefaultCulture = defaultCulture;
 | 
			
		||||
            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)
 | 
			
		||||
        {
 | 
			
		||||
            var startUps = AppDomain.CurrentDomain
 | 
			
		||||
 | 
			
		||||
@ -13,6 +13,6 @@ namespace Oqtane.Infrastructure.Localization
 | 
			
		||||
 | 
			
		||||
        public static string DefaultCulture { get; set; }
 | 
			
		||||
 | 
			
		||||
        public static IList<string> SupportedCultures { get; set; }
 | 
			
		||||
        public static List<string> SupportedCultures { get; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,4 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Globalization;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
@ -17,7 +16,6 @@ using Microsoft.Extensions.Hosting;
 | 
			
		||||
using Microsoft.OpenApi.Models;
 | 
			
		||||
using Oqtane.Extensions;
 | 
			
		||||
using Oqtane.Infrastructure;
 | 
			
		||||
using Oqtane.Infrastructure.Localization;
 | 
			
		||||
using Oqtane.Repository;
 | 
			
		||||
using Oqtane.Security;
 | 
			
		||||
using Oqtane.Services;
 | 
			
		||||
@ -55,7 +53,6 @@ namespace Oqtane
 | 
			
		||||
        {
 | 
			
		||||
            // Register localization services
 | 
			
		||||
            services.AddLocalization(options => options.ResourcesPath = "Resources");
 | 
			
		||||
            CultureInfo.CurrentUICulture = new CultureInfo(LocalizationSettings.DefaultCulture);
 | 
			
		||||
 | 
			
		||||
            services.AddServerSideBlazor();
 | 
			
		||||
 | 
			
		||||
@ -232,8 +229,8 @@ namespace Oqtane
 | 
			
		||||
            // to allow install middleware it should be moved up
 | 
			
		||||
            app.ConfigureOqtaneAssemblies(env);
 | 
			
		||||
 | 
			
		||||
            app.UseRequestLocalization(options => options
 | 
			
		||||
                .AddSupportedUICultures(LocalizationSettings.SupportedCultures.ToArray()));
 | 
			
		||||
            // Allow oqtane localization middleware
 | 
			
		||||
            app.UseOqtaneLocalization();
 | 
			
		||||
 | 
			
		||||
            app.UseHttpsRedirection();
 | 
			
		||||
            app.UseStaticFiles();
 | 
			
		||||
 | 
			
		||||
@ -11,5 +11,9 @@
 | 
			
		||||
    "DefaultTheme": "",
 | 
			
		||||
    "DefaultLayout": "",
 | 
			
		||||
    "DefaultContainer": ""
 | 
			
		||||
  },
 | 
			
		||||
  "Localization": {
 | 
			
		||||
    "DefaultCulture": "en-US",
 | 
			
		||||
    "SupportedCultures": []
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user