Modifying content items for a specific language

Posted by Community Admin on 05-Aug-2018 22:21

Modifying content items for a specific language

All Replies

Posted by Community Admin on 19-Aug-2011 00:00

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. 

Posted by Community Admin on 25-Aug-2011 00:00

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 >>

Posted by Community Admin on 25-Aug-2011 00:00

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?

Posted by Community Admin on 31-Aug-2011 00:00

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 >>

Posted by Community Admin on 31-Aug-2011 00:00

That worked. 

Thanks again,
David

This thread is closed