0

I am learning about interface localization and am using WPF in Visual Studio 2008. My goal for my program is to make it so that the user can click a button and change the language displayed in the window. I have been trying to change my resource files at run time in the code-behind section when the user clicks a button.

I have read through this question: Programmatically change resource file language (resx) in Code Behind

However, this doesn't seem to be a solution for me because I want to make the above change on a button click event. When i try to do this it seems as though I can't use this solution because the protected function from the other question cannot go inside the action event.

Here is what I've tried with no luck, based off of the previous question:

/// Change to English-US
        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            protected override void InitializeCulture()
            {
               base.InitializeCulture();
               System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            }
        }

/// Change to German
        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            protected override void InitializeCulture()
            {
               base.InitializeCulture();
               System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
            }
        }

I've been programming in school and on my own for a couple of years now, so I'm not an absolute noobie. However, I am new to C# and WPF, which is what I'm working with now. From what I know, you can't have a function within a function, like I have in my code. The only reason I put it there is because I need it to work with the button click event.

Help with this would be greatly appreciated.

Here is the new code:

namespace LocalizationConcept
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            Properties.Resources.Culture = new CultureInfo(ConfigurationManager.AppSettings["Culture"]);
            ///InitializeComponent();
        }

        /// Exit Button
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void ChangeCulture(string cultureKey)
        {
            try
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureKey);
            }
            catch (Exception err)
            {
                System.Environment.Exit(0);
            }
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            ChangeCulture("en-US");
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            ChangeCulture("de-DE");
        }
    }
}
Community
  • 1
  • 1
Eric after dark
  • 1,768
  • 4
  • 31
  • 79

2 Answers2

1

It seems like WPF doesn't support the same features of Winforms by speaking of localization and gloablization. Although I found some interesting articles which show step by step how to replace a language at runtime, on the fly.

  • WPF Localization - On-the-fly Language Selection:
    • It provides an option for replacing languages at runtime, on-the-fly
    • It performs better than a lame XML, XPath based binding solution
    • It can be used via Styles, Control Templates and Data Templates
    • It translates a formatted text with parameters, using default and custom formatters
    • It provides a Translate custom markup-extension to write an elegant XAML

  • WPF Runtime Localization
    • Switch cultures at runtime - automatically updating all localized elements
    • Use the existing Resource file structure (*.resx files), which can be maintained in Visual Studio
    • Keep design-time support for localized elements working in Expression Blend (and hopefully other XAML design applications)
Omar
  • 16,329
  • 10
  • 48
  • 66
0

It doesn't need to be in it's own function (although it could be). Consider the following:

    private void ChangeCulture(string cultureKey)
    {
        try
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureKey);
        }
        catch (Exception err)
        {
            // That culture probably doesn't exist
        }
    }

    private void ButtonA_Click(object sender, EventArgs args)
    {
        ChangeCulture("en-US");
    }

    private void ButtonB_Click(object sender, EventArgs args)
    {
        ChangeCulture("de-DE");
    }

EDIT:

I found this on another post and it was awarded +100 bounty, so I imagine it's a great solution. Check it out.

Community
  • 1
  • 1
Will Custode
  • 4,576
  • 3
  • 26
  • 51
  • I totally erased the button click events that I had before, and entered this in. The compiler didn't have a problem, but the language does not change when the button is clicked. I will post the new code. -It might also be important to note that I have an app.config file that I've been using to change the language before I run the program. – Eric after dark Jul 08 '13 at 18:38
  • I just added a link to a similar post that has a great answer for this, albeit a little more involved. – Will Custode Jul 08 '13 at 18:48
  • I'm trying to localize all of the text in my Interface. There's text on buttons, text boxes and tab items. Will that link work for those as well? The only reason I'm asking is because they always code strings into the XAML window as an example. – Eric after dark Jul 08 '13 at 19:29