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