Created
November 17, 2019 14:08
-
-
Save brijbaroda/4a27598987a72929069eccec05f633c1 to your computer and use it in GitHub Desktop.
Sitecore Commerce - Localizing Category - LocalizeCategory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using POC.Localization.Pipelines.Arguments; | |
using Microsoft.Extensions.Logging; | |
using Sitecore.Commerce.Core; | |
using Sitecore.Commerce.Core.Commands; | |
using Sitecore.Commerce.Plugin.BusinessUsers; | |
using Sitecore.Commerce.Plugin.Catalog; | |
using Sitecore.Commerce.Plugin.EntityVersions; | |
using Sitecore.Framework.Pipelines; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml.Linq; | |
namespace Plugin.Localization.Pipelines.Blocks | |
{ | |
public class LocalizeCategory : PipelineBlock<LocalizationPipelineArgument, LocalizationPipelineArgument, CommercePipelineExecutionContext> | |
{ | |
private readonly GetCategoryCommand _getCategoryCommand; | |
private readonly FindEntityCommand _findEntityCommand; | |
private readonly PersistEntityPipeline _persistEntityPipeline; | |
private readonly LocalizeEntityPropertyCommand _localizeEntityPropertyCommand; | |
private readonly IFindEntityPipeline _findEntityPipeline; | |
public LocalizeCategory(CommerceCommander commander, GetCategoryCommand getCategoryCommand, CreateCategoryCommand createCategoryCommand, AssociateCategoryToParentCommand associateCategoryToParentCommand, FindEntityCommand findEntityCommand, CreateRelationshipPipeline createRelationshipPipeline, AddEntityVersionCommand addEntityVersionCommand, LocalizeEntityPropertyCommand localizeEntityPropertyCommand, PersistEntityPipeline persistEntityPipeline, IFindEntityPipeline findEntityPipeline) | |
{ | |
this._getCategoryCommand = getCategoryCommand; | |
this._findEntityCommand = findEntityCommand; | |
_localizeEntityPropertyCommand = localizeEntityPropertyCommand; | |
_persistEntityPipeline = persistEntityPipeline; | |
_findEntityPipeline = findEntityPipeline; | |
} | |
public override async Task<LocalizationPipelineArgument> Run(LocalizationPipelineArgument arg, CommercePipelineExecutionContext context) | |
{ | |
context.Logger.LogInformation("---------Localization for category started------------"); | |
string catalogName = "Adventure Works Catalog"; | |
string categoryName = "Departments"; | |
await LocalizeCategoryData(context, catalogName, categoryName); | |
return arg; | |
} | |
private async Task<CommerceEntity> LocalizeCategoryData(CommercePipelineExecutionContext context, string catalogName, string categoryName, string parentCategoryId = null) | |
{ | |
try | |
{ | |
string fullCatalogName = GenerateFullCatalogName(catalogName); | |
var catalog = await _findEntityPipeline.Run(new FindEntityArgument(typeof(Catalog), fullCatalogName, 1), context); | |
string categoryId = GenerateFullCategoryId(catalogName, categoryName); | |
string categoryFriendlyName = $"{catalogName}-{categoryName}"; | |
//Check if catalog with given name already exists before trying to create a new one | |
Category categoryEntity = await _findEntityCommand.Process(context.CommerceContext, typeof(Category), categoryId, null, false) as Category; | |
if (categoryEntity == null) | |
{ | |
categoryEntity = await _getCategoryCommand.Process(context.CommerceContext, categoryId); | |
} | |
if (categoryEntity != null) | |
{ | |
LocalizationEntity localizationEntity = await this._localizeEntityPropertyCommand.GetLocalizationEntity(context.CommerceContext, categoryEntity); | |
// preparing translations | |
List<Parameter> localizations = new List<Parameter> | |
{ | |
new Parameter | |
{ | |
Key = "en", Value = $"{ categoryEntity.Name}_English" | |
}, | |
new Parameter | |
{ | |
Key = "de-DE", Value = $"{ categoryEntity.Name}_de-DE" | |
}, | |
new Parameter | |
{ | |
Key = "fr-FR", Value = $"{ categoryEntity.Name}_fr-FR" | |
}, | |
new Parameter | |
{ | |
Key = "ja-JP", Value = $"{ categoryEntity.Name}_ja-JA" | |
} | |
}; | |
// adding localizations for DisplayName property | |
localizationEntity.AddOrUpdatePropertyValue("DisplayName", localizations); | |
List<Parameter> localizationsdesc = new List<Parameter> | |
{ | |
new Parameter | |
{ | |
Key = "en", Value = $"{ categoryEntity.Name}desc_English" | |
}, | |
new Parameter | |
{ | |
Key = "fr-FR", Value = $"{ categoryEntity.Name}desc_fr-FR" | |
}, | |
new Parameter | |
{ | |
Key = "ja-JP", Value = $"{ categoryEntity.Name}desc_ja-JA" | |
}, | |
new Parameter | |
{ | |
Key = "de-DE", Value = $"{ categoryEntity.Name}desc_de-DE" | |
} | |
}; | |
localizationEntity.AddOrUpdatePropertyValue("Description", localizationsdesc); | |
// saving localization entity to database | |
await this._persistEntityPipeline.Run(new PersistEntityArgument(localizationEntity), context); | |
// saving category entity to database | |
//await this._persistEntityPipeline.Run(new PersistEntityArgument(categoryEntity), context); | |
return categoryEntity; | |
} | |
return categoryEntity; | |
} | |
catch (Exception ex) | |
{ | |
context.Logger.LogInformation($"Localization: Error during getcategory" + ex.Message); | |
return null; | |
} | |
} | |
private static string GenerateFullCategoryId(string catalogName, string categoryName) | |
{ | |
return $"{CommerceEntity.IdPrefix<Category>()}{catalogName}-{categoryName}"; | |
} | |
private static string GenerateFullCatalogName(string catalogName) | |
{ | |
return $"{CommerceEntity.IdPrefix<Catalog>()}{catalogName}"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment