I've recently been delving into some globalization (or globalisation for us Brits) and didn't particularly like .NET's way of quietly making you split your resources up.
Perhaps I'd better explain this. The Windows Forms designer includes good support for localization, you can utilise this by setting the
Localizable property of the
true and changing the
Language and simply type away. If, however, you need to include separate messages into your program, you must create a separate resource file and load things from there.
Now I'd assume (read: I haven't checked) that .NET lumps all these resources together when it compiles the application, but supposing you want someone else to translate stuff for you, you must manage several files: one for your custom messages and one for every form in the application.
So in summary, I wanted a solution in which I'd only have to manage one resource file per supported language.
Localizer is a simple
Component which implements the
IExtenderProvider interface. It provides a property called
ResourceID. There is only one important property for the component,
ResourceManager. This can be set to point to an instance of your project resource manager, wherever that may be.
By setting the
ResourceManager property, you enable
Localizer to lookup localized strings based on the
ResourceID of each control and set the
Text property of that control.
Localizer you can simply add a reference to the assembly and drop the component onto the form. Once that is done, somewhere you must set the
ResourceManager property. Once this is done, no more is required at runtime because changing the
ResourceManager automatically refreshes all the
You can assign a
ResourceID to each control you wish to be localized. Note: If the
ResourceID is an empty string, then the text will always remain as the text specified in the designer or code.
In VS2005, perhaps the simplest solution to adding localized strings is to simply add more
.resx files to the project, naming them in the pattern seen in the screenshot. Visual Studio will then perform the necessary operations to compile and embed these resources.
The final stage would be to put the translated strings / messages for each
ResourceID into each of the
A simple demonstration application has been included which dynamically changes the text of controls based on the
CurrentUICulture. There is a textbox to change this culture while the application is running.
As usual, I welcome any suggestions for improvements, bug fixes and the usual stuff that we have to deal with in our daily lives as developers.
- 16th July, 2007: Initial post
Ed is a student who due to a form of cancer (now clear) took a year out before going to Imperial College, London to study Electronic Engineering.
His interests include shooting (clay-pigeon (shotgun), air-rifle and rifle), playing with his three labradors (Sandy, Rosie and Tundra), programming (most experienced in C# and C, although those are not the only ones), walking (has completed Gold Duke of Edinburgh's Award), playing games and reading.
He lives in two places on a 57 acre farm in West Waleswith the rest of the family during the holidays; and Greater London during term time.
1st Year MEng Electronics Engineering (Imperial College, London)