Set Up Encoding

Crypto Ultimatum

How to make Money by Investing in Cryptocurrency

Get Instant Access

You can use encoding to create a site that supports multiple languages. Encoding refers to the way that the data within your file is stored. Certain languages, such as Japanese, have large character sets and therefore require an encoding type that would support all of the characters in the language.

Encoding is important because it determines how the data from your Web server is sent to and from the Web browser, along with how the files are stored on your Web server. Response encoding refers to the way the responses are sent to the Web browser, whereas request encoding refers to the way the Web server handles sent requests. File encoding pertains to how files are stored on the Web server.

UCS Transformation Format (UTF-8) is an encoding format that supports 8-bit form. This encoding supports all Unicode character values, which allow for support of most modern character sets.

Rather than specifying encoding for each page in your application, you can specify this in your web.config file for the entire application.

You can specify the response encoding type on a page to be UTF-8 using the @Page directive. You can then add multiple languages to the page to accommodate users that speak different languages.

SET UP ENCODING

SET UP ENCODING

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

0 Add the @Page directive to the page and set the ResponseEncoding attribute to UTF-8, as well as the Language attribute to C#.

□ Add a Spanish heading for the page.

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

0 Add the @Page directive to the page and set the ResponseEncoding attribute to UTF-8, as well as the Language attribute to C#.

Note: You will need to install support for languages not currently installed on your computer.

Add an English heading for the page.

□ Add a Spanish heading for the page.

LOCALIZATION AND ASP.NET

You can set up encoding for the entire application in the web.config file with the

<globalization> tag.

The options for the attributes for encoding are as follows:

requestEncoding Specifies the assumed encoding of each incoming request. The default is us-ascii. responseEncoding Sets the content encoding of responses. The default is iso-8859-1. fileEncoding Designates the default encoding for .aspx, .asmx, and .asax files.

Example:

<configuration>

<system.web>

<globalization " requestEncoding="utf-8

1 responseEncoding="utf-8

1 fileEncoding="utf-8

" />

</system.web>

</configuration>

Add a Japanese heading for the page.

Q Save the file and request it ■ The messages in the from the Web server. various languages appear.

Add a Japanese heading for the page.

Q Save the file and request it ■ The messages in the from the Web server. various languages appear.

USING CULTUREINFO

You can use the Cultureinfo class to display localized settings. For example, you can display the date in multiple formats based on the user's preferences. Begin by ensuring that the response's encoding type is appropriate. You can either do this at the Page level or the Application level. See page 262 for more information on setting up encoding. You need to have some way to determine which culture to use. One way to do this is to simply have the user select the culture. Another way is to read this information from the user's Web browser. After you determine the culture, you can set the current thread's culture and then access the appropriate property.

You can also use the Cultureinfo class to display the calendar preferences and the native name of the culture.

You can create a drop-down list of cultures so the user can specify a culture. After the user selects a culture, you can use Cultureinfo and a label control to display the localized time.

^USINGCULTUREINFO

£ Untitled Notepad

^USINGCULTUREINFO

£ Untitled Notepad

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

0 Create a drop-down list that has several languages from which to choose.

_0 Add a submit button control.

—Q Add a span control on the form to display messages.

_O Add the @Page directive to the page and set the ResponseEncoding attribute to UTF-8, as well as the Language attribute to C#.

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

Add a heading to the page.

Add a message to the user.

0 Create a drop-down list that has several languages from which to choose.

_0 Add a submit button control.

—Q Add a span control on the form to display messages.

_O Add the @Page directive to the page and set the ResponseEncoding attribute to UTF-8, as well as the Language attribute to C#.

• Add an alias to the System.Threading and System.Globalization

LOCALIZATION AND ASP.NET

RESULT:

The localized date is domingo, 05 de agosto de 2001.

The calendar to use for this culture is System.Globalization.GregorianCalendar. The native name for this culture is español (España.).

TYPE THIS:

<SCRIPT LANGUAGE="C#" RUNAT="Server">

