I am trying to change my text binded to a resource file in run time.
For that I am following this tutorial:
https://codinginfinity.me/post/2015-05-10/localization_of_a_wpf_app_the_simple_approach
I simply created a WPF project, added 2 resource file, the first one named Resource.resx and the second one named Resource.pt-PT.resx, both of them have a field called Tag1. Then, I created a class with the code given in the previous tutorial:
namespace WpfApplication1
{
public class TranslationSource
: INotifyPropertyChanged
{
private static readonly TranslationSource instance = new TranslationSource();
public static TranslationSource Instance
{
get { return instance; }
}
private readonly ResourceManager resManager = Properties.Resources.ResourceManager;
private CultureInfo currentCulture = null;
public string this[string key]
{
get { return this.resManager.GetString(key, this.currentCulture); }
}
public CultureInfo CurrentCulture
{
get { return this.currentCulture; }
set
{
if (this.currentCulture != value)
{
this.currentCulture = value;
var @event = this.PropertyChanged;
if (@event != null)
{
@event.Invoke(this, new PropertyChangedEventArgs(string.Empty));
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class LocExtension
: Binding
{
public LocExtension(string name)
: base("[" + name + "]")
{
this.Mode = BindingMode.OneWay;
this.Source = TranslationSource.Instance;
}
}
}
And finally created a simple interface with the following XAML code:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ns="clr-namespace:TranslationSource"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Margin="10">
<Button Content="{x:Static ns:Loc Tag1}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</StackPanel>
</Grid>
</Window>
This is what my Button_Click event is doing:
private void Button_Click(object sender, RoutedEventArgs e)
{
TranslationSource.Instance.CurrentCulture = new System.Globalization.CultureInfo("pt-PT");
}
My current problem is connecting the TranslationSource code with my UI, the tutorial misses that part, probably because its something very simple, but unfortunatelly I am not very expert in WPF... Can anybody explain me what are the next steps?