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.

588 ❘ ChaPTer 22 lOcAlizAtiOn<br />

The following table lists the changes for the French version.<br />

frenCh name<br />

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

labelItemsSold.Text<br />

labelBookOfTheDay.Text<br />

Value<br />

Le livre du jour<br />

Des livres vendus:<br />

Le livre du jour:<br />

By default, images are not moved to satellite<br />

assemblies. However, in the sample application,<br />

the flag should be different depending<br />

on the country. To achieve this, you have to<br />

add the image of the American flag to the file<br />

Resources.resx. You can find this file in the<br />

Properties section of the Visual Studio Solution<br />

Explorer. With the resource editor, select the<br />

Images category, as shown in Figure 22-9,<br />

<strong>and</strong> add the file americanflag.bmp. To make<br />

localization with images possible, the image must<br />

have the same name in all languages. Here, the figure 22-9<br />

image in the file Resources.resx has the name<br />

Flag. You can rename the image in the properties<br />

editor. Within the properties editor, you can also change whether the image should be linked or embedded.<br />

For best performance with resources, images are linked by default. With linked images, the image file must<br />

be delivered together with the application. If you want to embed the image within the assembly, you can<br />

change the Persistence property to Embedded.<br />

The localized versions of the flags can be added by copying the file Resource.resx to Resource.de.resx<br />

<strong>and</strong> Resource.fr.resx <strong>and</strong> replacing the flags with GermanFlag.bmp <strong>and</strong> FranceFlag.bmp. Because a<br />

strongly typed resource class is needed only with the neutral resource, the property CustomTool can be<br />

cleared with the resource files of all specific languages.<br />

Compiling the project now creates a satellite assembly for each language. Inside the debug directory (or<br />

the release, depending on your active configuration), language subdirectories like de <strong>and</strong> fr are created. In<br />

such a subdirectory, you will find the file BookOfTheDay.resources.dll. Such a file is a satellite assembly<br />

that includes only localized resources. Opening this assembly with ildasm, you see a manifest with the<br />

embedded resources <strong>and</strong> a defined locale. The assembly has the locale de in the assembly attributes,<br />

so it can be found in the de subdirectory. You can also see the name of the resource with .mresource;<br />

it is prefixed with the namespace name Wrox.ProCSharp.Localization, followed by the class name<br />

BookOfTheDayForm <strong>and</strong> the language code de.<br />

Changing the Culture Programmatically<br />

After translating the resources <strong>and</strong> building the satellite assemblies, you will get the correct translations<br />

according to the configured culture for the user. The welcome message is not translated at this time. This<br />

needs to be done in a different way, as you’ll see shortly.<br />

In addition to the system configuration, it should be possible to send the language code as a comm<strong>and</strong>line<br />

argument to your application for testing purposes. The BookOfTheDayForm constructor is changed to<br />

allow the passing of a culture string <strong>and</strong> the setting of the culture according to this string. A CultureInfo<br />

instance is created to pass it to the CurrentCulture <strong>and</strong> CurrentUICulture properties of the current<br />

thread. Remember that the CurrentCulture is used for formatting, <strong>and</strong> the CurrentUICulture is used for<br />

loading resources.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!