99using SmartStore . Data ;
1010using SmartStore . Services . Catalog ;
1111using SmartStore . Core . Events ;
12+ using SmartStore . Services . Localization ;
1213using SmartStore . Services . Media ;
1314using SmartStore . Services . Seo ;
1415using SmartStore . Utilities ;
@@ -36,6 +37,8 @@ public partial class ImportManager : IImportManager
3637 private readonly IRepository < Picture > _rsPicture ;
3738 private readonly IRepository < ProductPicture > _rsProductPicture ;
3839 private readonly IRepository < UrlRecord > _rsUrlRecord ;
40+ private readonly ILanguageService _languageService ;
41+ private readonly ILocalizedEntityService _localizedEntityService ;
3942
4043 public ImportManager (
4144 IProductService productService ,
@@ -50,7 +53,9 @@ public ImportManager(
5053 IRepository < ProductManufacturer > rsProductManufacturer ,
5154 IRepository < Picture > rsPicture ,
5255 IRepository < ProductPicture > rsProductPicture ,
53- IRepository < UrlRecord > rsUrlRecord )
56+ IRepository < UrlRecord > rsUrlRecord ,
57+ ILanguageService languageService ,
58+ ILocalizedEntityService localizedEntityService )
5459 {
5560 this . _productService = productService ;
5661 this . _categoryService = categoryService ;
@@ -65,6 +70,8 @@ public ImportManager(
6570 this . _rsProductPicture = rsProductPicture ;
6671 this . _rsUrlRecord = rsUrlRecord ;
6772 this . _rsPicture = rsPicture ;
73+ this . _languageService = languageService ;
74+ this . _localizedEntityService = localizedEntityService ;
6875 }
6976
7077 public virtual string CreateTextReport ( ImportResult result )
@@ -199,8 +206,23 @@ public virtual async Task<ImportResult> ImportProductsFromExcelAsync(
199206 _rsProduct . Context . AutoDetectChangesEnabled = false ;
200207 }
201208
209+ // ===========================================================================
210+ // 3.) Import Localizations
211+ // ===========================================================================
212+ if ( batch . Any ( x => x . ContainsKey ( "CategoryIds" ) ) )
213+ {
214+ try
215+ {
216+ await ProcessLocalizations ( batch , result ) ;
217+ }
218+ catch ( Exception ex )
219+ {
220+ result . AddError ( ex , segmenter . CurrentSegment , "ProcessLocalizations" ) ;
221+ }
222+ }
223+
202224 // ===========================================================================
203- // 3 .) Import product category mappings
225+ // 4 .) Import product category mappings
204226 // ===========================================================================
205227 if ( batch . Any ( x => x . ContainsKey ( "CategoryIds" ) ) )
206228 {
@@ -215,7 +237,7 @@ public virtual async Task<ImportResult> ImportProductsFromExcelAsync(
215237 }
216238
217239 // ===========================================================================
218- // 4 .) Import product manufacturer mappings
240+ // 5 .) Import product manufacturer mappings
219241 // ===========================================================================
220242 if ( batch . Any ( x => x . ContainsKey ( "ManufacturerIds" ) ) )
221243 {
@@ -230,7 +252,7 @@ public virtual async Task<ImportResult> ImportProductsFromExcelAsync(
230252 }
231253
232254 // ===========================================================================
233- // 5 .) Import product picture mappings
255+ // 6 .) Import product picture mappings
234256 // ===========================================================================
235257 if ( batch . Any ( x => x . ContainsKey ( "Picture1" ) || x . ContainsKey ( "Picture2" ) || x . ContainsKey ( "Picture3" ) ) )
236258 {
@@ -486,6 +508,79 @@ private async Task<int> ProcessSlugs(ICollection<ImportRow<Product>> batch, Impo
486508 return t ;
487509 }
488510
511+ private async Task < int > ProcessLocalizations ( ICollection < ImportRow < Product > > batch , ImportResult result )
512+ {
513+ //_rsProductManufacturer.AutoCommitEnabled = false;
514+
515+ //string lastInserted = null;
516+
517+ var languages = _languageService . GetAllLanguages ( true ) ;
518+
519+ foreach ( var row in batch )
520+ {
521+
522+ Product product = null ;
523+
524+ //get product
525+ try
526+ {
527+ object key ;
528+
529+ // try get by int ID
530+ if ( row . TryGetValue ( "Id" , out key ) && key . ToString ( ) . ToInt ( ) > 0 )
531+ {
532+ product = _productService . GetProductById ( key . ToString ( ) . ToInt ( ) ) ;
533+ }
534+
535+ // try get by SKU
536+ if ( product == null && row . TryGetValue ( "SKU" , out key ) )
537+ {
538+ product = _productService . GetProductBySku ( key . ToString ( ) ) ;
539+ }
540+
541+ // try get by GTIN
542+ if ( product == null && row . TryGetValue ( "Gtin" , out key ) )
543+ {
544+ product = _productService . GetProductByGtin ( key . ToString ( ) ) ;
545+ }
546+ }
547+ catch ( Exception ex )
548+ {
549+ result . AddWarning ( ex . Message , row . GetRowInfo ( ) , "ProcessLocalizations Product" ) ;
550+ }
551+
552+ foreach ( var lang in languages )
553+ {
554+ string localizedName = row . GetValue < string > ( "Name[" + lang . UniqueSeoCode + "]" ) ;
555+ string localizedShortDescription = row . GetValue < string > ( "ShortDescription[" + lang . UniqueSeoCode + "]" ) ;
556+ string localizedFullDescription = row . GetValue < string > ( "FullDescription[" + lang . UniqueSeoCode + "]" ) ;
557+
558+ if ( localizedName . HasValue ( ) )
559+ {
560+ _localizedEntityService . SaveLocalizedValue ( product , x => x . Name , localizedName , lang . Id ) ;
561+ }
562+ if ( localizedShortDescription . HasValue ( ) )
563+ {
564+ _localizedEntityService . SaveLocalizedValue ( product , x => x . ShortDescription , localizedShortDescription , lang . Id ) ;
565+ }
566+ if ( localizedFullDescription . HasValue ( ) )
567+ {
568+ _localizedEntityService . SaveLocalizedValue ( product , x => x . FullDescription , localizedFullDescription , lang . Id ) ;
569+ }
570+ }
571+ }
572+
573+ // commit whole batch at once
574+ var t = await _rsProductManufacturer . Context . SaveChangesAsync ( ) ;
575+
576+ // Perf: notify only about LAST insertion and update
577+ //if (lastInserted != null)
578+ // _eventPublisher.EntityInserted(lastInserted);
579+
580+ return t ;
581+ }
582+
583+
489584 private async Task < int > ProcessProductCategories ( ICollection < ImportRow < Product > > batch , ImportResult result )
490585 {
491586 _rsProductCategory . AutoCommitEnabled = false ;
0 commit comments