Creating Text Resources

I l @ ve RuBoard

There are several ways you can create text resources for your programs. The simplest is by creating a text file that has a set of name-value pairs. These specify a name that you can use in your code to find the resource and the text to associate with that name .

When you've finished making your basic string resource file, you must turn it into a .resx or .resource file using the .NET tool RESGEN .EXE . The following example shows this process.

The String Resource Text

The following simple text file shows a set of name/-value pairs for a resource that will be used to provide the default strings for an application. Note that quotation marks are not required on strings. If you use quotes, they will be embedded in the resource string.

 #Default culture resources WindowText = Internationalization example LabelText = Hello World!!! 

The text file can be converted into one of two forms ”an XML-based form that is stored as a .resx file or directly to its compiled .resource form. The .resx file is an XML dataset that is used to serialize resource information during development. Conversion of resources from one form to another is accomplished using the Resgen utility. Type in the previous simple resource text and save it as firstresource.txt , and then create a .resx file using the following command line:

 resgen firstresource.txt firstresource.resx 

The utility will compile the file and tell you that two resources have been converted. Listing 3.4.2 shows the resulting XML file.

Listing 3.4.2 firstresource.resx: The Converted Resource File in XML Form
 <?xml version="1.0" encoding="utf-8"?> <root>   <xsd:schema id="root" targetNamespace="" xmlns="" xmlns:xsd="http://www.w3.org/2001/ graphics/ccc.gif XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">     <xsd:element name="root" msdata:IsDataSet="true">       <xsd:complexType>         <xsd:choice maxOccurs="unbounded">           <xsd:element name="data">             <xsd:complexType>               <xsd:sequence>                 <xsd:element name="value" type="xsd:string" minOccurs="0" graphics/ccc.gif msdata:Ordinal="1" />                 <xsd:element name="comment" type="xsd:string" minOccurs="0" graphics/ccc.gif msdata:Ordinal="2" />               </xsd:sequence>               <xsd:attribute name="name" type="xsd:string" />               <xsd:attribute name="type" type="xsd:string" />               <xsd:attribute name="mimetype" type="xsd:string" />             </xsd:complexType>           </xsd:element>           <xsd:element name="resheader">             <xsd:complexType>               <xsd:sequence>                 <xsd:element name="value" type="xsd:string" minOccurs="0" graphics/ccc.gif msdata:Ordinal="1" />               </xsd:sequence>               <xsd:attribute name="name" type="xsd:string" use="required" />             </xsd:complexType>           </xsd:element>         </xsd:choice>       </xsd:complexType>     </xsd:element>   </xsd:schema>   <data name="WindowText">     <value>Internationalization example</value>   </data>   <data name="LabelText">     <value>Hello World!!!</value>   </data>   <resheader name="ResMimeType">     <value>text/microsoft-resx</value>   </resheader>   <resheader name="Version">     <value>1.0.0.0</value>   </resheader>   <resheader name="Reader">     <value>System.Resources.ResXResourceReader</value>   </resheader>   <resheader name="Writer">     <value>System.Resources.ResXResourceWriter</value>   </resheader> </root> 

You can see from Listing 3.4.2 that this is probably not a file you will want to type in by hand. The first part of the file is the schema that defines XML content of the file. Each element is contained within an XML node tagged <data name="some-name"> . If you look through the file, you will see the two resources that were specified in the text file and their respective values. It is this .resx form of the resources that Visual Studio maintains when you create resources in your applications.

To use the resource file in a sample program, you will need to convert it to its compiled form. You can do this from either the text format or the XML format by using one of the following command lines:

 Resgen firstresource.txt firstresource.resources 

or

 Resgen firstresource.resx firstresource.resources 

A Simple Winforms Application That Relies on a Resource

To test the resource file shown in Listing 3.4.2, you can create a simple "Hello World"-type application as shown in Listing 3.4.3. This program relies on the firstresource.resources file created previously to provide the text for the label and the window title bar.

Listing 3.4.3 hellores.cs: Using Simple String Resources
 1: namespace Sams  2: {  3:    using System;  4:    using System.Drawing;  5:    using System.Collections;  6:    using System.ComponentModel;  7:    using System.Globalization;  8:    using System.Windows.Forms;  9:    using System.Data; 10:    using System.Resources; 11: 12:    class hellores: Form 13:    { 14: 15:      public hellores() 16:      { 17:5         ResourceManager rm=new ResourceManager( "firstresource", graphics/ccc.gif this.GetType().Assembly); 18: 19:         this.Size = new Size(400,100); 20:         this.Text=rm.GetString("WindowText"); 21: 22:         Label l = new Label(); 23:         l.Location = new Point(3,5); 24:         l.Size = new Size(394,90); 25:         l.Font = new Font("Tahoma",36F,FontStyle.Bold); 26:         l.Text=rm.GetString("LabelText"); 27:         this.Controls.Add(l); 28: 29:      } 30: 31:      static void Main() 32:      { 33:         Application.Run(new hellores()); 34:      } 35:    } 36: 37: } 

