Reputation: 3
I'm creating a C# WPF MVVM application and currently busy with adding different languages. Right now I use a xaml file which contains a lot of rows like:
<system:String x:Key="btnNew">New</system:String>
<system:String x:Key="btnEdit">Edit</system:String>
The program reads a specific folder, gets the different language files (English.xaml, Dutch.xaml) and adds the selected file to the Resources.MergedDictionaties:
void updateLanguage(int id)
{
string sSelectedLanguage = obcLanguages[id].sRealName; // Get selected language name
ResourceDictionary dict = new ResourceDictionary(); // Create new resource dictionary
string sSelectedLanguageUrl = "pack://siteoforigin:,,,/Languages/" + sSelectedLanguage; // Create url to file
dict.Source = new Uri(sSelectedLanguageUrl, UriKind.RelativeOrAbsolute);
App.Current.MainWindow.Resources.MergedDictionaries.Add(dict); // Add new Language file to resources. This file will be automaticly selected
}
My xaml content with buttons refers with the DynamicResource
to the 'bntNew':
<Button x:Name="btnNewSeam" Content="{DynamicResource btnNew}"Style="{StaticResource BtnStyle}" Command="{Binding newSeamTemplateCommand}" />
This works fantastic. But right now i'm using Observablecollections for comboboxes and I'm adding options in code like this:
obcFunctionsPedalOptions.Add(new clCbbFilltype1("Block", 0));
obcFunctionsPedalOptions.Add(new clCbbFilltype1("Free", 1));
obcFunctionsPedalOptions.Add(new clCbbFilltype1(">0", 2));
But the options "Block", "Free" and ">0" have to be translated with information from the Language.xaml file.
How can I read this specific Xml file with code, get for an example the PedalOptions
x:Key and read its options?
Example:
<system:String x:Key="PedalOptions">Block, Free, >0</system:String>
Upvotes: 0
Views: 690
Reputation: 3
Thanks for your answer, i did make it work like you told me and the MSDN page:) So, right now i have one default "LanguageResource.resx" file added to my resources and did test it with a second "LanguageResource.nl.resx" file, and changed the language with CultureInfo("nl").
But, my customer wants a folder where he can put different "LanguageResource.XX.resx" files, the application adds this files from the folder, and he is able to change the language.
My customers doesn't want to compile this program everytime he adds a new language. So how do i runtime add a few .resx files to the resource manager?
Upvotes: 0
Reputation: 137178
Rather than define the strings in XAML use a resource (.resx) files. You have a default (ResourceFile.resx) file and then language specific ones (ResourceFile.nl.resx for Dutch, ResourceFile.en-US.resx for US English, etc). The system picks the correct resource file based on the current culture settings.
The MSDN has a section on this which you should read.
You can then just have the following in your code:
obcFunctionsPedalOptions.Add(new clCbbFilltype1(Resources.ResourceFile.Block, 0));
You'll need have make this visible to the XAML as follows by defining a class:
public class Localize : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyChange(String name)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
}
#endregion
//Declarations
private static ResourceFile _localizedStrings = new ResourceFile();
public ResourceFile LocalizedStrings
{
get { return _localizedStrings; }
set { NotifyChange("LocalizedStrings"); }
}
}
Then reference this from the XAML:
<local:Localize x:Key="LocalizedStrings"
xmlns:local="clr-namespace:Module" />
You can then use it as follows:
<TextBlock Text="{Binding LocalizedStrings.Block, Source={StaticResource LocalizedStrings}}" />
Upvotes: 2