Internationalizing Your Application in ASP.Net 2.0 using VB.NET

The Web has had worldwide reach since its inception. Users have different cultural expectations and speak different languages. In this Quickstart you will learn how powerful new ASP.NET v2.0 features make it easier to adapt your Web application to different countries, regions, and markets.

In this article, we will discuss the internationalization of web application using ASP.NET 2.0

Culture-sensitive Formatting

Setting a Preferred Language in Internet Explorer

HTTP allows browsers to send a list of preferred languages to the Web server using the Accept-Language HTTP request header field. In Internet Explorer the list can be configured like this:

  • Select the "Tools | Internet Options" command.
  • Select the "Languages" button.
  • In the dialog that is shown, click on the "Add" button to add a new language and select a new culture. Click "OK".
  • Ensure your preferred language is at the top of the list. Select that language and click "Move Up". Click "OK" to exit the dialog.
  • Refresh the page using F5.

Initially the list is preconfigured to the language of the Internet Explorer user interface, which makes sense as typically users want to see Web content in the language of the computer they are working on. Other browsers have similar features to configure this header field.

Auto-detecting the browser language for culture-sensitive formatting:

The language list can be used to determine which culture the user most likely prefers for formatting of numbers, dates and times. ASP.NET v2.0 now provides functionality to easily detect the first language in the list and use this for formatting:

<%@ Page Culture = "auto:en-US" %>

If the first language in the list matches the name of a specific culture supported by the .NET Framework, ASP.NET will set the CurrentCulture of the running thread to this culture. This happens early in the page life cycle so that all subsequent culture-sensitive formatting will be done with this culture.

If the first language in the list matches the name of a neutral culture supported by the .NET Framework, ASP.NET will try to use the function CultureInfo.CreateSpecificCulture() to create a specific culture for the purpose of formatting. For example, this would map fr into the culture for French (France). However, this determination could be inappropriate for somebody in the French-speaking part of Canada, which is why an application should provide the means to configure both culture and language

If the first language in the list isn't a culture supported by the .NET Framework, ASP.NET will fall back to the culture specified after the colon in the Culture declaration. This would be en-US - English (United States) in the example above.

This auto-detection functionality can be used in all places where Culture can be specified declaratively, such as in the globalization section of web.config. The auto-detection works the same for UICulture, which is used for localization and is explained later.

The following sample demonstrates a Currency Exchange Calculator that reacts to different language settings in the browser (see above for information how to change this). It will display the date in the format appropriate for the first language in the list and also uses the correct decimal separator for the exchange values. Furthermore, this decimal separator can be used for the entry of the value to be converted.

CODE:

XML:

<?xml version="1.0" encoding="UTF-8"?>

<ExchangeRates>

<Currency Region="US" Rate="1"/>

<Currency Region="AU" Rate="1.32766861391397"/>

<Currency Region="BR" Rate="2.78"/>

<Currency Region="CA" Rate="1.2253"/>

<Currency Region="CN" Rate="8.2765"/>

<Currency Region="DK" Rate="5.5995"/>

<Currency Region="DE" Rate="0.753352418261263"/>

<Currency Region="HK" Rate="7.776"/>

<Currency Region="IN" Rate="44.12"/>

<Currency Region="JP" Rate="104.93"/>

<Currency Region="MY" Rate="3.8"/>

<Currency Region="MX" Rate="11.29"/>

<Currency Region="NZ" Rate="1.41242937853107"/>

<Currency Region="NO" Rate="6.2225"/>

<Currency Region="SG" Rate="1.6495"/>

<Currency Region="ZA" Rate="5.82"/>

<Currency Region="KR" Rate="1055"/>

<Currency Region="SE" Rate="6.7723"/>

<Currency Region="CH" Rate="1.1545"/>

<Currency Region="TW" Rate="32.16"/>

<Currency Region="TH" Rate="39.67"/>

<Currency Region="GB" Rate="0.521240552514986"/>

<Currency Region="VE" Rate="1915.2"/>

</ExchangeRates>

 

Global_Vb.aspx:

<%@ Page Language="VB" AutoEventWireup="true" Culture="auto:en-US" %>

<%@ Import Namespace="System.Globalization" %>

<%@ Import Namespace="System.Xml" %>

<%@ Import Namespace="System.Data" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

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

<head runat="server">

<title>Currency Exchange Calculator</title>

</head>

<script runat="server">

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

If (Not IsPostBack Or CStr(Session("CultureName")) <> CultureInfo.CurrentCulture.Name) Then

DateTimePlaceholder.Text = DateTime.Now.ToLongDateString()

 

' Populate the drop-down with currency names

' If possible set the default selected currency to the currency of the CurrentCulture

Dim UserRegion As New RegionInfo(CultureInfo.CurrentCulture.Name)

