Screenshots
Introduction
In an ASP.NET Web page, you can set to two culture values, the Culture and UICulture properties. The Culture
value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on. The UICulture
value determines which resources are loaded for the page.
Background
This is a very simple article for those who know object oriented programming.
Using the Code
The code is arranged in:
App_Code mybasePage
class
App_LocalResources
- aspx pages
App_Code mybasePage Class
Class mybasepage
is derived from System.Web.UI.Page
. Public
property CultureName
sets cultureName
. Set the CurrentUICulture and CurrentCulture properties of the current thread to the UI culture and culture, respectively. The CurrentUICulture
property takes a language and culture information string
. To set the CurrentCulture
property, you create an instance of the CultureInfo
class and call its CreateSpecificCulture method.
public class mybasePage : System.Web.UI.Page
{
public mybasePage()
{
}
static string cultureName;
public static string CultureName
{
get { return cultureName; }
set { cultureName = value; }
}
protected override void InitializeCulture()
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(cultureName);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(cultureName);
base.InitializeCulture();
}
}
<table>
<tr>
<td >
<asp:Label ID="Label1" runat="server" Text="Select Language"></asp:Label>
</td>
<td>:</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="English" Value="en-US"></asp:ListItem>
<asp:ListItem Text="Marathi" Value="mr-IN"></asp:ListItem>
<asp:ListItem Text="Gujarathi" Value="gu-IN"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="3">
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Button ID="Button1" runat="server"
Text="set Language" onclick="Button1_Click"
style="height: 26px" />
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server"
PostBackUrl="~/Sample.aspx">Go to Sample Page</asp:LinkButton>
</td>
</tr>
</table>
protected void Button1_Click(object sender, EventArgs e)
{
mybasePage.CultureName = DropDownList1.SelectedItem.Value.ToString();
}
Go to the sample page. There is a link button to navigate to another page.
The sample page shows Language setting words.
App_LocalResources
Creating Resources from a Web Page
A resource file is an XML file that can contain string
s and other resources, such as image file paths. Resource files are typically used to store user interface string
s that must be translated into other languages. This is because you can create a separate resource file for each language into which you want to translate a Web page.
Global resource files are available to any page or component in your Web site. Local resource files are associated with a single Web page, user control, or master page, and contain the resources for only that page.
To generate a local resource file from an ASP.NET Web page:
- Open the page for which you want to create a resource file.
- Switch to Design View.
- In the Tools menu, click Generate Local Resource. Visual Web Developer creates the App_LocalResources folder if it does not already exist. Visual Web Developer then creates the culturally neutral base resource file for the current page, which includes a key/name pair for each control property or page property that requires localization. Finally, Visual Web Developer adds a meta attribute to each ASP.NET Web server control to configure the control to use implicit localization for more information about implicit and explicit localization.
- Type values for each resource that you need in your application, and then save the file.
- If the latest resource changes are not displayed, refresh Design view by switching to Source view and then switching back to Design view.
- To create resource files for additional languages, copy the file in Solution Explorer or in Windows Explorer, and then rename like:
- Sample.aspx.gu-IN.resx
- Sample.aspx.mr-IN.resx
Sample.aspx page contains a label. This label text changes if you make a change in the default.aspx page.
Points of Interest
- To design the same web site for national/international language.
History
- 12th May, 2009: Initial post