Eric after dark
Eric after dark

Reputation: 1808

XAML C# Localization problems

Okay, so the overall goal of this prototype is to create a GUI window using WPF in C# that allows the user to change languages from a menu. At the moment I am trying to switch some of the components of my window from English to German using an App.config file. I know that will not allow the assets to change using a button, but it's a start.

I have 3 resource files. One is a default (English), another is English as well, labelled en-US, and the last one is German, labelled de-DE. They are all PUBLIC.

I have used these two videos as guide lines:

http://www.youtube.com/watch?v=5MuN6VOw9r4

http://www.youtube.com/watch?v=BK7jp3snwCQ

I set the content of a label and two buttons in the XAML section:

<Label Content="{x:Static properties:Resources.Label1}" Height="28" Margin="6,6,79,0" Name="Label1" VerticalAlignment="Top"></Label>
<Button Content="{x:Static properties:Resources.Button1}" Height="23" HorizontalAlignment="Left" Margin="6,40,0,0" Name="Button1" VerticalAlignment="Top" Width="75"></Button>
<Button Content="{x:Static properties:Resources.Button2}" Height="23" HorizontalAlignment="Left" Margin="6,69,0,0" Name="Button2" VerticalAlignment="Top" Width="75"></Button>

This is what I have in my App.config file (de-DE) should change the edited content to German:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Culture" value="de-DE" />
  </appSettings>
</configuration>

Lastly, my xaml.cs file has the following in it:

using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Globalization;
using System.Configuration;

namespace WPF_Prototype1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            /// This line should allow the program to read the App.config file!
            Properties.Resources.Culture = new CultureInfo(ConfigurationManager.AppSettings["Culture"]);
        }
    }
}

Why is my program still not changing these icons to German? What can I do to fix it?

Thank you

Upvotes: 1

Views: 2098

Answers (2)

Eric after dark
Eric after dark

Reputation: 1808

The first thing that i did to fix my problem was to create a new project. Under the new project I created the same 3 Resource files and added them to the project properties. After that I created the same App.config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Culture" value="de-DE" />
  </appSettings>
</configuration>

I also left the same code in the xaml.cs file:

Properties.Resources.Culture = new CultureInfo(ConfigurationManager.AppSettings["Culture"]);

After making sure that all of the names were matching in my resource files I wrote out the following code in the xaml design window:

<Label Content="{x:Static properties:Resources.LabelFirstName}" Height="28" HorizontalAlignment="Left" Margin="33,29,0,0" Name="labelFName" VerticalAlignment="Top" />
        <Label Content="{x:Static properties:Resources.LabelLastName}"  Height="28" HorizontalAlignment="Left" Margin="34,65,0,0" Name="labelLName" VerticalAlignment="Top" />
        <Label Content="{x:Static properties:Resources.LabelAddress}"   Height="28" HorizontalAlignment="Left" Margin="35,103,0,0" Name="labelAddress" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="119,26,0,0" Name="textBoxFName" VerticalAlignment="Top" Width="152" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="119,63,0,0" Name="textBoxLName" VerticalAlignment="Top" Width="152" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="119,100,0,0" Name="textBoxAddress" VerticalAlignment="Top" Width="152" />
        <Button Content="{x:Static properties:Resources.EnglishButton}" Height="23" HorizontalAlignment="Left" Margin="177,155,0,0" Name="englishButton" VerticalAlignment="Top" Width="94" />
        <Button Content="{x:Static properties:Resources.GermanButton}" Height="23" HorizontalAlignment="Left" Margin="177,207,0,0" Name="germanButton" VerticalAlignment="Top" Width="94" />

I think the difference was the name of the asset from the resource file being DIFFERENT from the name in the xaml design window. For example, my first lable is called "LabelFirstName" in the resource files, but it's called labelFName in the xaml window. I also didn't drag and drop any of the components. I made sure that they were in written format.

Upvotes: 1

AlSki
AlSki

Reputation: 6961

I have done something similar which I can confirm works, and changes at run time too. Looking back at the code you might need to get INotifyPropertyChanged involved.

Firstly I have properties on the ViewModel for the Culture that also notify of changes to all texts

public CultureInfo CurrentUICulture
{
  get { return Properties.Resources.Culture; }
  set
  {
    Properties.Resources.Culture = value;

    OnPropertyChanged("Title");
    OnPropertyChanged("Description");
    OnPropertyChanged("FarenheitLabel");
    OnPropertyChanged("CelsiusLabel");
    OnPropertyChanged("Language");
  }
}

And properties exposed as (note the comment)

// We could have bound the UI directly to Properties.Resources then we wouldn't be able to 
// NotifyPropertyChanged when the culture changes 
public string Title { get { return Properties.Resources.Title; } }
public string Description { get { return Properties.Resources.Description; } }
public string FarenheitLabel { get { return Properties.Resources.FarenheitLabel; } }
public string CelsiusLabel { get { return Properties.Resources.CelsiusLabel; } }
public string Language { get { return Properties.Resources.Language; } }

And then I pull it directly from the ViewModel as

<Label Grid.Row="5" Grid.ColumnSpan="2"
            Content="{Binding Language, FallbackValue=Language}"/>

Upvotes: 0

Related Questions