资源不加载当前文化 [英] Resources don't load current culture
问题描述
我试图改变一个的ASP.NET Web API 2
文化
但我不能找出哪些功能覆盖
I'm trying to change the culture on a ASP.NET Web API 2
but I can't figure out which function to override
我曾尝试 ExecuteAsync
和初始化
,但应用程序使用英语资源,不管是什么。
I have tried ExecuteAsync
and Initialize
but the application uses the English resources no matter what.
这是我如何设置区域性
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);
}
和这里的资源始终是英语。我已经派人监视 Thread.CurrentThread.CurrentCulture
和它说 FR
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?
修改
我在制定培养后同样的问题的Global.asax
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);
}
}
的 Thread.CurrentThread.CurrentCulture
等于 FR
。我不明白为什么资源还是英文!
The Thread.CurrentThread.CurrentCulture
equals fr
. I don't understand why the resources are still in English!
编辑2
我创建在Web API项目的属性的资源文件,现在它的作品。
I created resource files in the properties of the Web API project and now it works.
所以,问题与resources.dll。
So the problem is with the resources.dll.
我不明白为什么API不能选择与resources.dll良好的资源文件。也许resources.fr.resx不与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.
推荐答案
我想通了这个问题。
在编译资源的一个项目,它与每个文化创建一个文件夹中的的.dll
When you compile a project with resources, it creates a folder for each culture with a .dll
所以我不得不复制这些文件夹了。不仅 resources.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.
因此,要使用 resources.dll
- 您必须导入参考
- 复制所有的
的.dll
bin文件夹中的所有文件夹的文化
- you have to import that reference
- copy all your culture folders in the bin folder with all the
.dll
注意:您不必添加每个文化参照,只有你的默认的
Note: you don't have to add a reference for each culture, only your default one.
这篇关于资源不加载当前文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!