如何:當地語系化程式碼How to: Localize code

未當地語系化程式碼會使用硬式編碼的字串值。Unlocalized code uses hard-coded string values. 若要將程式碼字串當地語系化,請將它們取代為的呼叫 GetGlobalResourceObject ,也就是參考當地語系化資源的方法。To localize code strings, replace them with calls to GetGlobalResourceObject, which is a method that references localized resources.

當地語系化程式碼Localize code

當地語系化程式碼To localize code

  1. 方案總管 中,開啟專案專案的快捷方式功能表,然後選擇 [加入 > 模組]。In Solution Explorer, open the shortcut menu for a project item, and then choose Add > Module.

    選擇 [ 資源檔 ] 範本。Choose the Resources File template.

    注意

    請務必將資源檔新增至 SharePoint 專案專案,以便使用 [部署類型] 屬性。Be sure to add the resource file to a SharePoint project item so that the Deployment Type property is available. 稍後在此程式中需要這個屬性。This property is required later in this procedure.

  2. 為預設語言資源檔提供您選擇的名稱,並附加 .resx 副檔名,例如 MyAppResources .resxGive the default language resource file a name of your choice appended with a .resx extension, such as MyAppResources.resx.

  3. 重複步驟 1 和 2,將個別資源檔加入至 SharePoint 專案項目:每個當地語系化語言擁有一個資源檔。Repeat steps 1 and 2 to add separate resource files to the SharePoint project item: one for each localized language.

    針對每個當地語系化的資源檔使用相同的基底名稱,但新增文化特性識別碼。Use the same base name for each localized resource file, but add the culture ID. 例如,將德國當地語系化的資源命名為 MyAppResources.de-de .resxFor example, name a German localized resource MyAppResources.de-DE.resx.

  4. 開啟每個資源檔並新增當地語系化的字串。Open each resource file and add localized strings. 在每個檔案中使用相同的字串 ID。Use the same string IDs in each file.

  5. 將每個資源檔的 [ 部署類型 ] 屬性值變更為 [ [appglobalresource ],使每個檔案都部署到伺服器的 App_GlobalResources 資料夾。Change the value of the Deployment Type property of each resource file to AppGlobalResource to cause each file to deploy to the server's App_GlobalResources folder.

  6. 將每個檔案的 [ 組建動作 ] 屬性值保留為 [ 內嵌資源]。Leave the value of the Build Action property of each file as Embedded Resource.

    將內嵌資源編譯為專案的 DLL。Embedded resources are compiled into the project's DLL.

  7. 建立專案以建立資源附屬 Dll。Build the project to create the resource satellite DLLs.

  8. 封裝設計 工具中,選擇 [ Advanced ] 索引標籤,然後加入附屬元件。In the Package Designer, choose the Advanced tab, and then add the satellite assembly.

  9. 在 [位置] 方塊中,于位置路徑前面加上文化特性識別碼資料夾,例如 de \ <Project Item Name>.resources.dllIn the Location box, prepend a culture ID folder to the Location path, such as de-DE\<Project Item Name>.resources.dll.

  10. 如果您的方案尚未參考 system.string 元件,請在其中加入參考,然後在您的程式碼中加入指示詞 System.WebIf your solution does not already reference the System.Web assembly, add a reference to it, and add a directive in your code to System.Web.

  11. 找出程式碼中所有使用者可見的硬式編碼字串,例如 UI 文字、錯誤和訊息文字。Locate all hard-coded strings in your code that are visible to users, such as UI text, errors, and message text. 使用下列語法呼叫 GetGlobalResourceObject 方法取代這些字串:Replace them with a call to the GetGlobalResourceObject method using the following syntax:

    HttpContext.GetGlobalResourceObject("Resource File Name", "String ID")
    
  12. 選擇 F5 鍵來建立和執行應用程式。Choose the F5 key to build and run the application.

  13. 在 SharePoint 中,從預設值變更顯示語言。In SharePoint, change the display language from the default.

    當地語系化的字串會出現在應用程式中。The localized strings appear in the application. 若要顯示當地語系化的資源,SharePoint 伺服器必須安裝符合資源檔文化特性的語言套件。To display localized resources, the SharePoint server must have a language pack installed that matches the resource file's culture.

另請參閱See also