I have a WPF application which needs to support localization. I have 2 buttons (add, delete) and a combo box (english, french). When I select french, button’s content should change to (ajouter, effacer).
Now, my question is, what would be the downside or drawback if I manually set the buttons content in code behind? See code below.
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ResourceDictionary currentLanguage = new ResourceDictionary();
switch (comboBox.Text)
{
case "english":
currentLanguage.Source = new Uri("../en-US.xaml", UriKind.RelativeOrAbsolute);
break;
case "france":
currentLanguage.Source = new Uri("../fr-FR.xaml", UriKind.RelativeOrAbsolute);
break;
}
btnAdd.Content = Resources.GetString("insert");
btnDelete.Content = Resources.GetString("delete");
}
I know its kind of weird but kindly answer it. Why shouldn’t I localize my application this way.
Thanks!
When swapping resource dictionaries you should utilize using
DynamicResourcesinstead ofStaticResourcesBasically in XAML you would do the following:XAML
Where Insert and Delete are in your Resource Dictionaries using the x:Key
When you swap the dictionary then these will dynamically update