Quantcast việt hóa asp.net website sử dụng visual studio

việt hóa asp.net website sử dụng visual studio

by Neon Quach 12. June 2010 15:48

Xây dựng ứng dụng web đa ngôn ngữ hiện nay là 1 trong những công việc bình thường và phổ biến nhất hiện nay, trong bài blog này mình sẻ hướng dẫn các cách có thể việt hóa 1 cách để dàng 1 asp.net website sử dụng Resource được build in sẵn trong visual studio.

Trong visual studio có 2 dạng resource: App_GlobalResources và App_LocalResources, tên gọi khác nhau nên cách sử dụng cũng khác nhau

App_GlobalResources: resource file được dùng cho toàn bộ website.
App_LocalResources: resource file cục bộ, phạm vi sử dụng trong 1 trang hoặc 1 user control.

Bắt đầu bằng việc tạo mới 1 website (blank website - ở đây mình sử dụng visual studio 2010)

Add new Item: default.aspx Page, folder App_GlobalResources, sau đó add 1 file resource với tên code2code.resx



Add 1 text resource mới cho code2code.resource name là hello và value là Hello world



Từ trang default.aspx add 1 label mới

<asp:Label ID="lblHello" runat="server" Text="<%$ Resources:code2code, hello%>"></asp:Label>

code behine:


lblHello.Text = (string)GetGlobalResourceObject("code2code", "hello");

Để có thể thấy được sự thay đổi chúng ta modify trong web.config như sau:

<globalization culture="vi-vn" uiCulture="vi-vn"/>


Chúng ta cũng có thể thiết lập bằng cách lập trình.

Đối với file việc việt hóa bằng App_LocalResources, chúng tiến hành thêm mới 1 trong login.aspx chẳng hạn, thêm 2 lable username, password, và 2 textboxes như hình sau:



Sau đó tiến hành generate ra file resource bằng cách chọn Tools → Generate Local Resource



Sau đó visual studio sẻ tự thêm 1 folder App_LocalResources và bên trong chứa file login.aspx.resx, mục đích cho việc việt hóa dành cho trang login.aspx, và visual studio sẻ tự động thêm code binding resource cho các text:

    <div>

        <p>

            <asp:Label ID="lblUsername" runat="server" Text="Username:" meta:resourcekey="lblUsernameResource1"></asp:Label>

            <asp:TextBox ID="txtUsername" runat="server" meta:resourcekey="txtUsernameResource1"></asp:TextBox></p>

        <p>

            <asp:Label ID="lblPassword" runat="server" Text="Password:" meta:resourcekey="lblPasswordResource1"></asp:Label>

            <asp:TextBox ID="txtPassword" runat="server" meta:resourcekey="txtPasswordResource1"></asp:TextBox></p>

    </div>

 

Mở file login.aspx.resx ta thấy chúng chứa tất cả những text cần thiết cho việc localize, tương tự như App_GlobalResources chúng ta cũng sẻ phải thêm mới 1 file resource cho ngôn ngữ tiếng Việt, bằng cách thêm 1 file resource với tên là: login.aspx.vi-vn.resx, sau đó thay đổi nội dung bằng tiếng Việt.

Và đây code behind giải quyết cho việc binding local resource cho trang login.aspx

lblUsername.Text = (string)GetLocalResourceObject("lblUsernameResource1.Text");


Tóm lại: làm bằng cách nào đi chăng nữa thì localize bằng các file resource built-in trong visual studio cũng là cách đơn giản nhất, nhanh nhất và hiệu quả nhất.

Hope this help!

Localization.rar (12.04 kb)

Tags: , , ,


Categories: asp.net | net framework | visual studio 2010 | c#

blog comments powered by Disqus

About me

I'm  currently employed as Software developer at devinition.com and also a Microsoft Certified Technology Specialist (MCTS), Microsoft Certified Professional Developer (MCPD) in Net Framework 2.0 and 3.5: Web Applications and MCTS .NET Framework 3.5, ADO.NET Applications

Powered by BlogEngine.NET 2.5.0.5 - Eco Theme by n3o Web Designers