SocialPub/PrivaPub/Services/RequestLocalizationOptionsService.cs
2023-02-19 00:43:43 +01:00

43 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Localization;
using PrivaPub.StaticServices;
using System.Globalization;
namespace PrivaPub.Services
{
public class RequestLocalizationOptionsService
{
RequestLocalizationOptions RequestLocalizationOptions { get; set; }
readonly AppConfigurationService AppConfigurationService;
public RequestLocalizationOptionsService(AppConfigurationService appConfigurationService)
{
AppConfigurationService = appConfigurationService;
}
public async Task<RequestLocalizationOptions> Get()
{
if (RequestLocalizationOptions != default) return RequestLocalizationOptions;
if (AppConfigurationService.AppConfiguration == default)
await AppConfigurationService.Init();
RequestLocalizationOptions = new RequestLocalizationOptions();
var cultures = AppConfigurationService
.AppConfiguration
.SupportedLanguages
.Select(sl => new CultureInfo(sl))
.ToArray();
RequestLocalizationOptions.DefaultRequestCulture = new(cultures.First(), cultures.First());
RequestLocalizationOptions.SupportedCultures = cultures;
RequestLocalizationOptions.SupportedUICultures = cultures;
RequestLocalizationOptions.RequestCultureProviders = new IRequestCultureProvider[] {
new AcceptLanguageHeaderRequestCultureProvider { Options = RequestLocalizationOptions }
};
RequestLocalizationOptions.ApplyCurrentCultureToResponseHeaders = true;
return RequestLocalizationOptions;
}
}
}