Search code examples
c#.netwpflocbaml

Can I use WPF window constructor to change culture?


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);
        }

Solution

  • 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)