Click here to Skip to main content
Click here to Skip to main content

Localizer

By , 16 Jul 2007
 

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)

About the Author

Ed.Poore
Engineer PooreDesign
United Kingdom United Kingdom
Member
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)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralHave a Look at this ComponentmemberPatrick Blackman10 Jun '09 - 3:04 
Rose | [Rose] Have a look at this link, I think this is what you want to achieve.
http://www.reflectionit.nl/Articles/LocalText.aspx"
QuestionWhat about design time support?memberKanangra27 Jul '07 - 15:28 
This is an interesting approach. The main problem I see however is the lack of design time support. The standard .NET localization approach allows you to localize much more than just text. It is often necessary, for instance, to make changes to sizes and locations of controls for different languages (because of the different space requirements). Some languages may require specific fonts. The standard approach allows you to see exactly what the form/control will look like at design time.
 
I agree that the number of resx files generated for a large project can be difficult to manage - but probably the best way to address this is by using a localization tool. These can generally suck the resources out of the multiple resource files and allow them to be managed (and sent to a translator) as a single entity. Once the translation is complete the localization tool can then update the resx files (or even build the binary satellite resources). There are a number of such tools (Sizuliser, Lingobit, Multilizer) on the market - however most of them are quite expensive and are not designed specifically for .NET. Infralution has just released a new reasonably priced product (Globalizer.NET) that integrates closely with Visual Studio. You might want to take a look at it.

AnswerRe: What about design time support?memberEd.Poore28 Jul '07 - 8:59 
Kanangra wrote:
What about design time support?

I fail to see how this is applicable since this is a component specifically for helping at the design time stage.  Granted it is also applicable at runtime but it was written with design time in mind.
Kanangra wrote:
You might want to take a look at it.

I might, but this was written with a very specific purpose in mind and to be honest the project it was intended for, they asked for the stuff to be written in English anyway D'Oh! | :doh:

 

GeneralRe: What about design time support?memberKanangra28 Jul '07 - 14:04 
Ed.Poore wrote:
I fail to see how this is applicable since this is a component specifically for helping at the design time stage. Granted it is also applicable at runtime but it was written with design time in mind.

 
I should have been more specific. I understand your solution allows to set the resource ID for each Text property at design time. What it doesn't do (as far as I can see), that the standard localization model does, is allow you to see at design time the language specific versions of your forms. This allows you to visually verify form elements are large enough to fit the require text and change the layout if necessary for the specific language.

The article Globalization of Windows Application in 20 Minutes using C# has another approach similar to yours. I think that some of the comments by Mihai Nita on that article about the problems with using the single resource file model also apply here.
 
How does your solution cope when there are multiple properties on a control that need to be localized. For instance Text and Tooltip? Since there is only one ResourceID per control.

GeneralRe: What about design time support?memberEd.Poore28 Jul '07 - 20:59 
Kanangra wrote:
How does your solution cope when there are multiple properties on a control that need to be localized

Simple answer?  It doesn't because I didn't want it to be able to do that Roll eyes | :rolleyes: .

 

Generaldoesn't work with several controlsmemberXDG23 Jul '07 - 23:37 
First of all thanks for your code.
I like Localizer very much (because you can change the language at runtime), but ...
 
... it seems like it only works on controls where also tooltip works which would mean that ...
... it doesn't work on menuitems (including menu and contextmenu), statusbaritems and toolbaritems
 
... you can't localize tooltips easy.
... the very best way would be to have only one file for all languages (i could only think of xml)

GeneralRe: doesn't work with several controlsmemberEd.Poore23 Jul '07 - 23:52 
XDG wrote:
it doesn't work on menuitems (including menu and contextmenu), statusbaritems and toolbaritems

That is because it only provides the property for controls derived from Control, MenuItems (e.g. ToolStripMenuItem) derive from System.ComponentModel.Component.  There is a way around this and I shall implement this when I get the chance, basically the ExtenderProvider would have to reflect on the controls to make sure that there is a Text property because this is not define in Component but in Control.
XDG wrote:
you can't localize tooltips easy

What do you mean easily?  It can't at the moment do that fullstop Roll eyes | :rolleyes: , are these tooltips the ones built in to the controls or the ones provided by ToolTipProvider?  If the later then I'm not sure of a way off the top of my head the former I have a possibility.

 

GeneralRe: doesn't work with several controlsmemberXDG23 Jul '07 - 23:59 
Ed.Poore wrote:
There is a way around this and I shall implement this when I get the chance

I am looking forward to your next update Big Grin | :-D
 
Ed.Poore wrote:
If the later then I'm not sure of a way off the top of my head the former I have a possibility.

Maybe if you add your own ToolTipProvider which supports ToolTipText andalso ToopTipResourceID or something?

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 16 Jul 2007
Article Copyright 2007 by Ed.Poore
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid