當地語系化 SharePoint 方案Localize SharePoint solutions

準備您的應用程式,使其可在全球使用的程式稱為「當地語系化」(當地語系化)。The process of preparing your applications so that they can be used worldwide is known as localization. 當地語系化會將資源轉譯成特定文化特性。Localization is translating resources to a specific culture. 如需詳細資訊,請參閱 全球化和當地語系化應用程式For more information, see Globalizing and Localizing Applications. 本主題提供如何將 SharePoint 方案當地語系化的總覽。This topic provides an overview on how to localize a SharePoint solution.

若要當地語系化方案,請從程式碼中移除硬式編碼的字串,並將它們抽象化為資源檔。To localize a solution, you remove hard-coded strings from the code and abstract them into resource files. 資源檔是副檔名為 XMLXML .resx 的檔案。A Resource file is an XMLXML-based file with a .resx extension. 資源檔包含解決方案中所使用之字串的翻譯版本。The resource file contains the translated versions of the strings used in your solution. 如需詳細資訊,請參閱 應用程式中的資源For more information, see Resources in Applications.

注意

只將字串資源加入至 SharePoint 方案資源檔。Add only string resources to SharePoint solution resource files. 雖然資源編輯器可讓您新增非字串資源,但是非字串資源也不會部署到 SharePoint。Although the Resource Editor enables you to add non-string resources, non-string resources do not deploy to SharePoint.

資源檔Resource files

資源檔有三種類型:預設、非語言相關,以及特定語言。There are three kinds of resource files: default, language-neutral, and language-specific.

資源檔案類型Resource File Type 描述Description
預設Default 預設資源檔也稱為 fallback 資源,包含針對預設文化特性(例如英文)當地語系化的字串。Also known as a fallback resource, default resource files contain strings localized for the default culture, such as English. 如果找不到指定語言的當地語系化資源檔,則會使用它們。They are used if no localized resource files for the specified language can be found. 預設資源沒有個別的檔案,它們會儲存在主要應用程式元件中。Default resources do not have separate files, they are stored in the main application assembly.
非語言相關Language-neutral 資源檔,其中包含針對語言(但不是特定文化特性)當地語系化的字串。A resource file that contains strings localized for a language, but not a specific culture. 例如,"fr" 代表法文。For example, "fr" for French.
特定語言Language-specific 資源檔,其中包含針對語言和文化特性當地語系化的字串。A resource file that contains strings localized for a language and a culture. 例如,"fr-CA" 適用于加拿大法文。For example, "fr-CA" for French Canadian.

如需詳細資訊,請參閱 階層式組織的當地語系化資源For more information, see Hierarchical Organization of Resources for Localization.

若要在中,指定您在中開發的 SharePoint 專案預設資源檔 Visual StudioVisual Studio ,請在新增資源檔時,在 [加入資源] 對話方塊的 [文化特性] 清單中選擇 [不區分文化特性] (非變異 國家)To specify default resource files in SharePoint projects that you develop in Visual StudioVisual Studio, choose Invariant Language (Invariant Country) in the culture list of the Add Resource dialog box when you add a resource file.

當地語系化 Visual Studio SharePoint 方案Localize Visual Studio SharePoint solutions

當您將方案當地語系化時,您應該考慮您的解決方案向使用者顯示的所有文字資訊。When you localize a solution, you should consider all of the textual information that your solution displays to users. 您必須將參考訊息、錯誤訊息和 UIUI 字串轉譯,以及將這些翻譯放在資源檔中。Informational messages, error messages, and UIUI strings must be translated and those translations placed in the resource files.

資源檔中的每個字串都有唯一的識別碼。Every string in a resource file has a unique identifier. 針對每個資源檔中的翻譯字串使用相同的識別碼。Use the same identifier for the translated string in each resource file. 例如,如果 "String1" 是預設資源檔中第一個字串的識別碼,請針對特定語言資源檔中的第一個字串使用相同的識別碼。For example, if "String1" is the identifier for the first string in the default resource file, use the same identifier for the first string in the language-specific resource files.

