@@ -322,7 +322,7 @@ protected virtual async Task<IEnumerable<SitemapUrlModel>> GetBlogPostUrlsAsync(
322322
323323 return await blogPosts
324324 . Where ( p => p . IncludeInSitemap )
325- . SelectAwait ( async post => await PrepareLocalizedSitemapUrlAsync ( post , post . CreatedOnUtc ) )
325+ . SelectAwait ( async post => await PrepareLocalizedSitemapUrlAsync ( post , post . CreatedOnUtc , languageId : post . LanguageId ) )
326326 . ToListAsync ( ) ;
327327 }
328328
@@ -539,88 +539,6 @@ protected virtual string GetLocalizedUrl(string currentUrl, Language lang)
539539 return new Uri ( new Uri ( scheme ) , localizedPath ) . ToString ( ) ;
540540 }
541541
542- /// <summary>
543- /// Return localized urls
544- /// </summary>
545- /// <param name="entity">An entity which supports slug</param>
546- /// <param name="dateTimeUpdatedOn">A time when URL was updated last time</param>
547- /// <param name="updateFreq">How often to update url</param>
548- /// <returns>A task that represents the asynchronous operation</returns>
549- protected virtual async Task < SitemapUrlModel > PrepareLocalizedSitemapUrlAsync < TEntity > ( TEntity entity ,
550- DateTime ? dateTimeUpdatedOn = null ,
551- UpdateFrequency updateFreq = UpdateFrequency . Weekly ) where TEntity : BaseEntity , ISlugSupported
552- {
553- var isSingleLanguageEntity = entity is BlogPost or NewsItem ;
554- var url = await _nopUrlHelper
555- . RouteGenericUrlAsync ( entity , await GetHttpProtocolAsync ( ) , ensureTwoPublishedLanguages : ! isSingleLanguageEntity ) ;
556-
557- var store = await _storeContext . GetCurrentStoreAsync ( ) ;
558-
559- var updatedOn = dateTimeUpdatedOn ?? DateTime . UtcNow ;
560- var languages = _localizationSettings . SeoFriendlyUrlsForLanguagesEnabled
561- ? await _languageService . GetAllLanguagesAsync ( storeId : store . Id )
562- : null ;
563-
564- if ( languages == null || languages . Count == 1 )
565- return new SitemapUrlModel ( url , new List < string > ( ) , updateFreq , updatedOn ) ;
566-
567- if ( isSingleLanguageEntity )
568- {
569- var languageId = entity is BlogPost blogPost ? blogPost . LanguageId : ( entity is NewsItem newsItem ? newsItem . LanguageId : 0 ) ;
570- if ( await _languageService . GetLanguageByIdAsync ( languageId ) is not Language language || ! language . Published )
571- return new SitemapUrlModel ( url , new List < string > ( ) , updateFreq , updatedOn ) ;
572-
573- var localizedUrl = await _nopUrlHelper
574- . RouteGenericUrlAsync ( entity , await GetHttpProtocolAsync ( ) , languageId : languageId , ensureTwoPublishedLanguages : false ) ;
575- localizedUrl = GetLocalizedUrl ( url , language ) ;
576- return new SitemapUrlModel ( localizedUrl , new List < string > ( ) , updateFreq , updatedOn ) ;
577- }
578-
579- //return list of localized urls
580- var localizedUrls = await languages
581- . SelectAwait ( async lang =>
582- {
583- var currentUrl = await _nopUrlHelper . RouteGenericUrlAsync ( entity , await GetHttpProtocolAsync ( ) , languageId : lang . Id ) ;
584- return GetLocalizedUrl ( currentUrl , lang ) ;
585- } )
586- . Where ( value => ! string . IsNullOrEmpty ( value ) )
587- . ToListAsync ( ) ;
588-
589- return new SitemapUrlModel ( url , localizedUrls , updateFreq , updatedOn ) ;
590- }
591-
592- /// <summary>
593- /// Return localized urls
594- /// </summary>
595- /// <param name="routeName">Route name</param>
596- /// <param name="dateTimeUpdatedOn">A time when URL was updated last time</param>
597- /// <param name="updateFreq">How often to update url</param>
598- /// <returns>A task that represents the asynchronous operation</returns>
599- protected virtual async Task < SitemapUrlModel > PrepareLocalizedSitemapUrlAsync ( string routeName ,
600- DateTime ? dateTimeUpdatedOn = null ,
601- UpdateFrequency updateFreq = UpdateFrequency . Weekly )
602- {
603- var url = _nopUrlHelper . RouteUrl ( routeName , null , await GetHttpProtocolAsync ( ) ) ;
604-
605- var store = await _storeContext . GetCurrentStoreAsync ( ) ;
606-
607- var updatedOn = dateTimeUpdatedOn ?? DateTime . UtcNow ;
608- var languages = _localizationSettings . SeoFriendlyUrlsForLanguagesEnabled
609- ? await _languageService . GetAllLanguagesAsync ( storeId : store . Id )
610- : null ;
611-
612- if ( languages == null || languages . Count == 1 )
613- return new SitemapUrlModel ( url , new List < string > ( ) , updateFreq , updatedOn ) ;
614-
615- //return list of localized urls
616- var localizedUrls = await languages
617- . Select ( lang => GetLocalizedUrl ( url , lang ) )
618- . Where ( value => ! string . IsNullOrEmpty ( value ) )
619- . ToListAsync ( ) ;
620-
621- return new SitemapUrlModel ( url , localizedUrls , updateFreq , updatedOn ) ;
622- }
623-
624542 #endregion
625543
626544 #region Methods
@@ -859,5 +777,91 @@ public virtual async Task<SitemapXmlModel> PrepareSitemapXmlModelAsync(int id =
859777 return new SitemapXmlModel { SitemapXmlPath = fullPath } ;
860778 }
861779
780+ /// <summary>
781+ /// Return localized sitemap URL models
782+ /// </summary>
783+ /// <param name="routeName">Route name</param>
784+ /// <param name="dateTimeUpdatedOn">A time when URL was updated last time</param>
785+ /// <param name="updateFreq">How often to update URL</param>
786+ /// <returns>
787+ /// A task that represents the asynchronous operation
788+ /// The task result contains the localized sitemap URL models
789+ /// </returns>
790+ public virtual async Task < SitemapUrlModel > PrepareLocalizedSitemapUrlAsync ( string routeName ,
791+ DateTime ? dateTimeUpdatedOn = null , UpdateFrequency updateFreq = UpdateFrequency . Weekly )
792+ {
793+ var url = _nopUrlHelper . RouteUrl ( routeName , null , await GetHttpProtocolAsync ( ) ) ;
794+
795+ var store = await _storeContext . GetCurrentStoreAsync ( ) ;
796+
797+ var updatedOn = dateTimeUpdatedOn ?? DateTime . UtcNow ;
798+ var languages = _localizationSettings . SeoFriendlyUrlsForLanguagesEnabled
799+ ? await _languageService . GetAllLanguagesAsync ( storeId : store . Id )
800+ : null ;
801+
802+ if ( languages == null || languages . Count == 1 )
803+ return new SitemapUrlModel ( url , new List < string > ( ) , updateFreq , updatedOn ) ;
804+
805+ //return list of localized urls
806+ var localizedUrls = await languages
807+ . Select ( lang => GetLocalizedUrl ( url , lang ) )
808+ . Where ( value => ! string . IsNullOrEmpty ( value ) )
809+ . ToListAsync ( ) ;
810+
811+ return new SitemapUrlModel ( url , localizedUrls , updateFreq , updatedOn ) ;
812+ }
813+
814+ /// <summary>
815+ /// Return localized sitemap URL models
816+ /// </summary>
817+ /// <param name="entity">An entity which supports slug</param>
818+ /// <param name="dateTimeUpdatedOn">A time when URL was updated last time</param>
819+ /// <param name="updateFreq">How often to update URL</param>
820+ /// <param name="languageId">Language id; pass when URL for this entity should be used in one language</param>
821+ /// <returns>
822+ /// A task that represents the asynchronous operation
823+ /// The task result contains the localized sitemap URL models
824+ /// </returns>
825+ public virtual async Task < SitemapUrlModel > PrepareLocalizedSitemapUrlAsync < TEntity > ( TEntity entity ,
826+ DateTime ? dateTimeUpdatedOn = null , UpdateFrequency updateFreq = UpdateFrequency . Weekly , int ? languageId = null )
827+ where TEntity : BaseEntity , ISlugSupported
828+ {
829+ var url = await _nopUrlHelper
830+ . RouteGenericUrlAsync ( entity , await GetHttpProtocolAsync ( ) , ensureTwoPublishedLanguages : languageId is null ) ;
831+
832+ var store = await _storeContext . GetCurrentStoreAsync ( ) ;
833+
834+ var updatedOn = dateTimeUpdatedOn ?? DateTime . UtcNow ;
835+ var languages = _localizationSettings . SeoFriendlyUrlsForLanguagesEnabled
836+ ? await _languageService . GetAllLanguagesAsync ( storeId : store . Id )
837+ : null ;
838+
839+ if ( languages == null || languages . Count == 1 )
840+ return new SitemapUrlModel ( url , new List < string > ( ) , updateFreq , updatedOn ) ;
841+
842+ if ( languageId . HasValue )
843+ {
844+ if ( await _languageService . GetLanguageByIdAsync ( languageId . Value ) is not Language language || ! language . Published )
845+ return new SitemapUrlModel ( url , new List < string > ( ) , updateFreq , updatedOn ) ;
846+
847+ var localizedUrl = await _nopUrlHelper
848+ . RouteGenericUrlAsync ( entity , await GetHttpProtocolAsync ( ) , languageId : languageId , ensureTwoPublishedLanguages : false ) ;
849+ localizedUrl = GetLocalizedUrl ( url , language ) ;
850+ return new SitemapUrlModel ( localizedUrl , new List < string > ( ) , updateFreq , updatedOn ) ;
851+ }
852+
853+ //return list of localized urls
854+ var localizedUrls = await languages
855+ . SelectAwait ( async lang =>
856+ {
857+ var currentUrl = await _nopUrlHelper . RouteGenericUrlAsync ( entity , await GetHttpProtocolAsync ( ) , languageId : lang . Id ) ;
858+ return GetLocalizedUrl ( currentUrl , lang ) ;
859+ } )
860+ . Where ( value => ! string . IsNullOrEmpty ( value ) )
861+ . ToListAsync ( ) ;
862+
863+ return new SitemapUrlModel ( url , localizedUrls , updateFreq , updatedOn ) ;
864+ }
865+
862866 #endregion
863867}
0 commit comments