SharePoint 2010 Localization with Visual Studio 2010


In this walkthrough, I’ll show you how to create a SharePoint 2010 localized feature using Visual Studio 2010.  The basic mechanics of localizing a SharePoint 2010 feature are the same as localizing a SharePoint 2007 feature, and is well documented.  If you are unfamiliar with localization in SharePoint, please see Additional Resources at the end of this article.  In a nutshell, localized resources are stored in Resources (.resx) files, and in SharePoint exist in one of the following locations:



  • \14\Template\Features\<Feature Name>\Resources\

  • \14\Resources\

  • \14\Config\Resources\

  • <Virtual Directory>\App_GlobalResources\ (note: resources files from \14\Config\Resources are copied here when provisioned)

Resources files follow a naming convention that consists of a base name such as “HelloLocalization.resx” and culture-specific resources such as “HelloLocalization.es-ES.resx.”  In SharePoint, these files must be in a particular location depending on usage.  In this walkthrough, I’ll show you how to create a “centralized” resources files that will be stored in 14\Resources.  My preference is to store custom files in as few locations as possible within the SharePoint hive.  I have also found that certain resources cannot be accessed from certain locations.  So with that in mind, let’s get started:


Since we are deploying to 14\Resources, we need to add a mapped folder.  Select Add > SharePoint Mapped Folder…


image


Select the Resources folder:


image


In the solution, delete the subfolder that was created in the Resources folder.  Next, right-click on the mapped folder and select Add New Item.  In the Search Installed Templates search box, type Resources to select the Resources file template.  Rename the resources file to something unique to your feature:


image


Set the Build Action to Content for the resources file:


image


Add your localized strings to the resource file:


image


For each feature, set the Default Resource File to the resource file name.  This enables you to reference a resource without fully qualifying it:


image


Optional: you can hide a feature when localized resources are not available for it by setting the Require Resources setting to True:


image


Now you can use the localized strings throughout your feature such as in the feature title and description:


image


When deployed, the feature appears like this in Site Collection Features:


image


The resources can also be used in the .webpart file:


<?xml version=1.0encoding=utf-8?>
<
webParts>
  <
webPart xmlns=http://schemas.microsoft.com/WebPart/v3>
    <
metaData>
      <
type name=HelloLocalization.LocalizedWebPart.LocalizedWebPart, $SharePoint.Project.AssemblyFullName$/>
      <
importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
    </
metaData>
    <
data>
      <
properties>
        <
property name=Titletype=string>$Resources:HelloLocalization,WebPartTitle</property>
        <
property name=Descriptiontype=string>$Resources:HelloLocalization,WebPartDescription</property>
      </
properties>
    </
data>
  </
webPart>
</
webParts>


Which appears like this in the Web Part Gallery:


image


When you add the web part to a page, it appears like this:


image


The localized resources can also be used in the web part:

protected void Page_Load(object sender, EventArgs e)
{
var lang = SPContext.Current.Web != null ? SPContext.Current.Web.Language : 1033;
webPartMessage.Text = SPUtility.GetLocalizedString(
“$Resources:WebPartMessage”,
“HelloLocalization”,
lang);

}


Which appears like this when added to a page:


image


To support a different culture, copy the resources file, and rename it to [ResourcesFileName].[Culture].resx.  For example, to create a Spanish (Spain) resources file:


image


Make the culture-specific changes to the resources strings:


image


If the language pack is installed on the SharePoint server, and the site has been created using that language, the resources file will be used:


image


image


image


For completeness, you should also be aware of the Visual Studio 2010 Feature Resources feature.  When you select Add Feature Resource…:


image


you are prompted for the culture, and the appropriately named resource file is created automatically:


image


Conclusion


In this walkthrough, I showed you how to localize a SharePoint 2010 feature.  Even if you aren’t targeting multiple languages, in my opinion it’s good practice to store resources outside of your compiled code.  I hope you found this useful, and your feedback is welcomed!


Additional Resources


