I use the well known LocBaml approach to change culture.

It works fine here:

public App()
{ 
    // Test code 
    bool override_current_ui_language = true;
    string locale = "es-ES";
    if (override_current_ui_language)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(locale);
        Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
    }    
} 

But when I use the same under WPF Window class controller it doesn't work.

Any clue why is it?


I use this but it doesn't work as well.

void cmbLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string locale = "es-ES";
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(locale);
            Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
        }
有帮助吗?

解决方案

I use LocalizeExtension for that.

In the .xaml you just use {LocText NAMESPACE::RESOURCENAME} to set the text and in the code behind the following to change the language on the fly:

Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
LocalizeDictionary.Instance.Culture = culture;

You can check my Project on Codeplex, where I use it, to see an extended example:
XAML and CodeBehind (->SetUICulture)

其他提示

It should work in the constructor if you do it before the InitalizeComponent() call, but your example shows an event handler. Once InitializeComponent() is called the BAML has been loaded from the resources and you won't be able to change it.

You can use an approach like the one illustrated here https://www.codeproject.com/articles/29800/webcontrols/ to create a new window with your new culture:

void SwitchCulture(CultureInfo newCulture)
{
    Thread.CurrentThread.CurrentUICulture = newCulture;
    Thread.CurrentThread.CurrentCulture = newCulture;
    // Reload all the merged dictionaries to reset the resources.
    List<Uri> dictionaryList = new List<Uri>();
    foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
    {
        dictionaryList.Add(dictionary.Source);
    }
    Application.Current.Resources.MergedDictionaries.Clear();
    foreach (Uri uri in dictionaryList)
    {
        ResourceDictionary resourceDictionary1 = new ResourceDictionary();
        resourceDictionary1.Source = uri;
        Application.Current.Resources.MergedDictionaries.Add( resourceDictionary1 );
    }

    MyWindowClass newWindow = new MyWiondowClass();
    // TODO: Attach any view model so the new window looks like the old one
    newWindow.Show();
    this.Close();
} 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top