C# 如何在 .Net 中更改整个进程(不仅仅是当前线程)的 CurrentCulture?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1460023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:46:32  来源:igfitidea点击:

How can I change the CurrentCulture of the entire process (not just current thread) in .Net?

c#.netlocalizationglobalization

提问by Assaf Lavie

I have a situation where I need to set my process' locale to en-US.

我有一种情况需要将我的流程的语言环境设置为 en-US。

I know how to do this for the current thread:

我知道如何为当前线程执行此操作:

System.Threading.Thread.CurrentThread.CurrentCulture = 
     System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

But my application uses BackgroundWorkersto do some processing, and the locale for these worker threads seems not to be affected by the above change to their spawning main-thread.

但是我的应用程序用于BackgroundWorkers进行一些处理,并且这些工作线程的语言环境似乎不受上述对其产生的主线程的更改的影响。

So how can I set the locale for all the threads in my application without setting it in each one manually?

那么如何为我的应用程序中的所有线程设置语言环境而不手动在每个线程中设置它呢?

采纳答案by RichardOD

You'll have to change the operating system locale if you want to do that. For what reason do you want BackgroundWorkers to run in en-US?

如果您想这样做,您必须更改操作系统区域设置。您希望 BackgroundWorkers 在美国运行的原因是什么?

You should have your business layer running in an invariant culture, and only have a specific culture for the end user's UI.

您应该让您的业务层以不变的文化运行,并且只有最终用户的 UI 具有特定的文化。

If you are using the BackgroundWorkercomponent, and have to do this you could try something like this in the DoWork method:

如果您正在使用BackgroundWorker组件,并且必须这样做,您可以在 DoWork 方法中尝试这样的事情:

// In DoWork
System.Globalization.CultureInfo before = System.Threading.Thread.CurrentThread.CurrentCulture;
try

{
    System.Threading.Thread.CurrentThread.CurrentCulture = 
        new System.Globalization.CultureInfo("en-US");
 // Proceed with specific code
}

finally
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = before;
}

回答by Ond?ej Vykouk

We use helper class for BackgroudWorker like this:

我们为 BackgroudWorker 使用 helper 类,如下所示:

public static class BackgroundWorkerHelper
{
public static void RunInBackground(Action doWorkAction, Action completedAction, CultureInfo cultureInfo)
    {
        var worker = new BackgroundWorker();
        worker.DoWork += (_, args) =>
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
            System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
            doWorkAction.Invoke();
        };
        worker.RunWorkerCompleted += (_, args) =>
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
            System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
            completedAction.Invoke();
        };

        worker.RunWorkerAsync();
    }
}

Example usage:

用法示例:

BackgroundWorkerHelper.RunInBackground(() => { Work(); }, () => { AfterWork(); },Thread.CurrentThread.CurrentCulture);

回答by Eric MSFT

With 4.0, you will need to manage this yourself by setting the culture for each thread. But with 4.5, you can define a culture for the appdomain and that is the preferred way to handle this. The relevant apis are CultureInfo.DefaultThreadCurrentCultureand CultureInfo.DefaultThreadCurrentUICulture.

在 4.0 中,您需要通过为每个线程设置文化来自行管理。但是在 4.5 中,您可以为 appdomain 定义一种文化,这是处理此问题的首选方式。相关的 api 是CultureInfo.DefaultThreadCurrentCultureCultureInfo.DefaultThreadCurrentUICulture

回答by Paolo30Rose

Use this:

用这个:

worker.RunWorkerAsync(Thread.CurrentThread.CurrentCulture.LCID);//Pass the LCID as argument

after on do work make this:

在做工作之后做这个:

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(int.Parse(e.Argument.ToString()));