Monday, May 18, 2009

Localizing Sharepoint web sites and web parts

Localizing Websites and webparts in SharePoint

Internationalization is not a topic which is widely discussed by sharepoint communalities. Recently when I was researching this topic I came across with several interesting strategies of accomplishing the localization of sharepoint websites and webparts, which I think worth to discuss here. I will discuss these two areas separately and first let's discuss about internationalizing websites.

Localizing Sharepoint sites

Internationalization of web sites provided by sharepoint can be done only by installing relevant language packs which can be download from Microsoft web site. Once relevant language packs are installed site collections and sites are available with specific language options with a dropdown list to select.


So before you create the site you have to select the desired language of the web site or collection. Once you create by specifying the language, site is created using the specified language's template files.

Following languages packs and service packs need to be installed. After installing each pack, setup will ask to run the SharePoint configuration wizard. Run configuration wizard for successful completion.

  1. Install Windows SharePoint Services 3.0 Language Pack

    http://www.microsoft.com/downloads/details.aspx?FamilyId=36EE1BF0-652C-4E38-B247-F29B3EEFA048&displaylang=en

  2. Install Windows SharePoint Services 3.0 Language Pack Service Pack

    http://www.microsoft.com/DownLoads/details.aspx?familyid=05046B1D-DD7B-456A-8838-8D978C5F3579&displaylang=en

  3. Install 2007 Office System Language Pack. When downloading select the correct language at the language drop down to get the service pack of specific language.

    http://www.microsoft.com/downloads/details.aspx?FamilyID=2447426b-8689-4768-bff0-cbb511599a45&displaylang=en


 

  1. Install the 2007 Microsoft Office Servers Language Pack Service Pack 1. When downloading select the correct language at the language drop down to get the service pack of specific language.

    http://www.microsoft.com/DownLoads/details.aspx?familyid=3A6C26FD-0BEB-40D5-8CBA-15164FAAB150&displaylang=en


 

That's it. SharePoint language specific configurations and templates are installed into your Sharepoint front end server and now it's ready create new sites using new language.

Localizing sharepoint custom web parts

The default web parts provided by Sharepoint installation are automatically localized according to the language you select once you install the language pack. But when you develop custom web parts for your business needs, those web parts need to be localized using different language resource files.

Normally sharepoint sites are not localized according to user's browser language, but according to the language you use to create the site. So all the web parts inside the site should also behave in the same manner. Let's see how we can accomplish this.

  1. Create a library project in Visual studio (VS). I name it "langwebpart".
  2. Add a Reference to Microsoft.SharePoint.dll
  3. Update assembly version and and allow partially trusted callers
    1. In Solution Explorer, double-click the AssemblyInfo file

    [assembly: AssemblyVersion("1.0.0")]

    1. Add the following line to the top of the file:

    using System.Security;

  1. Add the following line to the bottom of the file

    [assembly: AllowPartiallyTrustedCallers]


 

  1. Add a new class. I name it as "userinfowebpart"
  2. Inherit userinfowebpart from Microsoft.SharePoint.WebPartPages
  3. Override the render method of WebpartPages

    protected
    override
    void Render(HtmlTextWriter output){};


     

  4. Create two resource files for English and Norwegian named userinfo.resx and userinfo.nb-NO.resx and add relevant language strings values with their key names.
  5. These localized strings can be used inside your programming code as follows.

    nameLbl.Text = langwebpart.userinfo.name;


    langwebpart is the namespace


    userinfo is the language file name

    name is the key.

Below is our code of the simple web part class.

namespace langwebpart{


public
class
userinfowebpart:Microsoft.SharePoint.WebPartPages.WebPart

{


Label nameLbl;


 


protected
override
void Render(HtmlTextWriter output)

{


try

{

RenderChildren(output);

}


catch (Exception e)

{


 

output.Write(e.StackTrace); ;

}


 


 

}


protected
override
void CreateChildControls()

{

nameLbl = new
Label();

nameLbl.Text = langwebpart.userinfo.name;

Controls.Add(nameLbl);

}


 


 

}

}


 

Developing localized web part is simple as mentioned above. Next step is to deploy the web part to the SharePoint server instance.

Deploying localized web part

There are several ways to deploy a web part to SharePoint. One of the easiest and simple way is to copy the relevant assemblies to bin directory of the SharePoint web application instance.

  1. Deploying to bin directory
  • Copy assembly DLL to either
    - /bin directory for a given IIS virtual server (e.g., c:\inetpub\wwwroot\bin) for globally available or to specific web application /bin folder to available only that web application.
  • Copy DWP file to C:\Inetpub\wwwroot\wpcatalog
  • Copy resources to C:\Inetpub\wwwroot\wpresources
  • Adjust web.config
    - Register as SafeControl
    - Select Code Access Security settings


     

  1. Deploying to as a Webpart package

A Web Part package is a cabinet (.cab) file that contains the following items:

  1. Manifest.xml (mandatory)
  2. Web Part assemblies (optional)
  3. Class resource files (optional)
  4. .dwp files (optional)

Creating a Manifest File for a .cab File

The Manifest file, a configuration file that describes the structure and contents of the .cab file, is the only required component for the .cab file. The Stsadm.exe tool uses this file to deploy the remaining content of the .cab file.

The following example shows a manifest file for a package that deploys two assemblies and their resources:

<?xml
version="1.0"
encoding="utf-8" ?>

<WebPartManifest
xmlns="http://schemas.microsoft.com/WebPart/v2/Manifest">

