C
C#7mo ago
gio735

Unable to get localization to get to work in MVC .net8

I'm trying to implement localization using cookies, but for some reason no matter what .AspNetCore.Culture equals it always uses default resx file value of cookie is c%3Dka-GE%7Cuic%3Dka-GE or c%3Den-US%7Cuic%3Den-US Program.cs
builder.Services.AddLocalization();
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { "en-US", "ka-GE" };
options.SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
var defaultCookieRequestProvider =
options.RequestCultureProviders.FirstOrDefault(rcp =>
rcp.GetType() == typeof(CookieRequestCultureProvider));
if (defaultCookieRequestProvider != null)
options.RequestCultureProviders.Remove(defaultCookieRequestProvider);

options.RequestCultureProviders = new List<IRequestCultureProvider>()
{
new CookieRequestCultureProvider()
};
});
builder.Services.AddLocalization();
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { "en-US", "ka-GE" };
options.SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
var defaultCookieRequestProvider =
options.RequestCultureProviders.FirstOrDefault(rcp =>
rcp.GetType() == typeof(CookieRequestCultureProvider));
if (defaultCookieRequestProvider != null)
options.RequestCultureProviders.Remove(defaultCookieRequestProvider);

options.RequestCultureProviders = new List<IRequestCultureProvider>()
{
new CookieRequestCultureProvider()
};
});
4 Replies
gio735
gio735OP7mo ago
to access elements I'm using {FileName}.{ItemName} format
lycian
lycian7mo ago
I'm almost posivite to don't have to url encode/decode cookie values. That might be the issue if the values are actually c%3Dka-GE%7Cuic%3Dka-GE instead of c=ka-GE|uic=ka-GE
gio735
gio735OP7mo ago
To create cookie and assign value, I'm using method almost same as shown in Microsoft documentation
public IActionResult SetLanguage(string culture = "en-US", string returnUrl = "/")
{
Console.WriteLine( WebText.Culture);
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);

return LocalRedirect(returnUrl);
}
public IActionResult SetLanguage(string culture = "en-US", string returnUrl = "/")
{
Console.WriteLine( WebText.Culture);
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);

return LocalRedirect(returnUrl);
}
right now I changed value of cookie manually and refreshed the page, but it's same, also would replacing it with $"c={culture}|uic={culture}" be appropriate? and well... even like that, it changes to %3D and %7C when written in cookie this doesn't really make sense um, setting Designer.cs culture solves it, so was I perhaps supposed to manually read cookie and assign it?
lycian
lycian7mo ago
That I'm not sure about. #web might know the correct way to do it
Want results from more Discord servers?
Add your server