Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Set application-wide own culture
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Markup;
namespace WpfApplication1
{
/*
In Visual Studio:
1. Create WPF Application.
2. Use copy-paste!
*/
public partial class App
{
public const string CultureName = "ru-MIT";
static App()
{
SetLanguage();
}
private static void SetLanguage()
{
Console.WriteLine(@">>>> SetLanguage()");
CultureInfo culture = SystemHasMitCulture()
? new CultureInfo(CultureName)
: new CultureInfo("ru-RU");
Console.WriteLine(">>> Using culture: {0}", culture);
Console.WriteLine(">>> Test double: {0}", 123456.789.ToString("N"));
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
XmlLanguage xmlLanguage = XmlLanguage.GetLanguage(culture.IetfLanguageTag);
FrameworkElement.LanguageProperty.OverrideMetadata(typeof (FrameworkElement),
new FrameworkPropertyMetadata(xmlLanguage));
}
private static bool SystemHasMitCulture()
{
return CultureInfo.GetCultures(CultureTypes.UserCustomCulture)
.Any(ci => (ci.Name == CultureName));
}
}
}
using System;
using System.Globalization;
using System.Linq;
namespace MitCultureCreator
{
/*
In Visual Studio:
1. Create Console Application.
2. Add application manifest.
3. Uncomment following line in manifest
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
4. Use copy-paste!
*/
internal class Program
{
private const string CultureName = "ru-MIT";
private const string cultureEnglishName = "ru-MIT";
private const string cultureNativeName = "Русский (Мит)";
private static void Main(string[] args)
{
CreateAndRegisterOwnCulture();
}
private static void CreateAndRegisterOwnCulture()
{
var ownCulture = new CultureAndRegionInfoBuilder(CultureName, CultureAndRegionModifiers.None);
var parentCulture = new CultureInfo("ru-RU");
var region = new RegionInfo("RU");
ownCulture.LoadDataFromCultureInfo(parentCulture);
ownCulture.LoadDataFromRegionInfo(region);
ownCulture.CultureEnglishName = cultureEnglishName;
ownCulture.CultureNativeName = cultureNativeName;
var nfi = (NumberFormatInfo) parentCulture.NumberFormat.Clone();
nfi.NumberDecimalSeparator = ".";
nfi.NumberGroupSeparator = " ";
nfi.CurrencyDecimalSeparator = ".";
nfi.CurrencyGroupSeparator = " ";
ownCulture.NumberFormat = nfi;
ownCulture.Parent = parentCulture;
if (SystemHasMitCulture())
{
Console.WriteLine(">>> System already has own culture :)");
return;
}
// Admin rights are needed here
// CultureAndRegionInfoBuilder.Unregister(CultureName);
try
{
// Admin rights are needed here
Console.WriteLine(">>> Trying to set own culture");
ownCulture.Register();
Console.WriteLine(">>> Own culture set successfully");
}
catch (UnauthorizedAccessException)
{
Console.WriteLine(">>> Set own culture failed :(");
}
catch (InvalidOperationException)
{
Console.WriteLine(">>> Culture is already registered :)");
}
}
private static bool SystemHasMitCulture()
{
return CultureInfo.GetCultures(CultureTypes.UserCustomCulture).Any(ci => (ci.Name == CultureName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment