[英]Need help with creating a form for a multi-language structure in ASP.NET MVC
[英]ASP.net MVC 5 Multi-Language: language resources sometimes do not work consistently
我正在使用资源管理器在ASP.NET MVC 5应用程序中基于threadUI文化加载资源。我在Application_AcquireRequestState事件中设置了线程文化,当前文化保存了每个用户,它是由Web服务器在数据库中按以下代码加载的:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
string languageName = default(string);
CultureInfo ci = default(CultureInfo);
if (!User.Identity.IsAuthenticated)
{
languageName = Request.RequestContext.HttpContext.Session["_culture"] == null ?
Helper.ApplicationInformation.AppCulture.Name :
Request.RequestContext.HttpContext.Session["_culture"].ToString();
ci = CultureInfo.GetCultureInfo(languageName.ToUpper());
}
else
{
ci = CultureInfo.GetCultureInfo(Helper.User.Preferences.Language.Name);
}
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
Bridge.Resources.Global.Culture = ci;
Bridge.Resources.Login.Culture = ci;
Bridge.Resources.Search.Culture = ci;
Bridge.Resources.Workspace.Culture = ci;
}
这是在将区域性设置在不同的线程中时发生的,实际上是在2个或更多用户同时更改语言时,
我认为资源管理器中存在RaceCondition问题,导致使用当前线程的无效UI文化加载资源
我对此进行了研究,并找到以下相关链接:
ResouceManager有时会错误地加载ASP.Net MVC资源文件
ASP.NET MVC 5本地化资源冻结,即使更改CurrentThread.CurrentCulture也不会更改语言
多语言应用程序中的ASP.NET资源管理器RaceCondition
我尝试下载多语言示例,但这种情况也会发生,我从以下链接下载:
在action:index control:Home中添加“ ThreadSleep(4000)”以重现此问题。
我做了提到的一切,但没有任何效果。 我该如何尝试使资源持续工作?
谢谢。
有几种方法可以设置每种用户语言。 IIS服务器将会话保留20分钟。 您需要通过添加以下行来更改web.config文件中的时间:
<sessionState timeout="120" cookieless="AutoDetect">
// Change timeout to your preferred value
或者您需要去更改应用程序池中的设置。 对于我的项目,我使用cookie来保存用户语言选项。 Cookie设置的时间非常长。 例如我在Global.asax中使用了Application_BeginRequest
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["NAMEOFCOOKIE"];
if (cookie != null && cookie.Value != null)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
}
else
{
// FIND IN DB YOUR USER'S LANGUAGE AND SET IT
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
HttpCookie cookieNew = new HttpCookie("NAMEOFCOOKIE");
cookieNew.Value = "YOUR USERS'S DB VALUE";
Response.Cookies.Add(cookieNew);
}
}
}
}
而且,在使用资源时,请不要忘记一件事,您需要将其放在语言简称的文件名末尾。 例如,您具有默认文件MainPageLang.resx,并且需要英语和俄语,则必须添加文件MainPageLang.en.resx和MainPageLang.ru.resx。
“您的用户的数据库值”必须为“ en”或“ ru”。
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
HttpCookie cookieNew = new HttpCookie("NAMEOFCOOKIE");
cookieNew.Value = "YOUR USERS'S DB VALUE";
Response.Cookies.Add(cookieNew);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.