Reputation: 636
I have a ASP.NET CORE 3.1 c# Web Application with multilanguage support. The multilanguage support uses Portable Object Localization. I'm in need to get the current language via Javascript. It is stored in a cookie setted using the following method :
//GET /SetLanguage
[HttpGet]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1), IsEssential= true }
);
return LocalRedirect(returnUrl);
}
Hoe can I do it? Thanks,
Upvotes: 1
Views: 497
Reputation: 4279
I think you can use the following code.
var current = document.cookie.split('; ').reduce((r, v) => {
const parts = v.split('=')
return parts[0] === '.AspNetCore.Culture'? decodeURIComponent(parts[1]) : r
}, '').split('|')[0].split('=')[1]
Upvotes: 2