Tuesday, August 9, 2011

Đa ngôn ngữ (giao diện) với asp.net - MultiLanguagePage

1. Trong bài này tôi sẽ demo tinh năng Globalization, Localization
và UserControl trong Asp.Net
2. Kết hợp với profile để mô ta Personalization webapplication

Chúng ta nhìn thấy rõ ràng nếu như viết trực tiếp chuỗi trên trang sẽ
không thể thể sử dụng tính năng cục bộ hóa.

1. Create login.aspx page: Drag Login control to page as


Change connection string session in web.config file




2. Create default.aspx page

- Drag RadioButtonList to page: create to Item



Viet Nam – vi-VN
English – en-US
- Drag in to page a DropDownList and create two Item


Blue sky theme with value Blue
Red theme with value Red


- Add two theme

Right click Blue -> Add New Item and choose Skin file, modified as below picture


Repeat this step for Red theme








Open Default.aspx page choose as figure



Modified as


Right click on Default.aspx.resx page -> Copy. Right click on App_LocalResources -> Paste, rename as Default.aspx.vi-VN.resx after that change it as


Right click on Default.aspx choose view code, add code as
string cul;
string theme;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (theme != null)
DropDownList1.SelectedValue = Profile.Theme;
if (cul != null)
RadioButtonList1.SelectedValue = Profile.culure;
}
}

protected void Page_PreInit()
{
theme = Request.Form.Get("DropDownList1");
if (theme == null)
{
Page.Theme = Profile.Theme;
}
else
{
Profile.Theme = theme;
Page.Theme = theme;
}
}
protected override void InitializeCulture()
{
cul = Request.Form.Get("RadioButtonList1");

if (cul == null)
{
cul = Profile.culure;
}
else
{
Profile.culure = cul;
}

UICulture = cul;
Culture = cul;

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cul);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cul);

base.InitializeCulture();
}
3. Demo multi language page
Run default.aspx page


Chúc bạn thành công

1 comment:

Translate