Skip to content

Commit

Permalink
Change language - more user friendly URL address #145
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Oct 26, 2021
1 parent d5511ad commit 43a0af3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public partial interface ILanguageService
/// <returns>Language</returns>
Task<Language> GetLanguageById(string languageId);

/// <summary>
/// Gets a language
/// </summary>
/// <param name="languageCode">Language code</param>
/// <returns>Language</returns>
Task<Language> GetLanguageByCode(string languageCode);

/// <summary>
/// Inserts a language
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ public virtual Task<Language> GetLanguageById(string languageId)
return _cacheBase.GetAsync(key, () => _languageRepository.GetByIdAsync(languageId));
}

/// <summary>
/// Gets a language
/// </summary>
/// <param name="languageCode">Language code</param>
/// <returns>Language</returns>
public virtual async Task<Language> GetLanguageByCode(string languageCode)
{
if (string.IsNullOrEmpty(languageCode))
throw new ArgumentNullException(nameof(languageCode));

var key = string.Format(CacheKey.LANGUAGES_BY_CODE, languageCode);
return await _cacheBase.GetAsync(key, async () => await _languageRepository.FirstOrDefaultAsync(x=>x.UniqueSeoCode.ToLowerInvariant() == languageCode.ToLowerInvariant()));
}

/// <summary>
/// Inserts a language
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public static partial class CacheKey
/// </remarks>
public static string LANGUAGES_BY_ID_KEY => "Grand.language.id-{0}";

/// <summary>
/// Key for caching
/// </summary>
/// <remarks>
/// {0} : language code
/// </remarks>
public static string LANGUAGES_BY_CODE => "Grand.language.code-{0}";

/// <summary>
/// Key for caching
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Web/Grand.Web/Controllers/CommonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public virtual IActionResult ExternalAuthenticationError(IEnumerable<string> err
[PublicStore(true)]
public virtual async Task<IActionResult> SetLanguage(
[FromServices] AppConfig config,
string langid, string returnUrl = "")
string langcode, string returnUrl = default)
{

var language = await _languageService.GetLanguageById(langid);
var language = await _languageService.GetLanguageByCode(langcode);
if (!language?.Published ?? false)
language = _workContext.WorkingLanguage;

Expand Down
3 changes: 2 additions & 1 deletion src/Web/Grand.Web/Endpoints/EndpointProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,10 @@ private void RegisterCommonRoute(IEndpointRouteBuilder endpointRouteBuilder, str

//change language
endpointRouteBuilder.MapControllerRoute("ChangeLanguage",
pattern + "changelanguage/{langid}",
pattern + "changelanguage/{langcode}",
new { controller = "Common", action = "SetLanguage" });


//change tax
endpointRouteBuilder.MapControllerRoute("ChangeTaxType",
pattern + "changetaxtype/{customertaxtype}",
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Grand.Web/Views/Shared/_Selector_Language.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@foreach (var lang in availableLanguages)
{
var className = lang.Id == workContext.WorkingLanguage.Id ? "active" : "";
<b-dropdown-item class="@className" href="@Url.RouteUrl("ChangeLanguage", new { langid = lang.Id, returnurl })">
<b-dropdown-item class="@className" href="@Url.RouteUrl("ChangeLanguage", new { langcode = lang.UniqueSeoCode, returnurl })">
@lang.Name
</b-dropdown-item>
}
Expand Down

0 comments on commit 43a0af3

Please sign in to comment.