Skip to content

Commit

Permalink
fix language change problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ismcagdas committed Jul 24, 2024
1 parent c097aee commit dc015a5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Globalization;
using System.Linq;
using Abp;
using Abp.AspNetCore.Mvc.Controllers;
using Abp.AspNetCore.Mvc.Extensions;
using Abp.Auditing;
using Abp.Extensions;
using Abp.Timing;
using Abp.Web.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using IUrlHelper = Abp.Web.Http.IUrlHelper;

namespace AbpCompanyName.AbpProjectName.Web.Controllers
{
public class LocalizationController : AbpController
{
private readonly IUrlHelper _urlHelper;

public LocalizationController(IUrlHelper urlHelper)
{
_urlHelper = urlHelper;
}

[DisableAuditing]
public virtual ActionResult ChangeCulture(string cultureName, string returnUrl = "")
{
if (!IsValidCultureCode(cultureName))
{
throw new AbpException("Unknown language: " + cultureName + ". It must be a valid culture!");
}

var cookieValue = CookieRequestCultureProvider.MakeCookieValue(
new RequestCulture(cultureName, cultureName)
);

Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
cookieValue,
new CookieOptions
{
Expires = Clock.Now.AddYears(2),
HttpOnly = true
}
);

if (Request.IsAjaxRequest())
{
return Json(new AjaxResponse());
}

if (string.IsNullOrWhiteSpace(returnUrl))
{
return LocalRedirect("/");
}

var escapedReturnUrl = Uri.EscapeDataString(returnUrl);
var localPath = _urlHelper.LocalPathAndQuery(escapedReturnUrl, Request.Host.Host, Request.Host.Port);
if (!string.IsNullOrWhiteSpace(localPath))
{
var unescapedLocalPath = Uri.UnescapeDataString(localPath);
if (Url.IsLocalUrl(unescapedLocalPath))
{
return LocalRedirect(unescapedLocalPath);
}
}

return LocalRedirect("/");
}

private static bool IsValidCultureCode(string cultureCode)
{
if (cultureCode.IsNullOrWhiteSpace())
{
return false;
}

try
{
return CultureInfo.GetCultures(CultureTypes.AllCultures)
.Any(e => e.Name.ToLowerInvariant() == cultureCode.ToLowerInvariant());
}
catch (CultureNotFoundException)
{
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{
if (language.Name != Model.CurrentLanguage.Name)
{
<li><a href="@Url.Action("ChangeCulture", "AbpLocalization")?cultureName=@(language.Name)&returnUrl=@(Model.CurrentUrl)"><div class="@language.Icon"></div> @language.DisplayName</a></li>
<li><a href="@Url.Action("ChangeCulture", "Localization")?cultureName=@(language.Name)&returnUrl=@(Model.CurrentUrl)"><div class="@language.Icon"></div> @language.DisplayName</a></li>
}
}
</ul>
Expand Down

0 comments on commit dc015a5

Please sign in to comment.