Skip to content

Commit 5609f6f

Browse files
author
Jan Nielsen
committed
revert weird approach and set default to store default language
1 parent c22f039 commit 5609f6f

File tree

3 files changed

+26
-60
lines changed

3 files changed

+26
-60
lines changed

src/Libraries/Nop.Core/Http/NopHttpDefaults.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,4 @@ public static partial class NopHttpDefaults
1919
/// Gets the name of a request item that stores the value that indicates whether the request is being redirected by the generic route transformer
2020
/// </summary>
2121
public static string GenericRouteInternalRedirect => "nop.RedirectFromGenericPathRoute";
22-
23-
/// <summary>
24-
/// Gets the name of a request item that stores the value with default language for sitemap.xml
25-
/// </summary>
26-
public static string ForcedSitemapXmlLanguage => "nop.ForcedSitemapXmlLanguage";
2722
}

src/Presentation/Nop.Web.Framework/Mvc/Routing/LanguageParameterTransformer.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Routing;
33
using Nop.Core;
4-
using Nop.Core.Http;
54
using Nop.Core.Infrastructure;
65
using Nop.Services.Localization;
76

@@ -39,11 +38,7 @@ public LanguageParameterTransformer(IHttpContextAccessor httpContextAccessor,
3938
/// <returns>The transformed value</returns>
4039
public string TransformOutbound(object value)
4140
{
42-
// first check if we have forced value for sitemap.xml
43-
if (_httpContextAccessor.HttpContext?.Items[NopHttpDefaults.ForcedSitemapXmlLanguage] is string forcedLang && !string.IsNullOrEmpty(forcedLang))
44-
return forcedLang;
45-
46-
// then try to get a language code from the route values
41+
//first try to get a language code from the route values
4742
var routeValues = _httpContextAccessor.HttpContext.Request.RouteValues;
4843
if (routeValues.TryGetValue(NopRoutingDefaults.RouteValue.Language, out var routeValue))
4944
{

src/Presentation/Nop.Web/Factories/SitemapModelFactory.cs

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
using Nop.Core.Domain.Localization;
1414
using Nop.Core.Domain.News;
1515
using Nop.Core.Domain.Seo;
16-
using Nop.Core.Domain.Stores;
1716
using Nop.Core.Domain.Topics;
1817
using Nop.Core.Events;
19-
using Nop.Core.Http;
2018
using Nop.Core.Infrastructure;
2119
using Nop.Services.Blogs;
2220
using Nop.Services.Catalog;
@@ -524,7 +522,7 @@ protected virtual async Task WriteSitemapUrlAsync(XmlWriter writer, SitemapUrlMo
524522
/// <param name="fullPath">The path and name of the sitemap file</param>
525523
/// <param name="id">Sitemap identifier</param>
526524
/// <returns>A task that represents the asynchronous operation</returns>
527-
protected virtual async Task GenerateAsync(string fullPath, int id = 0)
525+
protected virtual async Task GenerateAsync(string fullPath, int id = 0)
528526
{
529527
//generate all URLs for the sitemap
530528
var sitemapUrls = await GenerateUrlsAsync();
@@ -605,22 +603,6 @@ protected string GetLocalizedUrl(string currentUrl, Language lang)
605603
return new Uri(new Uri(scheme), localizedPath).ToString();
606604
}
607605

608-
/// <summary>
609-
/// Retrieves the list of languages for the given store that are not excluded in the sitemap XML settings,
610-
/// if SEO-friendly URLs for languages are enabled.
611-
/// </summary>
612-
/// <param name="store">The store to retrieve allowed languages for.</param>
613-
/// <returns>
614-
/// A list of <see cref="Language"/> if SEO-friendly URLs are enabled; otherwise, <c>null</c>.
615-
/// </returns>
616-
protected async Task<List<Language>> GetAllowedLanguagesAsync(Store store)
617-
{
618-
return _localizationSettings.SeoFriendlyUrlsForLanguagesEnabled
619-
? (await _languageService.GetAllLanguagesAsync(storeId: store.Id))
620-
.Where(lang => !_sitemapXmlSettings.DisallowLanguages.Contains(lang.Id))
621-
.ToList()
622-
: null;
623-
}
624606
#endregion
625607

626608
#region Methods
@@ -841,43 +823,27 @@ public virtual async Task<SitemapModel> PrepareSitemapModelAsync(SitemapPageMode
841823
/// </returns>
842824
public virtual async Task<SitemapXmlModel> PrepareSitemapXmlModelAsync(int id = 0)
843825
{
844-
var workingLanguage = await _workContext.GetWorkingLanguageAsync();
826+
var language = await _workContext.GetWorkingLanguageAsync();
845827
var store = await _storeContext.GetCurrentStoreAsync();
846-
847-
// get list of allowed languages (null if multilingual URLs are disabled)
848-
var languages = await GetAllowedLanguagesAsync(store);
849828

850-
// select current language if allowed, fallback to first allowed if needed
851-
var language = languages?.FirstOrDefault(lang => lang.Id == workingLanguage?.Id) ?? languages?.FirstOrDefault() ?? workingLanguage;
829+
var fileName = string.Format(NopSeoDefaults.SitemapXmlFilePattern, store.Id, language.Id, id);
830+
var fullPath = _nopFileProvider.GetAbsolutePath(NopSeoDefaults.SitemapXmlDirectory, fileName);
852831

853-
if (language.Id != workingLanguage.Id)
854-
_actionContextAccessor.ActionContext.HttpContext.Items[NopHttpDefaults.ForcedSitemapXmlLanguage] = language.UniqueSeoCode.ToLowerInvariant();
855-
856-
try
832+
if (_nopFileProvider.FileExists(fullPath) && _nopFileProvider.GetLastWriteTimeUtc(fullPath) > DateTime.UtcNow.AddHours(-_sitemapXmlSettings.RebuildSitemapXmlAfterHours))
857833
{
858-
var fileName = string.Format(NopSeoDefaults.SitemapXmlFilePattern, store.Id, language.Id, id);
859-
var fullPath = _nopFileProvider.GetAbsolutePath(NopSeoDefaults.SitemapXmlDirectory, fileName);
860-
861-
if (_nopFileProvider.FileExists(fullPath) && _nopFileProvider.GetLastWriteTimeUtc(fullPath) > DateTime.UtcNow.AddHours(-_sitemapXmlSettings.RebuildSitemapXmlAfterHours))
862-
{
863-
return new SitemapXmlModel { SitemapXmlPath = fullPath };
864-
}
865-
866-
//execute task with lock
867-
if (!await _locker.PerformActionWithLockAsync(
868-
fullPath,
869-
TimeSpan.FromSeconds(_sitemapXmlSettings.SitemapBuildOperationDelay),
870-
async () => await GenerateAsync(fullPath, id)))
871-
{
872-
throw new InvalidOperationException();
873-
}
874-
875834
return new SitemapXmlModel { SitemapXmlPath = fullPath };
876835
}
877-
finally
836+
837+
//execute task with lock
838+
if (!await _locker.PerformActionWithLockAsync(
839+
fullPath,
840+
TimeSpan.FromSeconds(_sitemapXmlSettings.SitemapBuildOperationDelay),
841+
async () => await GenerateAsync(fullPath, id)))
878842
{
879-
_actionContextAccessor.ActionContext.HttpContext.Items.Remove(NopHttpDefaults.ForcedSitemapXmlLanguage);
843+
throw new InvalidOperationException();
880844
}
845+
846+
return new SitemapXmlModel { SitemapXmlPath = fullPath };
881847
}
882848

883849
/// <summary>
@@ -920,8 +886,18 @@ var name when name.Equals(nameof(ProductTag), StringComparison.InvariantCultureI
920886
var store = await _storeContext.GetCurrentStoreAsync();
921887

922888
var updatedOn = dateTimeUpdatedOn ?? DateTime.UtcNow;
923-
var languages = await GetAllowedLanguagesAsync(store);
889+
var languages = _localizationSettings.SeoFriendlyUrlsForLanguagesEnabled
890+
? (await _languageService.GetAllLanguagesAsync(storeId: store.Id))
891+
.Where(lang => !_sitemapXmlSettings.DisallowLanguages.Contains(lang.Id)).ToList()
892+
: null;
924893

894+
// select store default language if allowed, fallback to first allowed if needed
895+
var workingLanguage = await _workContext.GetWorkingLanguageAsync();
896+
var language = languages?.FirstOrDefault(lang => lang.Id == store.DefaultLanguageId) ?? languages?.FirstOrDefault() ?? workingLanguage;
897+
898+
if (language.Id != workingLanguage.Id)
899+
url = GetLocalizedUrl(url, language);
900+
925901
if (languages == null || languages.Count == 1)
926902
return new SitemapUrlModel(url, new List<string>(), updateFreq, updatedOn);
927903

0 commit comments

Comments
 (0)