Thursday, April 18, 2013

Overwrite Translations in Default Language from User Specified Text in an Alternate Language

As I blogged in another post, Multilingual User Interface feature (Alternate languages) in SharePoint 2010 allows a logged on user to change the language to one of the supported alternate languages configured by the site administrator.

For an example, in a SharePoint site if alternative languages feature is enabled and let’s say English is set as the default language of the site and Spanish is added as an alternative language. Now if we change the site title using English UI, it won’t affect the Spanish site. But if we want, there is an option in SharePoint to overwrite the site title as well as description in alternate language UIs, from the user specified text in the default language.


However, in a scenario where set of Spanish authors update content in this site, they might want to use the Spanish UI to change the website title in English UI. There is no out of the box option to support this scenario and we will have to write some custom code here.

Option 1: we can add a custom action link in Site Settings page and link to an application page with piece of custom code.
Option 2: With the application page approach, users or administrators have to manually trigger the update when needed. If we want it to work seamlessly, without the need to click another link after changing site title, we can use a timer job to copy the changes.

With either approach following piece of code can be used to copy the site title and description between alternate languages. It basically uses the TitleResource and DescriptionResource properties of the SPWeb object.

private void OverwriteSpanishTranslations()
{
  CultureInfo englishCultuInfo = new CultureInfo(1033);
  CultureInfo spanishEsCultuInfo = new CultureInfo(1034);

  using (SPSite site = new SPSite(SPContext.Current.Site.ID))
  {
    using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
    {
      // Title
      string titleSetInEnglishUI = web.TitleResource.GetValueForUICulture(englishCultuInfo);
      web.TitleResource.SetValueForUICulture(spanishEsCultuInfo, titleSetInEnglishUI);

      // Description
      string descriptionSetInEnglishUI = web.DescriptionResource.GetValueForUICulture(englishCultuInfo);
      web.DescriptionResource.SetValueForUICulture(spanishEsCultuInfo, descriptionSetInEnglishUI);

      web.Update();
    }
  }
}

No comments: