Copy and Translate
The easiest way to localize a Web page is usually
to create a copy and translate it to the target language.
This works well for static content that does not require
a lot of maintenance. To support this model for ASP.NET pages, you can set
the Culture attribute using the
Page directive. All locale-dependent methods pick
up the value of the Culture attribute.
The following sample shows how to do this for three independent,
localized versions of a page.
The Culture property is set on each page to determine
the format of the date:
<%@Page Culture="de-DE" Language="VB" %>
...
<%=DateTime.Now.ToString("f", Nothing)%>
VB
Localization and Controls
An improvement over the simple copy-and-translate approach is to
use controls to pick up the culture of the main page. In the following sample,
the image of the flag and the search bar are controls. Depending
on the culture of the hosting page, they render different content.
To support this, the UICulture attribute is also added to each page:
<%@Page Culture="de-DE" UICulture="de-DE" Language="VB" %>
VB
The flag control (Flag.ascx), for example, just uses the culture name to build the
Src attribute of an <img> tag:
<%@Import Namespace="System.Globalization"%>
<script runat="Server" Language="VB">
Overrides Protected Sub Render(writer As HtmlTextWriter)
FlagImage.Src = "../../flags/" & CultureInfo.CurrentCulture.Name & ".jpg"
FlagImage.Alt = CultureInfo.CurrentCulture.NativeName
MyBase.Render(writer)
End Sub
</script>
<img runat="server" id="FlagImage" />
VB
The search control (Search.ascx) uses a switch statement to initialize the
values of a label and a text box, but the culture name could also be the
parameter for a database query:
Sub LocalizeSearchText()
Select Case String.Intern(CultureInfo.CurrentUICulture.Name))
Case "en-US"
SearchText.Text = "Clinton"
SearchButton.Text = "Search"
Case "de-DE"
...
Case "ja-JP"
...
Case Else
SearchButton.Text = "Search"
End Select
End Sub
VB