Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / C#

Localizer

Rate me:
Please Sign up or sign in to vote.
4.43/5 (9 votes)
16 Jul 2007CPOL2 min read 41.5K   648   47   9
A simple component to aid localization of controls

Introduction

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 Form to 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

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.

Example

Multiple Resources

Resource Strings

To use 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 Control.Text properties.

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 .resx files.

Demo Application

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.

Summary

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.

History

  • 16th July, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer PooreDesign
United Kingdom United Kingdom
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.

Languages and Technologies: C#, C, VB6, VB.NET, XAML, (X)HTML, CSS, XSLT, Assembler (PIC), ASP.NET, WPF, Windows.Forms, ASP, VBScript, JavaScript, Pascal / Delphi, XML

Current Stuff:
1st Year MEng Electronics Engineering (Imperial College, London)

Comments and Discussions

 
GeneralHave a Look at this Component Pin
Patrick Blackman10-Jun-09 3:04
professionalPatrick Blackman10-Jun-09 3:04 
QuestionWhat about design time support? Pin
Grant Frisken27-Jul-07 15:28
Grant Frisken27-Jul-07 15:28 
AnswerRe: What about design time support? Pin
Ed.Poore28-Jul-07 8:59
Ed.Poore28-Jul-07 8:59 
GeneralRe: What about design time support? Pin
Grant Frisken28-Jul-07 14:04
Grant Frisken28-Jul-07 14:04 
GeneralRe: What about design time support? Pin
Ed.Poore28-Jul-07 20:59
Ed.Poore28-Jul-07 20:59 
GeneralMessage Closed Pin
DomGries23-Jul-07 23:37
DomGries23-Jul-07 23:37 
GeneralRe: doesn't work with several controls Pin
Ed.Poore23-Jul-07 23:52
Ed.Poore23-Jul-07 23:52 
GeneralMessage Closed Pin
23-Jul-07 23:59
DomGries23-Jul-07 23:59 
GeneralRe: doesn't work with several controls Pin
forcesoul8-Dec-15 22:46
forcesoul8-Dec-15 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.