The code in Listing 3.4.3 is a good global citizen application. It embeds no strings, and draws all of its text from the resource set provided. Line 17 of the listing shows how a resource manager is created, and lines 20 and 26 show how the named strings are retrieved from the resources in the assembly and applied to the Windows Forms elements.

To build this file and run it, you will need to use the following command line:

 csc /out:hellores.exe /t:winexe /res:firstresource.resources hellores.cs 

Notice how the command line option /res: is used to embed the compiled form of the resources created earlier.

Running the program now will produce the result shown in Figure 3.4.1.

Figure 3.4.1. The hellores program in operation.

graphics/0304fig01.gif

Creating and Using a Satellite Assembly

The globalization example is not much use without an alternative resource set from which to draw. In the following example, we'll add a satellite assembly containing a French resource set.

NOTE

A satellite assembly is a DLL, containing code or data, that is used by an application's main assembly.


A satellite assembly, in this case, is analogous to a resource-only DLL. Satellite assemblies for localization are normally private to a particular application. They are stored in a specially named subdirectory below the main application directory. In this case, you'll need to create a directory called fr so that the resource manager can find it.

First, create the resource text using the same names but different values for every string that needs to be displayed. Notice that only the label text changes here. This will be explained shortly.

 #Version Francaise. LabelText = Bonjour le monde!!! 

Call this file firstresource.fr.txt . After this file is complete, you can create the satellite assembly using the following command lines:

 resgen firstresource.fr.txt firstresource.fr.resources al /out:fr/hellores2.Resources.DLL /c:fr /embed:firstresource.fr.resources 

The utility program invoked in the second line is the Assembly Linker (AL). AL can be used for creating assemblies of all sorts, but here we're only interested in its resource packaging abilities . Notice that the /out: command line option places the assembly DLL into the fr directory.

Now you can build the hellores2.exe program. It's very similar to the original but has a command line option that allows you to select the culture by typing in the culture identification string. Listing 3.4.4 shows the source.

Listing 3.4.4 hellores2.cs: The Localized Hello World Example
 1: namespace Sams  2: {  3:    using System;  4:    using System.Drawing;  5:    using System.Collections;  6:    using System.ComponentModel;  7:    using System.Globalization;  8:    using System.Windows.Forms;  9:    using System.Data; 10:    using System.Resources; 11:    using System.Threading; 12: 13:    class hellores: Form 14:    { 15: 16:      private Label l; 17:      private ResourceManager rm; 18: 19: 20:      public hellores(string culture) 21:      { 22:         Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); 23: 24:         rm=new ResourceManager("firstresource", this.GetType().Assembly); 25: 26:         this.Size = new Size(400,100); 27:         this.Text=rm.GetString("WindowText"); 28: 29:         l = new Label(); 30:         l.Location = new Point(3,5); 31:         l.Size = new Size(394,90); 32:         l.Font = new Font("Tahoma",36F,FontStyle.Bold); 33:         l.Text=rm.GetString("LabelText"); 34:         l.Anchor = (AnchorStyles.Top  35:                 AnchorStyles.Left  36:                 AnchorStyles.Bottom  37:                 AnchorStyles.Right); 38:         this.Controls.Add(l); 39: 40:      } 41: 42:      static void Main(string[] args) 43:      { 44:         string culture =""; 45:         if(args.Length == 1) 46:            culture = args[0]; 47:         Application.Run(new hellores(culture)); 48:      } 49:    } 50: 51: } 

There are a few simple changes to the program in Listing 3.4.4. Line 22 forces a setting for the current user interface culture based on the input from the command line. The properties of the label are modified on lines 34 “37 so that the label changes size with the form. In this example, you will need to resize the form to see the whole label text. This shows another aspect of internationalization that you have to contend with, the possibility that physical resource sizes might be different across cultures. It is possible to store the size and placement information of your resources in the satellite assembly also. Visual Studio does this for you, and we'll look at that in a moment. Otherwise, the use of the string resources is identical.

The application can be compiled with the following command line:

 csc /out:hellores2.exe /t:winexe /res:firstresource.resources hellores2.cs 

Now the program can be run and an fr culture selected by invoking it as follows :

 hellores2 fr 

In this circumstance, you'll see the application displaying the French resource string, as shown in Figure 3.4.2.

Figure 3.4.2. The French connection to resources.

graphics/0304fig02.gif

There was a reason for only modifying one of the resources in the French satellite assembly. This illustrates that the resource manager will use strings and other resources from the correct locale or culture if they exist, but it will fall back to the default information in the main assembly if they do not. The "WindowText" string is not in the satellite assembly, so the default was used.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net