15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Windows forms localization Using Visual studio ❘ 587<br />

The class ComponentResourceManager is derived from ResourceManager <strong>and</strong> offers the method<br />

ApplyResources(). With ApplyResources(), the resources that are defined with the second argument are<br />

applied to the object in the first argument.<br />

The following XML segment shows a few of the properties of textBoxTitle: the Location property has a<br />

value of 29, 164; the Size property has a value of 231, 20; the Text property is set to Professional <strong>C#</strong><br />

4 with .<strong>NET</strong> 4; <strong>and</strong> so on. For every value, the type of the value is stored as well. For example,<br />

the Location property is of type System.Drawing.Point, <strong>and</strong> this class can be found in the assembly<br />

System.Drawing.<br />

Why are the locations <strong>and</strong> sizes stored in this XML file With translations, many strings have completely<br />

different sizes <strong>and</strong> no longer fit into the original positions. When the locations <strong>and</strong> sizes are all stored inside<br />

the resource file, everything that is needed for localization is stored in these files, separate from the <strong>C#</strong> code:<br />

<br />

Top, Left, Right<br />

<br />

<br />

29, 164<br />

<br />

<br />

231, 20<br />

<br />

<br />

3<br />

<br />

<br />

Professional <strong>C#</strong> 4 with .<strong>NET</strong> 4<br />

<br />

<br />

textTitle<br />

<br />

<br />

System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0,<br />

Culture=neutral, PublicKeyToken=b77a5c561934e089<br />

<br />

<br />

$this<br />

<br />

<br />

2<br />

<br />

code snippet BookOfTheDay/BookOfTheDayForm.resx<br />

When changing some of these resource values, it is not necessary to work directly with the XML code.<br />

You can change these resources directly in the Visual Studio Designer. Whenever you change the Language<br />

property of the form <strong>and</strong> the properties of some form elements, a new resource file is generated for the<br />

specified language. Create a German version of the form by setting the Language property to German, <strong>and</strong><br />

a French version by setting the Language property to French. For every language, you get a resource file<br />

with the changed properties: in this case, BookOfTheDayForm.de.resX <strong>and</strong> BookOfTheDayForm.fr.resX.<br />

The following table shows the changes needed for the German version.<br />

german name<br />

$this.Text (title of the form)<br />

labelItemsSold.Text<br />

labelBookOfTheDay.Text<br />

Value<br />

Buch des Tages<br />

Bücher verkauft:<br />

Buch des Tages:<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!