using System.Globalization; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Localization; namespace gehGassi.Web.Helper { /// /// Hilfsklasse die das Routing basierend auf Cultures implementiert und die gewählte Sprache setzt /// public class RouteValueRequestCultureProvider : RequestCultureProvider { /// public override Task DetermineProviderCultureResult(HttpContext httpContext) { string cultureCode = null; if (httpContext.Request.Path.HasValue && httpContext.Request.Path.Value == "/") cultureCode = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; //this.GetDefaultCultureCode(); else if (httpContext.Request.Path.HasValue && httpContext.Request.Path.Value.Length >= 4 && httpContext.Request.Path.Value[0] == '/' && httpContext.Request.Path.Value[3] == '/') { cultureCode = httpContext.Request.Path.Value.Substring(1, 2); if (!this.CheckCultureCode(cultureCode)) cultureCode = this.GetDefaultCultureCode(); //throw new HttpException(HttpStatusCode.NotFound); } else if (httpContext.Request.Path.HasValue && httpContext.Request.Path.Value.Length == 3 && httpContext.Request.Path.Value[0] == '/') { cultureCode = httpContext.Request.Path.Value.Substring(1, 2); if (!this.CheckCultureCode(cultureCode)) cultureCode = this.GetDefaultCultureCode(); //throw new HttpException(HttpStatusCode.NotFound); } else cultureCode = this.GetDefaultCultureCode(); //throw new HttpException(HttpStatusCode.NotFound); ProviderCultureResult requestCulture = new ProviderCultureResult(cultureCode); return Task.FromResult(requestCulture); } /// /// Standard-Culture holen /// /// Standard-Culture private string GetDefaultCultureCode() { return this.Options.DefaultRequestCulture.Culture.TwoLetterISOLanguageName; } /// /// Prüft ob die gewählte Culture unterstützt wird /// /// Culture /// true wenn unterstützt, false sonst private bool CheckCultureCode(string cultureCode) { return this.Options.SupportedCultures.Select(c => c.TwoLetterISOLanguageName).Contains(cultureCode); } } }