您通常會在 SharePoint 應用程式中將下列三個區域當地語系化 Visual StudioVisual Studio :功能、ASPX 頁面標記和程式碼。There are three areas you typically localize in Visual StudioVisual Studio SharePoint applications: features, ASPX page markup, and code. 為了方便說明,下列各節假設您有想要當地語系化成德文和日文的 SharePoint 方案。For purposes of illustration, the following sections assume you have a SharePoint solution that you want to localize into German and Japanese. 預設語言為英文。The default language is English.

當地語系化功能Localize features

若要將功能當地語系化,您必須將功能的硬式編碼標題和描述取代為參考當地語系化資源檔中翻譯標題和字串的運算式。To localize a feature, you have to replace the hard-coded title and description of the feature with an expression that references the translated title and string in the localized resources file. 您可以在的 功能設計 工具中進行這項變更 Visual StudioVisual StudioYou make this change in the Feature Designer in Visual StudioVisual Studio. 如需詳細資訊,請參閱 如何:當地語系化功能For more information, see How to: Localize a feature.

若要將您的英文功能當地語系化為德文和日文,請將三個資源檔專案專案新增至您的專案:一個用於英文、一個用於德文,另一個用於日文。To localize your English feature into German and Japanese, you add three Resource File project items to your project: one for English, one for German, and one for Japanese. 功能資源檔不能用來當地語系化 ASPX 標記或程式碼;需要個別的資源檔。Feature resource files cannot be used to localize ASPX markup or code; separate resource files are required for them.

在您建立功能資源檔之後,請在其中新增已轉譯的字串。After you create the feature resource files, add translated strings to them. 使用下列格式的運算式來存取當地語系化的字串:Access the localized strings with an expression in the following format:

$Resources:String ID

中的功能資源 Visual StudioVisual Studio 一律稱為資源。Feature resources in Visual StudioVisual Studio are always named Resources. 如果您選取非變異語言以外的語言,則會將文化特性 識別碼ID 新增至資源檔名稱。If you select a language other than Invariant Language, then a culture 識別碼ID is added to the resource file name. 例如,如果您將非變異語言新增 (預設) 功能資源檔,它就稱為 ResourcesFor example, if you add an invariant language (default) feature resource file, it is called Resources.resx. 如果您藉由選取日文 (日本) 的文化特性來新增特定語言的功能資源,則該檔案稱為 Resources. ja-jp. .resxIf you add a language-specific feature resource by selecting a culture of Japanese (Japan), the file is called Resources.ja-JP.resx. 功能資源檔名稱會自動指派,且無法變更。Feature resource file names are automatically assigned and cannot be changed.

功能資源的範圍是其新增的功能所在的區域。The scope of feature resources is local to the feature they are added to. 若要建立方案中任何功能或元素檔可使用的資源,請新增 全域資源檔 專案專案,而不是功能資源檔。To create resources that can be used by any feature or element file in the solution, add a Global Resources File project item instead of a feature resource File. 在 [加入新專案] 對話方塊中,[全域資源檔] 專案專案位於 [ SharePoint ] 下的 [ 2010 ] 資料夾中。The Global Resources File project item is located in the 2010 folder under SharePoint in the Add New Item dialog box. 全域資源檔會部署到 SharePoint 根資料夾的 \Resources 資料夾。Global resources files deploy to the \Resources folder of the SharePoint root folder.

當地語系化 ASPX 頁面標記Localize ASPX page markup

若要將 ASP.NETASP.NET 頁面當地語系化,您可以在專案中加入三個資源檔專案專案:一個用於英文、一個用於德文,另一個用於日文。To localize ASP.NETASP.NET pages, you add three Resources File project items to your project: one for English, one for German, and one for Japanese. 如果您不需要當地語系化標記以外的程式碼,您可以改為加入全域資源檔。If you do not have to localize code in addition to the markup, you can instead add Global Resources Files.

