1. Trang chủ
  2. » Công Nghệ Thông Tin

ASP.NET 4 Unleased - p 148 docx

10 205 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 759,92 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

CHAPTER 30 Localizing Applications for Multiple Languages In Listing 30.15, the page title is created with a Literal control, which contains an explicit resource expression for the v

Trang 1

CHAPTER 30 Localizing Applications for Multiple Languages

</div>

</form>

</body>

</html>

In Listing 30.15, the page title is created with a Literal control, which contains an

explicit resource expression for the value of its Text property You also can use implicit

resource expressions when setting the page title This approach is illustrated by the page in

Listing 30.16

LISTING 30.16 PageImplicit.aspx

<%@ Page Language=”C#” UICulture=”auto” meta:resourceKey=”page” %>

<!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>Page Title</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<h1>Page Implicit Localization</h1>

</div>

</form>

</body>

</html>

The <%@ Page %> directive includes a meta:resourceKey attribute If a local resource includes

a page.Title entry, the value of this entry is used for the title displayed by the page

Retrieving Local Resources Programmatically

If you need to retrieve a local resource in your page code, you can use the

GetLocalResourceObject() method For example, the page in Listing 30.17 grabs a

welcome message from a resource file The welcome message is used to format some text,

and then the formatted text displays in a Label control

LISTING 30.17 ProgramLocal.aspx

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

Trang 2

1445 Creating Local Resources

LISTING 30.18 App_LocalResources\ProgramLocal.aspx.es.resx

welcomeMessage Welcome {0} to our website!

<script runat=”server”>

void Page_Load()

{

string welcomeMessage = (string)GetLocalResourceObject(“welcomeMessage”);

lblMessage.Text = String.Format(welcomeMessage, “Steve”);

}

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Program Local Resource</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:Label

id=”lblMessage”

Runat=”server” />

</div>

</form>

</body>

</html>

The result returned from GetLocalResourceObject() must be cast to a string value As the

method name implies, the method returns an object and not a string value The resource

file associated with the page in Listing 30.17, named ProgramLocal.aspx.es.resx, is

contained in Listing 30.18

If someone’s browser is set to Spanish as the preferred language, and the user requests the

page, the welcome message is retrieved from this resource file, the name Steve is added to

the string, and the result displays in the browser (see Figure 30.8)

You also can retrieve local resources in a component Within a component, use the shared

HttpContext.GetLocalResourceObject() method For example, the component in Listing

30.19 grabs the entry named ClickHere from the local resource file that corresponds to

the page named LocalizablePage.aspx

Trang 3

ptg CHAPTER 30 Localizing Applications for Multiple Languages

LISTING 30.19 LocalComponent.cs

using System;

using System.Web;

public class LocalComponent

{

public static string getResource()

{

return (string)HttpContext.GetLocalResourceObject(“~/LocalizablePage.aspx”,

➥“ClickHere”);

}

}

Creating Global Resources

A local resource is scoped to a particular page A global resource, on the other hand, can

be used by any page in an application Any localized content that you need to share

among multiple pages in your website should be added to a global resource file

FIGURE 30.8 Retrieving a local resource programmatically

Trang 4

1447 Creating Global Resources

LISTING 30.20 App_GlobalResources\Site.resx

Copyright Copyright &copy; 2006 by the Company

You create global resource files by adding the files to a special folder named

App_GlobalResources This folder must be located in the root of your application

For example, the file in Listing 30.20 is a global resource file

The page in Listing 30.21 uses the entries from the global resource file (see Figure 30.9)

LISTING 30.21 ShowGlobalPage.aspx

<%@ Page Language=”C#” %>

<!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

id=”ltlTitle”

Text=”<%$ Resources:Site,Title %>”

Runat=”Server” />

</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<br />Page Content

<br />Page Content

<br />Page Content

<br />Page Content

<hr />

<asp:Literal

id=”ltlCopyright”

Text=”<%$ Resources:Site,Copyright %>”

Runat=”Server” />

</div>

</form>

</body>

</html>

Trang 5

ptg CHAPTER 30 Localizing Applications for Multiple Languages

Just as you can with a local resource file, you can localize a global resource file by

adding culture names to the file name For example, the page in Listing 30.22 is

local-ized to Spanish

LISTING 30.22 App_GlobalResources\Site.es.resx

Title Mi Website

Copyright Copyright &copy; 2006 de la compañía

FIGURE 30.9 Displaying global resource entries

If you modify the UICulture attribute contained in the <%@ Page %> directive in Listing

30.21 to the value es, the resource file in Listing 30.22 will be used with the page

Alternatively, you can set UICulture to the value auto and change your browser’s

language settings

Retrieving Global Resources Programmatically

You can retrieve a global resource entry programmatically from any page by using the

