Creating a website with multi languages very common development to all web developers. Somehow, I hope this could be helpful and it is just about sharing knowledge.
What is Global Resources :
- One Language One Resource file ( share within pages in the entire web site. )
- Explicit localization
- Create resource file manually.
What is Local Resources :
- For each page One Resource file ( cannot share with other page )
- Implicit localization
- By web Tools. (Finish the page first and switch to Design View. Tools > Generate Local Resource)
<asp:Label ID="Label1" runat="server"
Text="<%$ Resources:LocalString, Welcome %>"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server"
Text="<%$ Resources:LocalString, Construction %>"></asp:Label>
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="en-US" Value="en-US"></asp:ListItem>
<asp:ListItem Text="en-AU" Value="en-AU"></asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default2.aspx">HyperLink</asp:HyperLink>
4.b. Default.aspx.cs
using System.Threading;
using System.Globalization;
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["CUL"] != null)
{
DropDownList1.SelectedIndex=DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(Session["CUL"].ToString()));
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["CUL"] = DropDownList1.SelectedItem.Value;
Response.Redirect(Request.Url.AbsoluteUri);
}
}
5. Default2.aspx
<asp:Label ID="Label2" runat="server"
Text="<%$ Resources:LocalString, Construction %>"></asp:Label>
That is ALL. Enjoy programming
Download Sample
Useful links :
- What is BasePage Class
- Microsoft MSDN
http://msdn.microsoft.com/en-us/library/ms247245.aspx
http://msdn.microsoft.com/en-us/library/ms227427.aspx
http://msdn.microsoft.com/en-us/library/fw69ke6f(v=vs.90).aspx
What is Global Resources :
- One Language One Resource file ( share within pages in the entire web site. )
- Explicit localization
- Create resource file manually.
What is Local Resources :
- For each page One Resource file ( cannot share with other page )
- Implicit localization
- By web Tools. (Finish the page first and switch to Design View. Tools > Generate Local Resource)
My example is Explicit localization
1. Create BasePage Class in App_Code folder
public class BasePage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
if (Session["CUL"] != null)
{
String selectedLanguage = Session["CUL"].ToString();
UICulture = selectedLanguage;
Culture = selectedLanguage;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(selectedLanguage);
}
base.InitializeCulture();
}
}
{
protected override void InitializeCulture()
{
if (Session["CUL"] != null)
{
String selectedLanguage = Session["CUL"].ToString();
UICulture = selectedLanguage;
Culture = selectedLanguage;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(selectedLanguage);
}
base.InitializeCulture();
}
}
2. All the Web Forms need to change inheritance class BagePage class instead of default inherit System.Web.UI.Page.
3. Add Resource file (http://www.youtube.com/watch?v=-BXpZyoxHzY)
4.a. Default.aspx
Text="<%$ Resources:LocalString, Welcome %>"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server"
Text="<%$ Resources:LocalString, Construction %>"></asp:Label>
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="en-US" Value="en-US"></asp:ListItem>
<asp:ListItem Text="en-AU" Value="en-AU"></asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default2.aspx">HyperLink</asp:HyperLink>
4.b. Default.aspx.cs
using System.Threading;
using System.Globalization;
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["CUL"] != null)
{
DropDownList1.SelectedIndex=DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(Session["CUL"].ToString()));
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["CUL"] = DropDownList1.SelectedItem.Value;
Response.Redirect(Request.Url.AbsoluteUri);
}
}
5. Default2.aspx
<asp:Label ID="Label2" runat="server"
Text="<%$ Resources:LocalString, Construction %>"></asp:Label>
That is ALL. Enjoy programming
Download Sample
Useful links :
- What is BasePage Class
- Microsoft MSDN
http://msdn.microsoft.com/en-us/library/ms247245.aspx
http://msdn.microsoft.com/en-us/library/ms227427.aspx
http://msdn.microsoft.com/en-us/library/fw69ke6f(v=vs.90).aspx


No comments:
Post a Comment