C# 资源不';不加载当前文化
我正在尝试更改C# 资源不';不加载当前文化,c#,asp.net-web-api,C#,Asp.net Web Api,我正在尝试更改ASP.NET Web API 2 但我不知道该覆盖哪个函数 我尝试了ExecuteAsync和Initialize,但应用程序无论如何都会使用英文资源 这就是我设定文化的方式 protected void SetCulture(string cultureName) { // Validate culture name cultureName = CultureHelper.GetImplemente
ASP.NET Web API 2
但我不知道该覆盖哪个函数
我尝试了ExecuteAsync
和Initialize
,但应用程序无论如何都会使用英文资源
这就是我设定文化的方式
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
private void SendConfirmationEmail(string userId)
{
...
UserManager.SendEmail(userId,
Resources.Resources.ConfirmAccount,
Resources.Resources.PleaseClickToConfirmAccount
+ callbackUrl);
}
我们如何设置当前的文化
编辑
在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
。我不明白为什么资源仍然是英文的
编辑2
我在WebAPI项目的属性中创建了资源文件,现在它可以工作了
因此,问题在于resources.dll
我不明白为什么API不能用resources.dll选择好的资源文件。可能resources.fr.resx不在dll中 我解决了这个问题
当您使用资源编译项目时,它会使用.dll
为每个区域性创建一个文件夹
所以我也不得不复制那些文件夹。不仅仅是resources.dll
,因为这只是默认语言。这就是为什么无论是什么文化,我的资源都是英语
所以要使用resources.dll
您必须导入该引用
使用所有.dll
注意:您不必为每个区域性添加引用,只需添加默认区域性引用。您使用的是Web Hosting Web API吗?我不确定Web Hosting Web API是什么意思。你能解释一下你在IIS中和ASP.NET Web应用程序并排托管它吗?是的,我在IIS上也有一个ASP.NET MVC 5项目。我目前在本地主机上。请在Global.asax e中的受保护的无效应用程序_BeginRequest(对象发送方,EventArgs e)中设置区域性,查看是否有更改。