Skip to content

Instantly share code, notes, and snippets.

@nordinrahman
Created September 18, 2016 10:06
Show Gist options
  • Save nordinrahman/7a7174f254d8f3fcebda43047409d9c7 to your computer and use it in GitHub Desktop.
Save nordinrahman/7a7174f254d8f3fcebda43047409d9c7 to your computer and use it in GitHub Desktop.
Custom CultureInfo class, to be used, when instantiation of CultureInfo trigger CultureNotFoundException. Only support locale-name with format xx-XX.
using System.Globalization;
using System.Text.RegularExpressions;
namespace CustomeCultureInfo
{
/// <summary>
/// Custom culture info
/// </summary>
public class CustomCultureInfo : CultureInfo
{
public CustomCultureInfo(string name) : base(GetParentCulture(name))
{
Name = name;
Parent = new CultureInfo(GetParentCulture(name));
}
private static string GetParentCulture(string name)
{
return Regex.Match(name, @"^\w+-(\w+)").Groups[1].Value;
}
public override string Name { get; }
public override CultureInfo Parent { get; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment