Skip to content

Instantly share code, notes, and snippets.

@nordinrahman
Last active September 18, 2016 10:08
Show Gist options
  • Select an option

  • Save nordinrahman/b0f017711ba2f57cac789c501b078df2 to your computer and use it in GitHub Desktop.

Select an option

Save nordinrahman/b0f017711ba2f57cac789c501b078df2 to your computer and use it in GitHub Desktop.
Global.asax code to set current thread's CultureInfo with CustomCultureUI instance
public override void Init()
{
base.Init();
BeginRequest -= OnBeginRequest;
BeginRequest += OnBeginRequest;
}
private void OnBeginRequest(object sender, EventArgs eventArgs)
{
var locale = Request.QueryString["locale"];
if (!string.IsNullOrEmpty(locale))
{
CultureInfo localeCulture = null;
try
{
localeCulture = new CultureInfo(locale);
}
catch (CultureNotFoundException)
{
var m = Regex.Match(locale, @"^\w+-(\w+)");
var countryCode = m.Groups[1].Value;
if (!string.IsNullOrEmpty(countryCode))
{
localeCulture = new CustomCultureInfo(locale);
}
}
if (localeCulture != null)
{
Thread.CurrentThread.CurrentCulture = localeCulture;
Thread.CurrentThread.CurrentUICulture = localeCulture;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment