如何在WPF应用程序中全局设置文化?

时间:2022-12-20 02:54:02

I would like to set the Culture of a WPF application to a specific one based on user preferences.

我想根据用户首选项将WPF应用程序的文化设置为特定的文档。

I can do this for the current thread via Thread.CurrentThread.Current(UI)Culture, but is there any way to do this globally for the application (so it affects all threads by default)?

我可以通过Thread.CurrentThread.Current(UI)Culture为当前线程执行此操作,但有没有办法为应用程序全局执行此操作(因此默认情况下它会影响所有线程)?

5 个解决方案

#1


9  

there is no way to set it for all threads in the application, however if you are creating the threads in your application you can set the culture for everyone by yourself as you mentioned above

没有办法为应用程序中的所有线程设置它,但是如果您在应用程序中创建线程,您可以自己为每个人设置文化,如上所述

To set the Culture to the Main Application use the following snippet code:

要将Culture设置为Main Application,请使用以下代码段:

Dim newCulture As CultureInfo = new CultureInfo("fr-FR")
CurrentThread.CurrentCulture = newCulture

#2


7  

Try putting

试试吧

<UICulture>en-US</UICulture>

... in your csproj file.

...在你的csproj文件中。

#3


5  

ok so this is what I use in order to make sure all of my app is in a en-US culture.

好吧,这就是我使用的,以确保我的所有应用程序都在en-US文化中。

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
XmlLanaguage lang = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(lang));
FrameworkContentElement.LanguageProperty.OverrideMetadata(typeof(System.Windows.Documents.TextElement), new FrameworkPropertyMetadata(lang));

in order to make a single thread in a culture you can make

为了在文化中制作单个帖子,你可以制作

Thread.CurrentThread.CurrentCulture = new CultureInfo("EN-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("EN-US");

#4


3  

Or you could try this:

或者你可以试试这个:

 FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)))

#5


0  

Or try building an appropriate Attached Property like this

或者尝试构建这样的适当附加属性

public class CultureHelper : DependencyObject
{

    public string Culture
    {
        get { return (string)GetValue(CultureProperty); }
        set { SetValue(CultureProperty, value); }
    }




    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CultureProperty =
        DependencyProperty.RegisterAttached("Culture", typeof(string), typeof(CultureHelper), new FrameworkPropertyMetadata("en", CultureChanged));

    private static void CultureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //For testing purposes in designer only 
        if (DesignerProperties.GetIsInDesignMode(d))
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo((string)e.NewValue);
        }

    }

    public static void SetCulture(DependencyObject element, string value)
    {
        element.SetValue(CultureProperty, value);
    }

    public static string GetCulture(DependencyObject element)
    {
        return (string)element.GetValue(CultureProperty);
    }


}

#1


9  

there is no way to set it for all threads in the application, however if you are creating the threads in your application you can set the culture for everyone by yourself as you mentioned above

没有办法为应用程序中的所有线程设置它,但是如果您在应用程序中创建线程,您可以自己为每个人设置文化,如上所述

To set the Culture to the Main Application use the following snippet code:

要将Culture设置为Main Application,请使用以下代码段:

Dim newCulture As CultureInfo = new CultureInfo("fr-FR")
CurrentThread.CurrentCulture = newCulture

#2


7  

Try putting

试试吧

<UICulture>en-US</UICulture>

... in your csproj file.

...在你的csproj文件中。

#3


5  

ok so this is what I use in order to make sure all of my app is in a en-US culture.

好吧,这就是我使用的,以确保我的所有应用程序都在en-US文化中。

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
XmlLanaguage lang = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(lang));
FrameworkContentElement.LanguageProperty.OverrideMetadata(typeof(System.Windows.Documents.TextElement), new FrameworkPropertyMetadata(lang));

in order to make a single thread in a culture you can make

为了在文化中制作单个帖子,你可以制作

Thread.CurrentThread.CurrentCulture = new CultureInfo("EN-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("EN-US");

#4


3  

Or you could try this:

或者你可以试试这个:

 FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)))

#5


0  

Or try building an appropriate Attached Property like this

或者尝试构建这样的适当附加属性

public class CultureHelper : DependencyObject
{

    public string Culture
    {
        get { return (string)GetValue(CultureProperty); }
        set { SetValue(CultureProperty, value); }
    }




    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CultureProperty =
        DependencyProperty.RegisterAttached("Culture", typeof(string), typeof(CultureHelper), new FrameworkPropertyMetadata("en", CultureChanged));

    private static void CultureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //For testing purposes in designer only 
        if (DesignerProperties.GetIsInDesignMode(d))
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo((string)e.NewValue);
        }

    }

    public static void SetCulture(DependencyObject element, string value)
    {
        element.SetValue(CultureProperty, value);
    }

    public static string GetCulture(DependencyObject element)
    {
        return (string)element.GetValue(CultureProperty);
    }


}