ptg 1434 CHAPTER 30 Localizing Applications for Multiple Languages <form id=”form1” runat=”server”> <div> <asp:Calendar id=”Calendar1” Runat=”server” /> </div> </form> </body> </html> The Culture attribute in the <%@ Page %> directive is set to the value id-ID (Indonesian). When the calendar is rendered, Indonesian month names display in the calendar. Using the CultureInfo Class The CultureInfo class contains information about more than 150 different cultures. You can use the methods of this class in your code to retrieve information about a specific culture and use the information when formatting values such as dates, numbers, and currency amounts. To represent a culture with the CultureInfo class, you can instantiate the class by passing a culture name to the class constructor like this: Dim culture As New CultureInfo(“de-DE”) You can also use any of the following methods of the CultureInfo class to retrieve infor- mation about a culture or cultures: . CreateSpecificCulture—Enables you to create a CultureInfo object by supplying the name of a specific culture. . GetCultureInfo—Enables you to create a CultureInfo object by supplying an identi- fier, culture name, or CompareInfo and TextInfo object. . GetCultureInfoByIetfLanguageTag—Enables you to create a CultureInfo object effi- ciently by supplying a culture name. . GetCultures—Enables you to retrieve an array of cultures. The CultureInfo class lives in the System.Globalization namespace. Before you can use the CultureInfo class, you need to import this namespace. From the Library of Wow! eBook ptg 1435 Using the CultureInfo Class 30 Using the CultureInfo Class to Format String Values To this point, the culture has been set at the level of an individual ASP.NET page or the level of an entire ASP.NET application. However, you might need to take advantage of locale-specific formatting at a more granular level. You can use the CultureInfo class to format a particular value independent of the Culture set for the page. When you use the ToString() method to format dates, times, numbers, and currency amounts, you can supply an additional parameter that formats the value in accordance with a specific culture. For example, the page in Listing 30.9 formats two sets of date and time values. LISTING 30.9 ToStringCulture.aspx <%@ Page Language=”C#” %> <%@ Import Namespace=”System.Globalization” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> void Page_Load() { // Get German Culture Info CultureInfo gCulture = new CultureInfo(“de-DE”); // Use culture when formatting strings lblGermanDate.Text = DateTime.Now.ToString(“D”, gCulture); lblGermanPrice.Text = (512.33m).ToString(“c”, gCulture); // Get Indonesian Culture Info CultureInfo iCulture = new CultureInfo(“id-ID”); // Use culture when formatting strings lblIndonesianDate.Text = DateTime.Now.ToString(“D”, iCulture); lblIndonesianPrice.Text = (512.33m).ToString(“c”, iCulture); } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>ToString Culture</title> </head> <body> <form id=”form1” runat=”server”> <div> From the Library of Wow! eBook ptg 1436 CHAPTER 30 Localizing Applications for Multiple Languages <h1>German</h1> Today’s date is: <br /> <asp:Label id=”lblGermanDate” Runat=”server” /> <br /><br /> The price of the product is: <br /> <asp:Label id=”lblGermanPrice” Runat=”server” /> <h1>Indonesian</h1> Today’s date is: <br /> <asp:Label id=”lblIndonesianDate” Runat=”server” /> <br /><br /> The price of the product is: <br /> <asp:Label id=”lblIndonesianPrice” Runat=”server” /> </div> </form> </body> </html> The first date and time is formatted with German cultural conventions, and the second date and time is formatted with Indonesian cultural conventions (see Figure 30.7). Two CultureInfo objects, corresponding to two cultures, are created in the Page_Load() method. From the Library of Wow! eBook ptg 1437 Using the CultureInfo Class 30 FIGURE 30.7 Formatting with the ToString() method. Comparing and Sorting String Values Different cultures follow different conventions when comparing and sorting string values. If you need to compare or sort string values in your code, you should use the String.Compare() method and optionally supply the method with an instance of the CultureInfo object. The String.Compare() method returns one of the following values: . Negative Integer—The first string is less than the second string. . Zero—The first string is equal to the second string. . Positive Integer—The first string is greater than the second string. For example, the following conditional compares two strings, using the current culture set for the page: if (String.Compare(“Hello”, “Hello”) == 0) lblResult.Text = “The strings are the same!”; From the Library of Wow! eBook ptg 1438 CHAPTER 30 Localizing Applications for Multiple Languages The following conditional uses a specific culture to perform a string comparison: if (String.Compare(“Hello”, “Hello”, true, new CultureInfo(“de-DE”)) == 0) lblResult.Text = “The strings are the same!”; In this case, the first two parameters passed to the String.Compare() method are the strings compared. The third parameter indicates whether the comparison performed should be case-sensitive. Finally, the last parameter represents a CultureInfo object. Creating Local Resources If you need to modify the text (or other content) in a page depending on a user’s language, you can take advantage of resource files. Each resource file can contain page text translated into a particular language. The ASP.NET Framework supports two types of resource files: local and global. In this section, you learn how to use local resources. A local resource is scoped to a particular file such as an ASP.NET page. Explicit Localization Expressions The page in Listing 30.10 is a simple page. It contains a button labeled Click Here! and displays the text Thank You! after you click the button. LISTING 30.10 SimplePage.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> protected void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Visible = true; } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Simple Page</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:Button id=”btnSubmit” From the Library of Wow! eBook ptg 1439 Creating Local Resources 30 Text=”Click Here!” OnClick=”btnSubmit_Click” Runat=”server” /> <br /><br /> <asp:Label id=”lblMessage” Text=”Thank You!” Visible=”false” Runat=”server” /> </div> </form> </body> </html> The page in Listing 30.10 displays the same text regardless of the language of the user visiting the page. If you want to display text in different languages for different users, you need to make a few modifications to the page. The page in Listing 30.11 is a localizable version of the same page. LISTING 30.11 LocalizablePage.aspx <%@ Page Language=”C#” UICulture=”auto” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> protected void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Visible = true; } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Localizable Page</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:Button id=”btnSubmit” Text=”<%$ Resources:ClickHere %>” From the Library of Wow! eBook ptg 1440 CHAPTER 30 Localizing Applications for Multiple Languages OnClick=”btnSubmit_Click” Runat=”server” /> <br /><br /> <asp:Label id=”lblMessage” Text=”<%$ Resources:ThankYou %>” Visible=”false” Runat=”server” /> </div> </form> </body> </html> Two types of changes were made to the page in Listing 30.11. First, the <%@ Page %> direc- tive includes a UICulture attribute that is set to the value auto. When a user requests the page, a resource file that matches the user’s preferred browser language loads automatically. NOTE Don’t confuse the Page UICulture property with the Page Culture property. The UICulture property determines which resource files load for the page. The Culture property, on the other hand, determines how date, number, and currency values are formatted. Second, both the Button and Label controls have been modified. The Button control is declared like this: <asp:Button id=”btnSubmit” Text=”<%$ Resources:ClickHere %>” OnClick=”btnSubmit_Click” Runat=”server” /> The value of the Text property is a resource expression. This resource expression retrieves the value of an entry named ClickHere from the loaded resource file. This resource expres- sion is considered to be an explicit resource expression because the property is explicitly set to the value of a particular resource entry. After you localize a page, you can associate a resource file with the page. All the resource files that you want to associate with a page must be added to a special folder named App_LocalResources. You create the App_LocalResources folder in the same folder as the page that you want to localize. For example, if the page is located in the root of your application, you would add the App_LocalResources folder to the root folder. From the Library of Wow! eBook ptg 1441 Creating Local Resources 30 LISTING 30.12 App_LocalResources\LocalizablePage.aspx.es.resx Name Value ClickHere chasque aquí ThankYou ¡Gracias! You associate a resource file in the App_LocalResources folder with a particular page by using the following file-naming convention: page name.[culture name].resx For example, all the following resource files are associated with the LocalizablePage.aspx page: LocalizablePage.aspx.resx LocalizablePage.aspx.es-PR.resx LocalizablePage.aspx.es.resx The first resource file is the default resource file. If none of the other resource files match the user’s language settings, the contents of the default resource file are used. The second resource filename includes the specific culture name es-PR (Puerto Rican Spanish). If a user’s browser is set to Puerto Rican Spanish, the contents of this resource file are loaded. Finally, the third resource filename includes the neutral culture name es (Spanish). If a user’s preferred language is Spanish, but not Puerto Rican Spanish, the contents of this resource file are loaded. You create a resource file when using Visual Web Developing by right-clicking an App_LocalResources folder, selecting Add New Item, and Assembly Resource file. Visual Web Developer automatically displays an editor for the resource file. The editor enables you to enter name and value pairs. For example, the LocalizablePage.aspx.es.resx resource file contains the two name/value pairs in Listing 30.12. Behind the scenes, resource files are XML files. You can open a resource file in Notepad and edit its contents. The ASP.NET Framework dynamically compiles resource files into assemblies in the background. Implicit Localization Expressions As an alternative to using explicit localization expressions, you can use an implicit local- ization expression. An implicit localization expression enables you to localize multiple control properties with one resource key. The page in Listing 30.13 uses implicit localization expressions. From the Library of Wow! eBook ptg 1442 CHAPTER 30 Localizing Applications for Multiple Languages LISTING 30.13 LocalizablePageImplicit.aspx <%@ Page Language=”C#” UICulture=”auto” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> protected void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Visible = true; } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Localizable Page Implicit</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:Button id=”btnSubmit” meta:resourceKey=”btnSubmit” Text=”Click Me!” ToolTip=”Click to show message” OnClick=”btnSubmit_Click” Runat=”server” /> <br /><br /> <asp:Label id=”lblMessage” meta:resourceKey=”lblMessage” Text=”Thank You!” Visible=”false” Runat=”server” /> </div> </form> </body> </html> Both the Button and Label control include a meta:resourceKey property. The value of this property represents a resource key in a local resource file. For example, the resource file in Listing 30.14 contains three entries. From the Library of Wow! eBook ptg 1443 Creating Local Resources 30 LISTING 30.14 App_LocalResources\LocalizablePageImplicit.aspx.es.resx Name Value btnSubmit.Text chasque aquí btnSubmit.ToolTip Chasque aquí para demostrar el mensaje lblMessage.Text ¡Gracias! The first two entries set the Text and ToolTip properties of the btnSubmit control. The third entry sets the value of the Text property of the lblMessage property. WARNING When you are ready to start localizing a page, always create a default localization file (for example, LocalizablePageImplicit.aspx.resx). If you don’t create a default localization file, other culture-specific localization files are ignored. There are two advantages to using implicit localization expressions instead of using explicit localization expressions. First, implicit expressions enable you to override multiple control properties by associating a single resource key with the control. Second, by taking advantage of implicit localization expressions, you can easily localize an existing website. You simply need to add the meta:resourceKey attribute to any control that you need to localize. Using Local Resources with Page Properties You can use resource expressions when setting page properties such as the page title. For example, the page in Listing 30.15 uses an explicit resource expression to set the page title. LISTING 30.15 PageExplicit.aspx <%@ Page Language=”C#” UICulture=”auto” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title><asp:Literal Text=”<%$ Resources:Title %>” runat=”Server” /></title> </head> <body> <form id=”form1” runat=”server”> <div> <h1>Page Explicit Localization</h1> From the Library of Wow! eBook . resource files are associated with the LocalizablePage.aspx page: LocalizablePage.aspx.resx LocalizablePage.aspx.es-PR.resx LocalizablePage.aspx.es.resx The first resource file is the default. uses implicit localization expressions. From the Library of Wow! eBook ptg 144 2 CHAPTER 30 Localizing Applications for Multiple Languages LISTING 30.13 LocalizablePageImplicit.aspx <%@ Page. Page Properties You can use resource expressions when setting page properties such as the page title. For example, the page in Listing 30.15 uses an explicit resource expression to set the page