03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Resource Files ❘ 407<br />

Localization Resources<br />

One of the most important reasons for inventing resource files was to allow localization: supporting<br />

different text, images, and other items for different languages and cultures. Resources make localization<br />

in Visual Studio .NET easy.<br />

First, create a form using whatever language you typically use from day to day. For me, that’s English<br />

as spoken in the United States. Open the form in the form designer and give it whatever controls you<br />

need. Set the form’s and controls’ properties as usual.<br />

Next, set the form’s Localizable property to true and set the form’s Language property to the<br />

first language you want to support other than the default language that you have been working<br />

with so far. Modify the controls’ properties for the new language.<br />

As you modify a form, Visual Studio saves the changes you make to a new resource file attached to<br />

the form. If you open Solution Explorer and expand the form’s entry, you can see these resource files<br />

below the form’s file.<br />

Example program Localized uses default settings for United States English. It also includes localizations<br />

for generic German (as opposed to German as spoken in Switzerland, Germany, Liechtenstein,<br />

or some other country). If you expand the form’s entry in Solution Explorer, you’ll find the files<br />

Form1.resx holding the default settings and Form1.de.resx holding the German settings.<br />

When you build the program, Visual Studio compiles the resources and saves them in a DLL file<br />

named Localized.resources.dll in a directory named after the locale. (The “Localized” part of the file’s<br />

name comes from the program’s name.) At run time, the application automatically checks the computer’s<br />

regional settings and selects the resource file that matches most closely.<br />

For example, suppose your computer is configured for the German/Switzerland (de-CH) locale.<br />

The program first looks for Localized.resources.dll in a subdirectory named de-CH. If it doesn’t<br />

find the DLL, it looks for the generic German resources DLL in the subdirectory de (the code for<br />

generic German). If the program still can’t find a resource DLL, it uses the default implementation<br />

that is included in the form itself.<br />

Later, if you need to modify the resources for a locale, you can rebuild the application and then copy<br />

the new Localized.resources.dll into the appropriate subdirectory.<br />

Normally, you should let the application pick the appropriate resource file automatically, but you<br />

can explicitly select a resource file for testing purposes. To do that, open the form’s code file and<br />

add the following using directives.<br />

using System.Threading;<br />

using System.Globalization;<br />

Next, find the form’s constructor and add the bold lines shown in the following code.<br />

public Form1()<br />

{<br />

// Set the culture and UI culture to German.<br />

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");<br />

Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");<br />

}<br />

InitializeComponent();<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!