+1-888-365-2779
Try Now
More in this section

Forums / Sitefinity SDK / Modifying content items for a specific language

Modifying content items for a specific language

5 posts, 1 answered
  1. David
    David avatar
    34 posts
    Registered:
    26 Feb 2010
    19 Aug 2011
    Link to this post
    I used the online example for creating and modifying content items (http://www.sitefinity.com/40/help/developers-guide/sitefinity-essentials-modules-generic-content-modifying-content-items.html), and they work well. I also want to create/modify a content item and then set a translation for that content.

    For instance, I want to create a content block called "Instructions". I first create the content block in English and modify it using Fluent. I would also like to programmatically add a translation for "Instructions" for French (using Fluent or the Native API). 

    Is this possible? I've tried a few things (setting the culture), but I'm getting nowhere. 
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    25 Aug 2011
    Link to this post
    Hello David,

    Localizable fields are of type LString in Sitefinity. This type allows you to easily set the values for all of your available cultures without the need to change the current culture. Quick sample bellow:
    var definedFrontendLanguages = AppSettings.CurrentSettings.DefinedFrontendLanguages;
    Guid newsItemId= fluent.NewsItem().CreateNew().Do(c =>
     {
         foreach(CultureInfo language indefinedFrontendLanguages)
         {
             //content is set as bellow
             c.GetString("Content")[language] = "something";
             //set title, all LString type properties as set using this approach. This creates language versiosn for the provided culture
             c.Title[language] = "title";
         }
     }).Publish().SaveAndContinue().Get().Id;


    Regards,
    Radoslav Georgiev
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. David
    David avatar
    34 posts
    Registered:
    26 Feb 2010
    25 Aug 2011
    Link to this post
    That only partially works. Here's my test code:

    var contentTitle = "Test content " + DateTime.Now.ToString();
     
    // create English version of the content
    Guid itemID = Guid.Empty;
    App.WorkWith().ContentItem().CreateNew()
        .Do(cI =>
        {
            itemID = cI.Id;
            cI.Title = contentTitle;
            cI.Content = "Original content (en)";
        })
        .Publish()
        .SaveChanges();
     
    // update English content
    App.WorkWith().ContentItem(itemID)
        .CheckOut()
        .Do(i =>
        {
            i.Content = "Update content (en)";
            i.LastModified = DateTime.UtcNow;
        })
        .CheckIn()
        .SaveChanges();
     
    // add French translation
    App.WorkWith().ContentItem(itemID)
        .CheckOut()
        .Do(c =>
                {
                    //c.Content["fr"] = "Initial French content";  // this also doesn't work
                    c.GetString("Content")["fr"] = "Initial French content";
                    c.Title["fr"] = contentTitle + "-fr";
                })
        .CheckIn()
        .SaveChanges();

    Once this executes, an English and French content block is added. The French content has the correct title, but the content is "Update content (en)", which is also the English content.

    Any insight on how to set the French content?
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    31 Aug 2011
    Link to this post
    Hello David,

    Try this:
    var contentTitle = "Test content " + DateTime.Now.ToString();
     
    // create English version of the content
    Guid itemID = Guid.Empty;
    App.WorkWith().ContentItem().CreateNew()
        .Do(cI =>
        {
            itemID = cI.Id;
            cI.Title["en"] = contentTitle;
            cI.GetString("Content")["en"] = "Original content (en)";
        })
        .Publish()
        .SaveChanges();
     
    // update English content
    App.WorkWith().ContentItem(itemID)
        .Do(i =>
        {
            i.GetString("Content")["en"] = "Update content (en)";
            i.LastModified = DateTime.UtcNow;
        })
        .Publish()
        .SaveChanges();
     
    // add French translation
    App.WorkWith().ContentItem(itemID)
        .Do(c =>
        {
            //c.Content["fr"] = "Initial French content";  // this also doesn't work
            c.GetString("Content")["fr"] = "Initial French content";
            c.Title["fr"] = contentTitle + "-fr";
        })
        .Publish()
        .SaveChanges();


    Regards,
    Radoslav Georgiev
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

    Answered
  5. David
    David avatar
    34 posts
    Registered:
    26 Feb 2010
    31 Aug 2011
    Link to this post
    That worked. 

    Thanks again,
    David
5 posts, 1 answered