void SubmitBtn_Click(object Source, EventArgs e) {

CultureInfo cultureinfoLanguage = new CultureInfo(dropdownlistCultures.SelectedItem.Value);

Thread.CurrentThread.CurrentCulture = cultureinfoLanguage; spanMessage.InnerHtml = "The localized date is " + DateTime.Now.ToString("D", CultureInfo.CurrentCulture) +

+ "<BR/> The calendar to use for this culture is 11 + CultureInfo.CurrentCulture.Calendar + "." + "<BR/> The native name for this culture is " + CultureInfo.CurrentCulture.NativeName + ".";

Add the

SubmitBtn_Click function.

Create a new Culturelnfo variable based on the item selected in the drop-down list box.

Set the current culture. u Save the file and request it Click the Submit button.

le Set the InnerHtml property of the span control to the date based on the current culture.

from the Web server.

le Click 3 to select a culture.

-■ The message appears according to the culture selected.

USING REGIONINFO

You can use the Regioninfo class to show regionalized settings. For example, you can display the local currency based on the user's preferences.

Working with the Regioninfo class is similar to working with the Cultureinfo class, you must ensure that the response's encoding type is set up appropriately. You can either do this at the Page level or the Application level. See page 262 for more information on setting up encoding. You need to determine which region to use for the user. You can simply have the user select the culture, or you can try to determine it from the information sent from the user's Web browser. After you determine the region, you can then access the property with which you want to work.

You can also use the Regioninfo class to determine whether the region uses the metric system and what the Windows Region Name is. See the Apply It! section for the code necessary to do this.

You can create a drop-down list of regions for the user to choose from on a Web page. After the user selects a region, you can use the Regioninfo class and a label control to display the regional currency for the user.

Bi Untitled - Notepad

¡1 Untitled - Notepad

File Edit Format Help

:%(5iPaqe Language-'C#" ResponseEncodinq-'utf-

%@import Namespace-'System.'Threading"0/«; %@lmport Namespace-'System.Globalization"%H <HTML> <HEAD>

<H3>Welcome to mylifetimegoals.com</H3> Select your region below:

<SPAN ID="spanMessaoe" RUNAT="Server" /> </FORM>

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

< Add a message to the user.

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

Add a heading to the page.

< Add a message to the user.

Create a drop-down list that has several languages from which to choose.

¡1 Untitled - Notepad

File Edit Format Help

:%(5iPaqe Language-'C#" ResponseEncodinq-'utf-

%@import Namespace-'System.'Threading"0/«; %@lmport Namespace-'System.Globalization"%H <HTML> <HEAD>

<H3>Welcome to mylifetimegoals.com</H3> Select your region below:

<ASP:DROPDOWNLIST ID-'dropdownlistRegions" RUNAT-'Server"»

•=ASP:LISTITEM VALUE="US">United States=/ASP.LISTITEM> ■eASP:LISTITEM VALUE="ES">Spain</ASP:LISTITEM=--=ASP:LISTITEM VALUE="JP">Japan-=/ASP:LISTn~EMs •=/ASP.DRQPDQWNLIST?_

:ASP:BLrTTQN TEXT="Submit" OnClick="Submi Btn Click" RUNAT="Seiver"/>

<SPAN ID="spanMessaoe" RUNAT="Server" /> </FORM>

-0 Add a submit button control.

-Q Add a span control on the form to display messages.

"O Add the @Page directive to the page and set the ResponseEncoding attribute to UTF-8, as well as the Language attribute to C#.

'—• Add an alias to the System.Threading and System.Globalization namespaces.

LOCALIZATION AND ASP.NET

In addition to determing the local date and time, you can also determine whether the region uses the metric system and what the three-letter Windows Region Name is.

TYPE THIS:

<SCRIPT LANGUAGE="C#" RUNAT="Server">

void SubmitBtn_Click(object Source, EventArgs e) {

RegionInfo regioninfoLanguage = new RegionInfo(dropdownlistRegions.SelectedItem.Value); String stringMetric = ""; if (regioninfoLanguage.IsMetric == true) stringMetric = "uses";

else stringMetric = "does not use"; spanMessage.InnerHtml = "The local currency for " + regioninfoLanguage.EnglishName + " is " + regioninfoLanguage.CurrencySymbol + "." + "<BR/> This region " + stringMetric + " the metric system." +

"<BR/> The Windows region name for this region is " +

regioninfoLanguage.ThreeLetterWindowsRegionName + "."; }

RESULT:

The local currency for United States is $. This region does not use the metric system. The Windows region name for this region is USA.

q Untitled - Notepad

File Edit Format Help

<%@Paqe Language-'C#" ResponseEncoding-'utf-8" %> <%@lmport Name5pace="Sy5tem.Threading"%s-<%@lmport Namespace-'System.Globallzatlon"%> <HTML> <HEAD=

(SCRIPT LANGUAGE-'C#M RUNAT="Server"=-void SubmitBtn Cllck(obiect Source, EventArgs e) {

Regionlnfo regloninfoLanguage - new Reqionlnfo[dropdownlistReqions.Selectedltem. Value)

spanMessage.InnerHtml = "The local currency for " + regloninfoLanguage EnglishName + " is " + regloninfoLanguage CurrencySymbol ^

<H3>Welcome to mylifetimegoals.com-e/HSs Select your region below

<FORM RUNAT-'Server">

CP/5

<ASP:DROPDOWNLIST ID-'dropdownlistRe <ASP:LISTITEM VALUE="US">Un

Add the SubmitBtn_Click function.

L— Create a new Regionlnfo variable based on the item selected in the drop-down list.

glons" RUNAT="Server"> ted States</ASP LISTITEM>

Set the InnerHtml property of the span control to the date based on the current culture.

E Save the file and request it from the Web server.

le Click q to select a culture.

£ Click the Submit button.

-■ The message appears according to the region selected.

Was this article helpful?

0 0

Post a comment