One of my personal projects is a C# open-source RPG engine, with a WPF front-end. I want it to be usable by people in different countries, in different languages.
So, text in the UI will use strings from resource files. This will make it easy to internationalize the program.
These are the steps to bind a value from a resource file to a property of a WPF window.
Create the resource file
I created a “Resources” folder, in the WPF project, to hold all the resources files.
Next, I created a resource file named “Literals.resx”, to hold all the screen literals – window titles, labels, error messages, etc.
IMPORTANT: Make your resource files public, if you want to bind their values to a WPF UI. Even though the resource file is inside the WPF project, and “internal” should work (based on the normal rules of scope), you need to make the resource file “public”, in order for the data-binding to work.
For this demonstration, I wanted to bind the word “About” to the “Title” property of an About window. So, I created a literal with the name, and value, of “About”.
Then, I added a new WPF Window, named “About.xaml”, to the WPF project.
Bind the resource value to the WPF window’s property
The next step was to add the namespace for the resource file to my WPF window.
I added this to the opening “Window” tag, and named the XML namespace “resources”. Its value is the project namespace, plus the namespace created by my “Resources” folder. So, that attribute looks like this (for my project “ScottsOpenSourceRPG”:
To bind the “About” value, from the resource file, to the window’s Title attribute, I added this line:
You don’t have to name your name space “resources”. But, whatever you name it, you will need to use that same name in the binding for the Title attribute. So, this would also work (using the name “mytext”, instead of “resources”):
Here is the completed Window tag, with a few other attributes set for the window’s size, location, etc.:
It takes a little extra work to prepare your program for internationalization. However, especially with open-source projects, I believe it’s worth the effort.
Once this data-binding is in place, it will be simple to have someone translate the text in the resource file into another language.