How to: Localize Code

Unlocalized code uses hard-coded string values. To localize code strings, replace them with calls to GetGlobalResourceObject, which is a method that references localized resources.

Localizing Code

To localize code

  1. Add separate resource files to a SharePoint project item: one for the default language and one for each localized language.

    To do this, right-click a project item node in Solution Explorer, point to Add, click New Item, and then select Resources File.

    Note

    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. Give the default language resource file a name of your choice appended with a .resx extension, such as MyAppResources.resx. Use the same base name for each localized resource file, but add the culture ID. For example, name a German localized resource MyAppResources.de-DE.resx.

  3. Open each resource file and add localized strings. Use the same string IDs in each file.

  4. Change the Deployment Type property of each resource file to AppGlobalResource to cause each file to deploy to the server's App_GlobalResources folder.

  5. Leave the Build Action property of each file as Embedded Resource.

    Embedded resources are compiled into the project's DLL.

  6. Build the project to create the resource satellite DLLs.

  7. In the Package Designer, click the Advanced tab and add the satellite assembly.

  8. In the Location box, prepend a culture ID folder to the Location path, such as de-DE\Project Item Name.resources.dll.

  9. If 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.

  10. Locate all hard-coded strings in your code that are visible to users, such as UI text, errors, and message text. Replace them with a call to the GetGlobalResourceObject method using the following syntax:

    HttpContext.GetGlobalResourceObject("Resource File Name", "String ID")
    
  11. Press F5 to build and run the application.

  12. In SharePoint, change the display language from the default.

    The localized strings appear in the application. To display localized resources, the SharePoint server must have a language pack installed that matches the resource file's culture.

See Also

Tasks

Walkthrough: Localizing a Visual Studio SharePoint Application

How to: Localize a Feature

How to: Localize ASPX Markup

How to: Add a Resource File

Concepts

Localizing SharePoint Solutions