Dim TargetRegion As RegionInfo

Dim dir As String = Server.MapPath("")

 

Dim ExchangeRateTextReader As New XmlTextReader(Server.MapPath("~/ExchangeRates.xml"))

Dim ExchangeRateDataSet As New DataSet("ds")

ExchangeRateDataSet.ReadXml(ExchangeRateTextReader)

Dim CurrencyTable As DataTable = ExchangeRateDataSet.Tables("Currency")

Dim CurrencyTableReader As DataTableReader = CurrencyTable.CreateDataReader()

CurrencyDropDown.Items.Clear() ' Make sure list is empty on culture change in current session

While CurrencyTableReader.Read()

TargetRegion = New RegionInfo(CurrencyTableReader.GetString(0))

Dim Currency As New ListItem(TargetRegion.CurrencyEnglishName, CurrencyTableReader.GetString(1))

If TargetRegion.ISOCurrencySymbol = UserRegion.ISOCurrencySymbol Then

Currency.Selected = True

End If

CurrencyDropDown.Items.Add(Currency)

End While

Session("CultureName") = CultureInfo.CurrentCulture.Name

End If

 

End Sub 'Page_Load

 

Private Function CalculateExchangeValue(ByVal Rate As String) As String

Dim Value As Double

Try

Value = Double.Parse(ConvertValue.Text)

Catch

Value = 0

End Try

Dim ExchangeValue As Double = Double.Parse(Rate, CultureInfo.InvariantCulture) / Double.Parse(CurrencyDropDown.SelectedItem.Value, CultureInfo.InvariantCulture) * Value

 

Return ExchangeValue.ToString("F")

 

End Function 'CalculateExchangeValue

 

Private Function CurrencyName(ByVal RegionName As String) As String

Dim result As String

Try

Dim ri As New RegionInfo(RegionName)

result = ri.CurrencyEnglishName

Catch

result = "Unknown"

End Try

Return result

 

End Function 'CurrencyName

 

Private Sub ConvertButton_Click(ByVal sender As Object, ByVal e As EventArgs)

ExchangeGrid.DataBind()

 

End Sub 'ConvertButton_Click

</script>

<body bgcolor="moccasin">

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

<div>

<h2>

Currency Exchange Calculator</h2>

<asp:Label ID="DateTimePlaceholder" runat="server" Text="DateTimePlaceholder"></asp:Label><br /><br />

Enter an amount, choose a currency and click the Convert button to calculate the

exchange values.<br />

<br />

&nbsp;<asp:TextBox ID="ConvertValue" runat="server">100</asp:TextBox>&nbsp;&nbsp;&nbsp;

<asp:DropDownList ID="CurrencyDropDown" runat="server">

</asp:DropDownList>

<asp:Button ID="ConvertButton" runat="server" Text="Convert" OnClick="ConvertButton_Click" />

<asp:XmlDataSource DataFile="ExchangeRates.xml" ID="XmlExchangeRates"

runat="server"></asp:XmlDataSource>

&nbsp;&nbsp;

<br />

<asp:GridView AutoGenerateColumns="False" DataSourceID="XmlExchangeRates" ID="ExchangeGrid"

runat="server" >

<Columns>

<asp:TemplateField HeaderText="Name">

<ItemTemplate>

<asp:Label ID="ColumnCurrency" runat="server" Text='<%# CurrencyName(CStr(Eval("Region"))) %>'></asp:Label>

</ItemTemplate>

<ItemStyle HorizontalAlign="Left" />

<HeaderStyle HorizontalAlign="Left" />

</asp:TemplateField>

<asp:TemplateField HeaderText="Exchange Value">

<ItemTemplate>

<asp:Label ID="ColumnExchangeValue" runat="server" Text='<%# CalculateExchangeValue(CStr(Eval("Rate"))) %>'></asp:Label>

</ItemTemplate>

<ItemStyle HorizontalAlign="Right" />

<HeaderStyle HorizontalAlign="Right" />

</asp:TemplateField>

</Columns>

</asp:GridView>

<br />
 

Disclaimer:
The rate calculation above is based on exchange rates published on Dec 9th, 2004
by the United States Federal Reserve Bank of New York. For up-to-date exchange rates please go to the website of this
organization. This sample application
is not intended to provide accurate or up-to-date currency conversions.</div>
</form>
</
body>
</
html>

Internationaliziing1-in-vb.net.gif

In order to view text in some East Asian languages and some languages with complex scripts, you might need to install the appropriate font for Internet Explorer. Refer to Internet Explorer Help Topics for more information (see in the index under "foreign languages used in Web pages"). Alternatively you can install additional fonts from the Regional and Language Options of the Control Panel.
 v

Up Next
    Ebook Download
    View all
    Learn
    View all