views:

602

answers:

3

I'm working on some SharePoint web parts and I'm trying to make them as locale-independent as possible. I've got all most text in resource files, and I'm looking at the attributes on my Web Part:

[WebBrowsable(true),
Category("My Category"),
WebDisplayName("Display Name here"),
WebDescription("Tells you all about it"),
Personalizable(PersonalizationScope.Shared)]
public string SomeProperty { get; set; }

It would be nice to replace those hard-coded strings with something more useful to users (SharePoint administrators in this case) who don't use English.

What are my options, if any?

+1  A: 

You are looking for the Microsoft.SharePoint.WebPartPages.ResourcesAttribute class.

This blog post has a description of it's use and a simple example.

//RESOURCES LOCALIZATION
//Property that is localized. Use the ResourceAttibute.
//[ResourcesAttribute (PropertyNameID=1, CategoryID=2, DescriptionID=3)]
[Resources("PropNameResID", "PropCategoryResID", "PropDescriptionResID")]
spoon16
Many thanks - my implementation of your answer is below.
+1  A: 

Here's my implementation of spoon16's answer:

    [WebBrowsable(true),
    Resources("SearchWebPartWebDisplayName", 
    "SearchWebPartCategory", 
    "SearchWebPartWebDescription"),
    FriendlyName("Display Name here"),
    Description("Tells you all about it"),
    Category("My Category"),
    Personalizable(PersonalizationScope.Shared)]
    public string SomeProperty { get; set; }

    public override string LoadResource(string id)
    {
        string result = Properties.Resources.ResourceManager.GetString(id);
        return result;
    }

Note the change of property names and their order in the attributes block.

I also had to change my WebPart to derive from Microsoft.SharePoint.WebPartPages.WebPart, with the attendant changes to how I handle the Width and Height of my WebPart.

+1  A: 

You can just create subclasses from the normal ASP.NET attributes and localize those. THis approach is legacy and should not be used for your new web parts. Do not derive from the SP Web Part when there is no need.

http://forums.asp.net/t/937207.aspx

Noted. Thanks Wouter!