Comments (14)

  1. darnals says:

    Its great thanks

    <a href="http://kag3.co.cc&quot; rel="do follow">MMORPG Server</a>  <a href="http://proxy-jail.co.cc&quot; rel="do follow">Web Proxy</a>

  2. BinaryJam says:

    Hi,

    first why are your pictures not displaying, but I can click them and they open ? Is this just me ?

    Having been using Resources for a while in 2007 I know the pain of using them and 2010 has just made things a little easier and a little harder.

    We use the resources as Embedded Resources as well as Feature/XML resources, so why have MS not provided this capability.  If a resource is in that Resources folder and its embedded why are you just not copying it as well.

    I suppose my way around this is two resource sets internal and external, but what If I share content, now thats two files to keep in step.

    This is almost there, would appreciate some feedback if your on that team or perhaps you could give them a prod 😉

  3. Philip says:

    Great Post! Thank you.

    Do you know how does it works with Localized Resources in Sandbox Solutions? I think there is now "Mapped Folder" allowed? Right?

    Thanks.

    philip

  4. I did some further investigation on RESX file localization in SharePoint 2010:

    http://www.fkollmann.de/…/SharePoint-2010-resource-files-have-to-be-complete.aspx

  5. ondecity network blogger says:

    its most better

    http://ondecity.com

  6. Mekatani says:

    http://mekatani.blogspot.com untuk pupuk semi organik

  7. Jose says:

    Great Job but I'm getting a problem, I've used your method to include resources in a web part. But when I tried to use a resource in a label in the .ascx code I got an error:

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

    Parser Error Message: The resource object with key 'ComparativeForm' was not found.

    But If I use the code behind like you show in the example:

    var lang = SPContext.Current.Web != null ? SPContext.Current.Web.Language : 1033;

    lblHeaderTitle.Text = SPUtility.GetLocalizedString("$Resources:lblHeaderTitle", "ComparativeForm",lang)

    everything goes right.

    My cuestion is, do you know how to do that? In my opinion is more elegant to write the resources in the ascx..

    Thanks

  8. Alaa says:

    Hello Jose,

    I had quite the same problem, I solved it in deploying the resource file in {SharePointRoot}Resources . It seams that if you want to access a resource file from a webpart you have to deploy it in the {SharePointRoot}Resources folder and if you want to access a resource file from a usercontrol you have to deploy it in the {WebApplicationRoot}App_GlobalResources

  9. Nice post and a very useful one. We need to implement variations for 6 languages and this has helped  us a lot :).

    Thank you very much.

  10. dotNetFollower says:

    Veri nice article. In my blog I have similar article on how to  use SPUtility.GetLocalizedString – <a href="dotnetfollower.com/…/a>

  11. cherry says:

    British reporters are known for doing almost anything to get a Mulberry Bags. But reports that a newspaper secretly listened to telephone messages of murdered schoolgirls and other private citizens have produced Mulberry Handbags and anger.

    On Friday, British police arrested Andy Coulson, former editor of Mulberry Bag Britain's best-selling newspaper, News of Mulberry Outlet the World. The investigation led him to Mulberry UK Sale resign in January as communications director to Prime Minister David Cameron.

    The arrest came in a widening investigation of Mulberry UK telephone hacking. Other accusations include paying police for mulberry shoulder bags information on stories. The Reuters news agency reported that Mr. Coulson was released on Mulberry Bag UK until a date in October.

    Prime Minister Cameron promised Men's Mulberry Bags Friday that a judge will lead a full public inquiry into Women's Mulberry Bags the case after police complete their investigation.

    DAVID CAMERON: "Murder victims, terrorist victims, families who have lost loved ones, sometimes defending our country, that these people could have had their phones hacked into, in order to generate stories for Mulberry Bags  Mulberry Handbags  Mulberry Bag   Mulberry Outlet  Mulberry UK  mulberry bayswater bag  Mulberry Alexa Bag, is simply disgusting."

  12. Rockie_ says:

    John, it's

    Great, thanks!

    Best regards,

    Gennady

  13. vladimir says:

    great thanks!!!