开发者

Modify current culture?

 CultureInfo culture = new CultureInfo("en-US");

        culture.DateTimeFormat.DateSeparator = "/";
        culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";

        //dekadikoi arithmoi
        culture.NumberFormat.NumberDecimalSeparator = ".";
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

As it is required to work that way everywhere! The problem is that doing new CultureInfo("en-US"); all other computer-specific settings are omited... Is there a way to copy the CurrentCulture? I tried to modify c开发者_开发百科urrentculture but i got read only error...


CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();

culture.DateTimeFormat.DateSeparator = "/";
culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";

//dekadikoi arithmoi
culture.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

You may wish to use:

CultureInfo culture = (CultureInfo)CultureInfo.CurrentUICulture.Clone();

instead.


Just change one line:

CultureInfo culture = CultureInfo.GetCultureInfo("en-US");


what about this : CultureInfo culture=CultureInfo.CurrentCulture;


Just take a copy of the CurrentCulture class and modifiy it as you see fit. If you do need to change the CultureInfo of the thread itself (rather than using a copy) you need to give your code a security permission and set the ControlThread property to true. (see link for example)


Following on Jon Hanna's solution I would suggest starting on a specific culture, as the CurrentCulture can be set to anything.

public static CultureInfo GetCustomCulture(string baseCulture = "en-ZA", char numberDecimalSeperator = '.', char listSeperator = ',') {
    var culture = CultureInfo.GetCultureInfo(baseCulture).Clone();

    culture.NumberFormat.NumberDecimalSeparator = numberDecimalSeperator + "";
    culture.TextInfo.ListSeparator = listSeperator + "";

    return culture;
}

And then add this line to the beginning of Program.cs

System.Globalization.CultureInfo.CurrentCulture = GetCustomCulture();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