Skip to content

Instantly share code, notes, and snippets.

@DmitrySikorsky
Created January 9, 2018 16:48
Show Gist options
  • Save DmitrySikorsky/86f840764dd3c9e86658793a07c9b859 to your computer and use it in GitHub Desktop.
Save DmitrySikorsky/86f840764dd3c9e86658793a07c9b859 to your computer and use it in GitHub Desktop.
public class RouteValueRequestCultureProvider : RequestCultureProvider
{
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
string cultureCode = null;
if (httpContext.Request.Path.HasValue && httpContext.Request.Path.Value == "/")
cultureCode = this.GetDefaultCultureCode();
// TODO: make it look more beautiful
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 cultureCode = this.GetDefaultCultureCode(); //throw new HttpException(HttpStatusCode.NotFound);
// TODO: from the SEO point of view, we should return 404 error code for unknown cultures
ProviderCultureResult requestCulture = new ProviderCultureResult(cultureCode);
return Task.FromResult(requestCulture);
}
private string GetDefaultCultureCode()
{
return this.Options.DefaultRequestCulture.Culture.TwoLetterISOLanguageName;
}
private bool CheckCultureCode(string cultureCode)
{
return this.Options.SupportedCultures.Select(c => c.TwoLetterISOLanguageName).Contains(cultureCode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment