SocialPub/PrivaPub/Services/RequestLocalizationOptionsService.cs

43 lines
1.4 KiB
C#
Raw Normal View History

2023-02-18 08:52:17 +01:00
using Microsoft.AspNetCore.Localization;
2023-02-19 00:43:43 +01:00
using PrivaPub.StaticServices;
2023-02-18 08:52:17 +01:00
using System.Globalization;
2023-02-19 00:43:43 +01:00
namespace PrivaPub.Services
2023-02-18 08:52:17 +01:00
{
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;
}
}
}