3

How to add DisplayName in another language version from code?

createCategoryCommand.Process(context, category.ParentCatalogId, category.Name, category.DisplayName, category.Description);

commands don't accept language parameter and I cannot find any other method for that.

2 Answers 2

5

You will need to save category first and then provide localizations for it. Let's say you already have Category categoryEntity. You then need to do the following steps:

// getting localization entity for your category. it will automatically create new if there is no existing one
LocalizationEntity localizationEntity = await this.localizeEntityPropertyCommand.GetLocalizationEntity(context.CommerceContext, categoryEntity);

// preparing translations
List<Parameter> localizations = new List<Parameter>
                                    {
                                        new Parameter
                                            {
                                                Key = "en", Value = "Default Display Name"
                                            },
                                        new Parameter
                                            {
                                                Key = "fr-FR", Value = "French Display Name"
                                            }
                                    };

// adding localizations for DisplayName property
localizationEntity.AddOrUpdatePropertyValue("DisplayName", localizations);

// saving localization entity to database
await this.persistEntityPipeline.Run(new PersistEntityArgument(localizationEntity), context);

// this is needed if initial category didn't have LocalizedEntityComponent to link category with localization entity
if (!categoryEntity.HasComponent<LocalizedEntityComponent>())
{
    categoryEntity.SetComponent(new LocalizedEntityComponent(localizationEntity.Id, localizationEntity.Name));

    // saving category entity to database
    await this.persistEntityPipeline.Run(new PersistEntityArgument(categoryEntity), context);
}
1
  • This answer is no longer valid as of XC 9.2, Please see @Hao Liu's answer below for the correct approach moving forwards
    – Rob Earlam
    Commented Sep 17, 2019 at 4:43
5

There are some changes in Experience Commerce 9.2, copied below from one of the related support ticket:

In previous versions of Sitecore Commerce, the LocalizedEntityComponent was used to know which localization entity was related to an entity. In 9.2 the localization entity ID was changed to a composition of the commerce entity ID for a localization entity and the entity ID, e.g. The localization entity ID for an entity with ID 'Entity-SellableItem-12345' would then be 'Entity-LocalizationEntity-12345'. This then makes the LocalizationEntityComponent obsolete, as the localization entity can be inferred from the entity ID.

To explain with the above linked stackexchange post, the following:

LocalizationEntity localizationEntity = await this.localizeEntityPropertyCommand.GetLocalizationEntity(context.CommerceContext, categoryEntity);

now generates the localizationEntity with an ID following the above mentioned new format. This then renders the following code not necessary:

// this is needed if initial category didn't have LocalizedEntityComponent to link category with localization entity

if (!categoryEntity.HasComponent()) { categoryEntity.SetComponent(new LocalizedEntityComponent(localizationEntity.Id, localizationEntity.Name)); // saving category entity to database await this.persistEntityPipeline.Run(new PersistEntityArgument(categoryEntity), context); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.