More in this section

Forums / Developing with Sitefinity / Copy generic content from another language

Copy generic content from another language

3 posts, 0 answered
  1. Priya Ramesh
    Priya Ramesh avatar
    26 posts
    Registered:
    23 Jul 2009
    27 Apr 2010
    Link to this post
    HI,
    I want to copy all my generic content to a new language. I have like 30 items.Is there an easy way to copy Generic content from another language. (Like when we create a page in a new language it asks if we want to copy it from another page.)
    Thanks.
    Priya
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    27 Apr 2010
    Link to this post
    Hi Priya Ramesh,

    1. Create App_Code folder under your project root.
    2. Create a new class called TranslateContent
    3. Paste the code below to this class and save it.


    using System.Collections.Generic;
    using System.Globalization;
    using Telerik;
    using Telerik.Cms.Engine;
    using Telerik.Localization;
      
    public class TranslateContent
    {
        public static void Translate(object sender, ExecutedEventArgs e)
        {
            if (e.CommandName == "CreateContent")
            {
                IContent source = (IContent)e.Data;
                if (source != null && LocalizationManager.IsDefaultCulture)
                {
                    CultureInfo backupCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
                    string originalContent = source.Content.ToString();
                    string sourceCulture = source.Language;
      
                    ContentManager manager = new ContentManager(source.ProviderName);
                    foreach (KeyValuePair<string, CultureInfo> pair in Telerik.Localization.LocalizationManager.Cultures)
                    {
                        System.Threading.Thread.CurrentThread.CurrentUICulture = pair.Value;
                        IContent target = manager.GetStagedContent(source.ID);
                        if (!string.IsNullOrEmpty(target.Content.ToString()))
                            continue;
      
                        target.Content = string.Format(
                            "Translate {0} to {1}.", originalContent, pair.Key);
                        manager.SaveContent(target);
                    }
                    System.Threading.Thread.CurrentThread.CurrentUICulture = backupCulture;
                }
            }
        }
    }


    4. Open your project Global.asax file, and modify the lines as below( we are adding a new event handler to Application_Start.

    void Application_Start(object sender, EventArgs e) 
     {
         Telerik.Cms.Engine.ContentManager.Executed += TranslateContent.Translate;
     
     }

    The code will create copies of all content items you are going to create. For the existing items you could change the CommandName if check to "UpdateContent" and you have to get for editing each of the items you have already created.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Priya Ramesh
    Priya Ramesh avatar
    26 posts
    Registered:
    23 Jul 2009
    28 Apr 2010
    Link to this post
    I added Content using the code you gave. Now I get an error whenever I edit a content 

     

    Server Error in '/' Application.

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       Telerik.Cms.Engine.StagedContent.Publish(IVersionItem item) +613
       Telerik.Cms.Engine.ContentManager.SaveContent(IContent content, ContentStatus status) +314
       Telerik.Cms.Engine.WebControls.Admin.ContentItemEdit`1.SaveContent() +1211
       Telerik.Cms.Engine.WebControls.Admin.ContentItemEdit`1.Button_Command(Object sender, CommandEventArgs e) +62
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +108
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
    
Register for webinar
3 posts, 0 answered