Creating items in different languages

Creating items in different languages is very simple using Glass.Mapper.

For this tutorial I have created an additional language Afrikaans (af-ZA), the default language is English (EN).

For this tutorial we will be using the following model, notice that it has a property called Language with the type Sitecore.Globalization.Language, this property is used by Glass.Mapper to determine the intended language of the item.

        [SitecoreType(TemplateId = "{24C53E69-DD16-40FA-A6A3-9B276635E20B}", AutoMap = true)]
        public class InsertModel
        {
            public virtual Guid Id { get; set; }
            public virtual Language Language { get; set; }
            public virtual string Name { get; set; }
            public virtual string Title { get; set; }
        }

In this first example we grab the parent item in the default language (en) and creates a new child item in the second language (af-ZA).

            var context = new SitecoreContext();
            //get the parent item in the default language (en)
            var parent = context.GetCurrentItem<InsertModel>();
            
            //Create the new item model
            var newItem = new InsertModel();
            newItem.Name = "MyNewItem";
            newItem.Title = "Language specific title";
            newItem.Language = LanguageManager.GetLanguage("af-ZA");
          
            //save the item back to Sitecore
            service.Create(current, newItem);

If your model does not have a property that defines a language then the item is created in the same language as the parent.

In the second example below we grab an existing item from Sitecore, update the title and language and save it back to Sitecore. This is a very quick way to duplicate data across different Sitecore languages:

            var context = new SitecoreContext();
            //get the current item in the default language (en)
            var item = context.GetCurrentItem<InsertModel>();
            
            //update the language specific fields
            item.Title = "Language specific title";
            //update the language so it saves in our second language
            item.Language = LanguageManager.GetLanguage("af-ZA");

            service.Save(item);

You can see from this tutorial how simple it is to use Glass.Mapper to create items in different languages.