<Assemblies>

<Assembly
FileName="langwebpart.dll">

<ClassResources>

</ClassResources>

<SafeControls>

<SafeControl
Namespace="langwebpart"
TypeName="*"/>

</SafeControls>

</Assembly>

</Assemblies>

<DwpFiles>

<DwpFile
FileName="webpart.dwp"/>

<DwpFile
FileName="nb-NO\webpart.dwp"/>

</DwpFiles>

</WebPartManifest>


 

Creating a .cab File

Developers can use either of the following methods to create a .cab file:

  • Use Microsoft Visual Studio® .NET Professional to create a CAB Project.
  • Use MakeCAB.exe, a command-line tool included with Microsoft Windows 2000 and later.

Using Visual Studio

To use Visual Studio.NET Professional to create a CAB project, follow these steps:

  • Start Visual Studio .NET.
  • Create a new solution.
    • On the File menu, point to New, and then click Blank Solution.
    • Type a name for the solution, and then click OK.
  • Open the Web Part project within this solution.
    • In Solution Explorer, right-click the new solution, point to Add, and then click Existing Project.
    • Browse to the location of Web Part project that you want to add, click the project, and then click Open.
  • Create a new CAB project
    • In Solution Explorer, right-click the new solution, point to Add, and then click New Project.
  • Under Project Types, click Setup and Deployment Projects, and then under Templates, click Cab Project.
    • Type a name for the CAB project, and then click OK.
  • Add the Web Part project output to the CAB project.
    • In Solution Explorer, right-click the CAB project, point to Add, and then click Project Output.
    • In the Project box, click the Web Part project that you added to the solution in step 3.
    • Select all the appropriate file categories that appear in the list, and then click OK.

    Make sure that you select Primary output and Content Files. These categories add the Manifest.xml file, the .dwp files, and the Web Part assembly to the .cab file. Verify that the Manifest.xml file and the .dwp files are marked as content for build action in their respective properties sheets.

  • Build the solution to create the .cab file.

Using MakeCAB.exe

MakeCAB.exe is a tool included with Windows 2000 or later. To use MakeCAB.exe to create a .cab file, follow these steps:

  1. Create a directive file for makecab.exe, and save the file with a .ddf extension. The following sample directive file (named langwebpart.ddf) creates a .cab file for a simple Web Part library project:

;*** Sample Source Code MakeCAB Directive file example


 

.OPTION EXPLICIT ; Generate errors

.Set CabinetNameTemplate=langwebpart.cab

.set DiskDirectoryTemplate=CDROM

.Set CompressionType=MSZIP

.Set UniqueFiles="OFF"

.Set Cabinet=on

.Set DiskDirectory1=langwebpart.CAB

Manifest.xml

webpart.dwp

langwebpart.dll

.Set DestinationDir=nb-NO

nb-NO\webpart.dwp

;*** <the end>

  1. Copy all the files that you want to include in the .cab file into the directory where you created the .ddf file. In this example, these files are the Manifest.xml file, the WebPart.dwp file, and the langwebpart.dll file.
  2. Open a command prompt, change to the directory that contains the .ddf file and the files that you want to include in the .cab file, and then run the following command:

    Makecab.exe /F langwebpart.ddf

MakeCAB.exe creates a directory named langwebpart.cab, and in this directory MakeCAB.exe creates a .cab file named langwebpart.cab that contains the all the files listed in the langwebpart.ddf directive file.

Creating a CAB File with Localized Resources

Visual Studio does not support creating .cab files with internal directories. However, localized Web Parts may require a .cab file with internal directories. Because of this you must use a tool like MakeCAB.exe or another, third-party CAB utility to deploy localized Web Parts.

Deploying a Web Part Package

Administrators can use the Stsadm.exe tool to deploy .cab files created by developers. Stsadm.exe is a command-line tool that you can use to manage a Windows SharePoint Services computer. Three Stsadm.exe options apply to Web Part package deployment:

  • AddWPPack
  • DeleteWPPack
  • EnumWPPacks

For more information about a specific Stsadm.exe command-line option, type the following line at a command prompt:

stsadm –help option

Adding a Web Part Package

To use the Stsadm.exe command to deploy a Web Part package on a server computer running Windows SharePoint Services, open a command prompt, and then type the following command:

stsadm.exe -o addwppack 

-filename Web_Part_Pack_name

[-url URL]

[-globalinstall]

[-force]

If you want to install a Web Part package on a virtual server when the Web Part package is already installed on another virtual server on the same computer, use the following command:

stsadm.exe -o addwppack

-name Web_Part_Pack_name

[-url URL]

[-globalinstall]

[-force]

Deleting a Web Part Package

To delete a Web Part package from specified virtual servers on a computer running Windows SharePoint Services, open a command prompt, and then type the following command:

stsadm.exe -o deletewppack

           -name Web_Part_Pack_name

           [-url URL]

To decide whether to install a Web Part package in the GAC or in the Bin directory, ask the following questions:

  • Do you want to make this package available to all virtual servers on your computer?
  • Do you trust the assemblies and resources of the package completely?

If you answer "yes" to both of these questions, install the Web Part package to the GAC. Assemblies in the GAC are fully trusted and are available to all virtual servers.


For more info http://msdn.microsoft.com/en-us/library/dd583149.aspx

1 comment:

Unknown said...

If you need to localize websites, you can have a look at https://poeditor.com/, a collaborative translation management platform with a user-friendly work interface and integrated features that will improve and automate the localization process.