How to create a localize ASP.NET 2.0 web page in Visual Studio 2005 - II

在上一篇 How to create a localize ASP.NET 2.0 web page in Visual Studio 2005 中, 我們用 .resx 檔來儲存所有在 web page 上要 localize 的 data, 但要是我要 localize 從某個 event 中傳回的 data, 例如最簡單的 "Hello World", 這個字串不在頁面上, 縱然我們也可以把 "Hello World" 儲存在資源檔, 但要如何從事件中把這個字串中把這個字串取出來呢?

在以前 .net 1.1 時, 我們可以把字串儲在 xxx.resources 檔中, 然後用 ResourceManager 把字串取出來, 但在我上一篇的作法, 我利用 VS 2005 generate 一個 xxx.resx 檔, 這個檔案基本上是以 xml 格式來儲存 resources, 所以如果要用 ResourceManager, 我們必需用 Resgen.exe 這個工具將 xxx.resx 轉成 xxx.resources 我們才能利用 ResourceManager 來取出我們要的字串.

現在在 .net 2.0, 我們有一個新的 method 在 System.Web.UI namespace 中 TemplateControl 這個 class 中: GetLocalResourceObject (string resourcekey), 所以上述的問題, 假設我的 "Hello World" 是要在按下 button 的 event 中去 update 某個 label 的 text value, 只要在 event handler 中加下列一行 code 就好啦:

Label1.Text = GetLocalResourceObject("HelloWorld").ToString();

當然, 要記得在 xxx.resx 檔中加入 "HelloWorld" 這個 resource key 並指定它的value. 簡言之, 在 .net 2.0 中要 localize asp.net 2.0 的網站是越來越簡單了.