提供預設語言資源檔的名稱。Provide a name for the default language resource file. 提供當地語系化的資源檔,並以特定語言的文化特性附加相同的名稱 識別碼IDGive the localized resource files the same name appended with the language-specific culture 識別碼ID. 例如,適用于德文的 MyAppResources.de-de .resx 以及日文的 MyAppResources. ja-jp .resxFor example, MyAppResources.de-DE.resx for German and MyAppResources.ja-JP.resx for Japanese.

將每個資源檔的 [ 部署類型 ] 屬性設定為 [ [appglobalresource]。Set the Deployment Type property of each resource file to AppGlobalResource. 這會導致資源檔部署到 App_GlobalResources 資料夾,供方案中的所有 ASPX 頁面和控制項使用。This causes the resource files to deploy to the App_GlobalResources folder, where they are available to all ASPX pages and controls in the solution. App_GlobalResources 資料夾位於 C:\inetpub\wwwroot\wss\VirtualDirectories \<埠編號 > \ App_GlobalResources。The App_GlobalResources folder is located in C:\inetpub\wwwroot\wss\VirtualDirectories\<port number>\App_GlobalResources.

注意

如果您使用非全域資源檔,請將它們移到專案專案資料夾,以啟用 [部署類型] 屬性和其他 SharePoint 特定屬性。If you use non-global resource files, move them into the project item folder to enable the Deployment Type property and other SharePoint-specific properties.

ASPX 標記資源檔也可以用來當地語系化程式碼。ASPX markup resource files can also be used to localize code. 如果您使用資源來當地語系化 ASPX 標記以外的程式碼,請將每個檔案的 [組建動作] 屬性設定保留為 [內嵌資源],使資源編譯成附屬元件。If you are using the resources to localize code in addition to ASPX markup, leave the Build Action property setting of each file as Embedded Resource to cause the resource to compile into a satellite assembly. 但是,如果您只使用資源檔來將標記當地語系化,則可以選擇性地將 [組建] 動作變更為 [內容],以防止將檔案編譯成主要的應用程式元件。However, if you are using the resource files only to localize markup, you can optionally change Build Action to Content to prevent the file from being compiled into the main application assembly.

以下列格式的運算式取代 ASPX 頁面中的所有硬式編碼屬性字串和控制項標記:Replace all hard-coded property strings in your ASPX pages and controls markup with an expression in the following format:

<asp:<class> runat="server" Text="<%$Resources:<Resource File Name>, <String ID>%>" />

例如:For example:

<asp:Button ID="btn1" runat="server" onclick="btn1_Click" Text="<%$Resources:Resource1,String7%>"></asp:Button>

若為 ASPX 做為文字,請使用下列格式的運算式:For ASPX as text, use an expression in the following format:

<asp:literal ID="<ID>" runat="server" Text="<%$Resources:<Resource File Name>, <String ID>%>" />

例如:For example:

<asp:literal ID="Literal1" runat="server" Text="<%$Resources:Resource1, String9%>" />

如需詳細資訊,請參閱 如何:當地語系化 ASPX 標記For more information, see How to: Localize ASPX markup.

當地語系化程式碼Localize code

除了當地語系化功能字串和標記之外 ASP.NETASP.NET ,您還必須將出現在方案程式碼中的訊息字串和錯誤字串當地語系化。In addition to localizing Feature strings and ASP.NETASP.NET markup, you also have to localize the message strings and error strings that appear in your solution code. 當地語系化的參考和錯誤訊息會包含在附屬元件中。Localized informational and error messages are contained in satellite assemblies. 附屬元件包含使用者可以看到的字串,例如 UIUI 文字和輸出訊息,例如例外狀況。Satellite assemblies contain strings that are visible to users, such as UIUI text and output messages like exceptions.

Visual StudioVisual Studio 使用標準 .NET Framework 中樞和輪輻模型。uses the standard .NET Framework hub and spoke model. 中樞或主要程式元件包含預設語言資源。The hub, or main program assembly, contains the default language resources. 輪輻或附屬元件包含特定語言的資源。The spokes, or satellite assemblies, contain the language-specific resources. 如需詳細資訊,請參閱封裝和部署資源For more information, see Packaging and Deploying Resources. 附屬元件會從資源 (.resx) 檔案進行編譯。Satellite assemblies are compiled from resource (.resx) files. 當您將特定語言的資源檔新增至專案和方案套件時,會將 Visual StudioVisual Studio 資源檔編譯為名為 {project Name} .resources.dll 的附屬元件。When you add language-specific resource files to your project and the solution package, Visual StudioVisual Studio compiles the resource files into satellite assemblies named {Project Name}.resources.dll.

如同 ASPX 標記,將個別資源檔專案專案加入至專案,以當地語系化 SharePoint 應用程式程式碼;一個用於預設語言,另一個用於每個當地語系化的語言。As with ASPX markup, localize SharePoint application code by adding separate Resources File project items to your project; one for the default language and one for each localized language. 不過,如先前所述,如果您已經有可當地語系化 ASPX 標記的資源檔,您可以重複使用它們來當地語系化程式碼。However, as mentioned previously, if you already have resource files for localizing ASPX markup, you can reuse them for localizing code. 如果您需要建立資源檔,請為預設語言資源檔提供您選擇的名稱,並附加 .resx 副檔名。If you need to create resource files, give the default language resource file a name of your choice appended with a .resx extension. 將當地語系化的資源檔命名為與特定語言文化特性一起附加的相同名稱 識別碼IDName the localized resource files the same name appended with the language-specific culture 識別碼ID. 將每個資源檔的 [組建動作] 屬性設定為 [內嵌資源],以啟用附屬資源元件的建立。Set the Build Action property of each resource file to Embedded Resource to enable the creation of satellite resource assemblies.

若要建立附屬元件,請建立專案,然後透過 封裝設計 工具的 [ Advanced ] 索引標籤,將檔案新增為其他元件。To create the satellite assemblies, build the project and then add the files as additional assemblies through the Advanced tab of the Package Designer. 新增元件時,請在位置路徑前面加上文化特性 識別碼ID 資料夾,例如 de de \ {Project Item Name} .resources.dllWhen adding the assemblies, prepend a culture 識別碼ID folder to the Location path, such as de-DE\{Project Item Name}.resources.dll. 這可讓封裝包含具有相同名稱的檔案。This allows the package to contain files that have the same name.

在您的程式碼中,使用下列語法,將硬式編碼的字串取代為對方法的呼叫 GetGlobalResourceObjectIn your code, replace hard-coded strings with calls to the GetGlobalResourceObject method using the following syntax:

HttpContext.GetGlobalResourceObject("<Resource File Name>", "<String ID>")

如需詳細資訊,請參閱 如何:當地語系化程式碼For more information, see How to: Localize code.

Web 元件程式碼當地語系化Web part code localization

Web 元件包含自訂屬性編輯器功能,其中包括使用硬式編碼字串的程式碼屬性,例如 WebDisplayName、Category 和 WebDescription。Web parts include a custom property editor feature that includes code attributes that use hard-coded strings, such as WebDisplayName, Category, and WebDescription. 若要取代這些屬性的字串值,請建立衍生自屬性類別的不同類別。To replace the string values for these attributes, create a separate class that derives from the attribute's class. 在這些類別中,設定屬性的屬性。In those classes, set the attribute's property. Attribute 屬性相依于基類。The attribute property depends on the base class. 例如,WebDisplayName 屬性(attribute)屬性(attribute)是 DisplayNameValue,而 WebDescription 屬性(property)屬性(DescriptionValue)。For example, the WebDisplayName attribute property is DisplayNameValue and the WebDescription attribute property is DescriptionValue.

在衍生類別中,從資源檔和 ResourceManager 物件參考字串識別碼,以取得字串識別碼的當地語系化值。In the derived class, reference the string ID from the resource file and the ResourceManager object to get the localized value for the string ID. 將此值傳回至屬性編輯器屬性。Return this value to the property editor attribute.

另請參閱See also