[英]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.