Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've seen other similar questions (like this one), checked Microsoft documentation (like all the ones stemming from and related to the page Localization of Sandboxed Solutions in SharePoint 2010), plus other articles (JohnWPowell and SPbits).

Anyway, I'm not able to solve the following issue:

having a SP2010 Sandboxed solution in VS2010 with a code-only webpart, how do I localize the webpart's Title, Description (in .webpart file), Group, QuickAddGroup (elements.xml)?

Thanks everyone

Additional notes:

I'm able to localize feature title and description, as well as webpart content by code (e.g . some label's text). I'm also aware about custom Attributes in order to localize webpart Properties when the user edits them in browser.

I was able to do this in SP2007, through some Visual Studio random magic (BuildAction + CopyToOutput + CustomTool + Deployment Location + Deployment Type + editing manifest ... don't ask me how :) ), but most of those options are not available in a Sandbox solution.

Edit

After re-reading docs, I've spotted this note which puts the final word at least on the .webpart file:

Strings in the .webpart file of a sandboxed solution can be localized only if localized resource files have been separately installed to the file system as part of a farm solution. In most situations in which you are creating a sandboxed solution, it is because you do not have permission to install farm solutions on the target farms. In such cases, there is no practical way to localize the .webpart file in a sandboxed solution. This means that the name of the Web Part in the Web Part gallery is the same in sites of all languages.

Any hint on how to do this at least programmatically, e.g. by adding the webpart to the gallery in a feature event receiver?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

I assume you cannot install a Farm Solution which includes resource files as part of a Hybrid approach? If you can then this does get around the problem.

If not: (sorry if you know this already) as you are probably aware, resources referenced in a .webpart (or.dwp) file look something like this: "$Resources:core,WebPartTitle;" where "core" refers to the resource file name and "WebPartTitle" refers to the resource string name. These resources are resolved when the .webpart file is deployed as part of a feature and they are resolved by looking at the language of the root site because this is where the web part gallery exists. It's a one-time only hit at the point of provisioning into the site.

In a Sandbox, you can only reference server installed resource files, not resources deployed as part of the Sandbox solution. So, the only way around this appears to be to use code like a Feature Receiver. On Feature Activated, I would suggest you get a handle on the webpart file in the Web Part Gallery (using SPWeb.GetCatalog) and then do a simple search and replace, resolving the resource reference programmatically by looking for the resource string which is stored in the Feature Receiver assembly.

share|improve this answer
    
Thanks for pointing this out. I've read a couple of links related to the point, like this SP.SE and this Anders. I'm making up an answer, based on your hints. –  superjos Sep 9 '11 at 13:50
add comment

In this case the simple way to localize a web part title is modify the title field of list item:

SPList catalog = web.GetCatalog(SPListTemplateType.WebPartCatalog);

SPQuery query = new SPQuery();
query.Query = string.Format( @"<Where><Eq><FieldRef Name='LinkFilename'/><Value Type='Computed'>{0}</Value></Eq></Where>", webPartName);

SPListItem webPartItem = catalog.GetItems(query).Cast<SPListItem>().FirstOrDefault();
webPartItem[SPBuiltInFieldId.Title] = WebPartsResources.Title;
webPartItem.Update();
share|improve this answer
    
thanks for your integration to the discussion! I will include it in code as soon as I get back to the localization requirement. –  superjos Oct 12 '11 at 14:04
add comment

I'm building this one based on Steve answer and other links, with the major steps needed. It might be helpful for the next ones coming. Please do correct if wrong.

  1. Get the web part gallery as an SPList with GetCatalog(SPListTemplateType.WebPartCatalog)
  2. Find the item in the list corresponding to your web part, e.g. by matching on SPListItem.Name
  3. Create an XmlTextReader based on web part file with XmlReader xmlReader = new XmlTextReader(listItem.File.OpenBinaryStream());
  4. Create an XmlDocument based on the reader with XmlDocument xmlDoc = new XmlDocument(xmlReader)
  5. Create an XPathNavigator based on the document with XPathNavigator navigator = xmlDoc.CreateNavigator()
  6. Get a single XPathNavigator node for web part title with XPathNavigator titleNode = navigator.SelectSingleNode(titleXPath), where titleXPath is "/webParts/webPart/data/properties/property[@name=Title]"
  7. Set its value taken from localized resources + autogenerated class with titleNode.SetValue(MyResources.SomeClass.MyWebPartTitle)
  8. Repeat 6. and 7. for the web part description (XPath is "/webParts/webPart/data/properties/property[@name=Description]")
  9. Save the XmlDocument with xmlDoc.Save()
  10. Put all this in FeatureReceiver.FeatureActivated method of the feature containing your web part.

Other credits to Cliff, Anders and James.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.