GetGlobalResourceObject() method For example, the page in Listing 30.23 grabs the

Title entry from the Site resource file and displays the value of the entry in a Label control

Trang 6

1449 Creating Global Resources

LISTING 30.23 ProgramGlobal.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”>

void Page_Load()

{

lblMessage.Text = (string)GetGlobalResourceObject(“Site”, “Title”);

}

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Program Global</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:Label

id=”lblMessage”

Runat=”server” />

</div>

</form>

</body>

</html>

The GetGlobalResourceObject() method requires two parameters: the name of the

resource class and the name of an entry The resource class corresponds to the global

resource filename

Using Strongly Typed Localization Expressions

The ASP.NET Framework automatically converts global resources into compiled classes

behind the scenes This enables you to use strongly typed expressions when working with

global resources in your code When you create a resource, a new class is added

automati-cally to the Resources namespace The class exposes all the entries of the resource file as

properties

For example, the page in Listing 30.24 retrieves the Title entry from the Site global

resource file (Site.resx and its culture-specific variations)

Trang 7

CHAPTER 30 Localizing Applications for Multiple Languages

LISTING 30.24 ProgramGlobalTyped.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”>

void Page_Load()

{

lblMessage.Text = Resources.Site.Title;

}

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Program Global Typed</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:Label

id=”lblMessage”

Runat=”server” />

</div>

</form>

</body>

</html>

You can use the following expression magically to refer to the Title entry in the Site

resource file:

lblMessage.Text = Resources.Site.Title

Using the Localize Control

The ASP.NET Framework includes a control named the Localize control, which is

included in Framework to make it easier to localize big chunks of text in a page

For example, the page in Listing 30.25 uses the Localize control in the body of the page

Trang 8

1451 Using the Localize Control

LISTING 30.25 ShowLocalizeControl.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>Show Localize Control</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:Localize

ID=”locBodyText”

meta:resourceKey=”locBodyText”

Runat=”server”>

Here is the page body text

</asp:Localize>

<br /><br />

<asp:Literal

ID=”ltlBodyText”

runat=”server”>

Here is some literal text

</asp:Literal>

</div>

</form>

</body>

</html>

The Localize control is similar to the Literal control (it derives from the Literal

control) In Source View, there is nothing that distinguishes the two controls The

differ-ence between the Localize control and Literal control is apparent only in Design View

Unlike the Literal control, the contents of the Localize control can be edited directly on

the Designer surface in Design View (see Figure 30.10)

Trang 9

CHAPTER 30 Localizing Applications for Multiple Languages

FIGURE 30.10 Using the Localize control in Design View

Summary

In this chapter, you learned how to localize websites for different languages and culture

In the first section, you learned how to use the Culture and UICulture properties to set

the current culture for the page You also learned how to set these properties automatically

by detecting a browser’s preferred language settings

Next, you learned how to create local resource files that you can apply to particular pages

(and other files) You learned how to use both explicit and implicit localization expressions

You also saw how you can programmatically retrieve local resource entries in your code

You then studied the topic of global resource files, which contain entries that can be used

within any page in a website You learned to use explicit resource expressions with global

resources and how to retrieve global resource entries programmatically

Finally, you had a brief look at the ASP.NET Localize control You learned how to use this

control to localize big chunks of text in a page

Trang 10

CHAPTER 31

Working with the HTTP Runtime

IN THIS CHAPTER

Creating a Custom BuildProvider Creating a Custom ExpressionBuilder Creating HTTP Handlers Working with HTTP Applications and HTTP Modules

Summary

This chapter tackles a number of advanced topics by

digging deeper into the mechanics of how an ASP.NET page

is processed In this first section, you learn how to create a

custom BuildProvider, which is a NET class that generates

source code from a file automatically You learn how to

create a custom BuildProvider that builds custom data

access components automatically

Next, you learn how to create a custom ExpressionBuilder,

which is responsible for parsing an expression into code

For example, when you use the <%$ ConnectionStrings:

MyDatabase %> syntax to refer to a connection string, you

use the ConnectionStringExpressionBuilder in the

back-ground In this chapter, you learn how to build a custom

ExpressionBuilder that looks up values from an XML file

You also learn how to work with HTTP Handlers An HTTP

Handler is a NET class that executes whenever a request is

made for a file at a certain path For example, you can use a

custom HTTP Handler to retrieve an image from a database

table whenever someone requests a file with the extension

.gif or jpeg

Finally, you will see how to create custom HTTP Modules

An HTTP Module is a NET class that executes with each

and every request For example, you can implement a

custom authentication system by creating a custom HTTP

Module You also can use a custom HTTP Module to create

a custom logging module

Ngày đăng: 06/07/2014, 18:20

TỪ KHÓA LIÊN QUAN