Reputation: 16512
I'm trying to change the culture on a ASP.NET Web API 2
but I can't figure out which function to override
I have tried ExecuteAsync
and Initialize
but the application uses the English resources no matter what.
This is how I set the culture
protected void SetCulture(string cultureName)
{
// Validate culture name
cultureName = CultureHelper.GetImplementedCulture(cultureName);
// Modify current thread's cultures
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
protected override void Initialize(HttpControllerContext controllerContext)
{
if (controllerContext.Request.Headers.AcceptLanguage != null &&
controllerContext.Request.Headers.AcceptLanguage.Count > 0)
{
string language = controllerContext.Request.Headers.AcceptLanguage.First().Value;
SetCulture(language);
}
base.Initialize(controllerContext);
}
and here the resources is always in English. I have put a watch on Thread.CurrentThread.CurrentCulture
and it says fr
private void SendConfirmationEmail(string userId)
{
...
UserManager.SendEmail(userId,
Resources.Resources.ConfirmAccount,
Resources.Resources.PleaseClickToConfirmAccount
+ callbackUrl);
}
How can we set the current culture?
EDIT
I have the same problem after setting the culture in the Global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpRequest currentRequest = HttpContext.Current.Request;
if (currentRequest.Headers.AllKeys.Contains("Accept-Language"))
{
string culture = currentRequest.Headers["Accept-Language"].ToString();
SetCulture(culture);
}
}
The Thread.CurrentThread.CurrentCulture
equals fr
. I don't understand why the resources are still in English!
EDIT 2
I created resource files in the properties of the Web API project and now it works.
So the problem is with the resources.dll.
I don't understand why the API can't choose the good resources file with the resources.dll. Maybe the resources.fr.resx is not with the dll.
Upvotes: 5
Views: 4536
Reputation: 16512
I figured out the problem.
When you compile a project with resources, it creates a folder for each culture with a .dll
So I had to copy those folders too. Not only resources.dll
because this is only the default language. That's why no matter what was the culture, my resources were in English.
So to use resources.dll
.dll
Note: you don't have to add a reference for each culture, only your default one.
Upvotes: 6