2009-07-18 51 views

回答

3

如果你不想與訪問修飾符一塌糊塗,你可以做一個幫手來簡化你必須按順序訪問資源編寫代碼文件是這樣的:

public static class LocalizationHelper 
{ 
    public static string Localize(this HtmlHelper helper, string key) 
    { 
     var resourceObject = helper.ViewContext.HttpContext.GetGlobalResourceObject("NameOfResourceFileClass", key); 
     if (resourceObject == null) 
     { 
      // i don't recommend throwing the Exception class, I'd define my own Exception type here 
      throw new Exception(String.Format("Resource key '{1}' could not be found in Resource class '{0}'","NameOfResourceFileClass", key)); 
     } 

     return resourceObject.ToString(); 
    } 
} 

然後在你的.master ...

<%= Html.Localize("NameOfResourceKey") %> 
2

您應該使用全局資源文件。

  1. 創建App_GlobalResources asp.net文件夾
  2. 您的語言與My.Resources.Resource.MyText(VB語法)創建資源文件
  3. 設置文件的訪問修飾符來Public
  4. 訪問所有的資源

alt text

從源代碼訪問母版頁的資源:

<asp:Literal ID="Literal2" runat="server" Text="<%$ Resources:ResourcesFileName, ResourcesName%>" /> 
相關問題