ASP.net MVC 5 多语言:语言资源有时无法一致工作

问题描述 投票:0回答:2

我正在使用资源管理器在 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)'以重现此问题。

我做了提到的所有事情,但没有任何效果。 我可以尝试什么来使资源持续发挥作用?

谢谢。

c# asp.net model-view-controller race-condition multilingual
2个回答
0
投票

设置每种用户语言有多种方法。 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);
    

0
投票
我们能够找到问题的解决方案。需要更改所有 ResourceName.Designer.cs 文件:

/// <summary> /// Overrides the current thread's CurrentUICulture property for all /// resource lookups using this strongly typed resource class. /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] public static global::System.Globalization.CultureInfo Culture { get { return resourceCulture; } set { resourceCulture = value; } }
对此:

/// <summary> /// Overrides the current thread's CurrentUICulture property for all /// resource lookups using this strongly typed resource class. /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] public static global::System.Globalization.CultureInfo Culture { get { return System.Threading.Thread.CurrentThread.CurrentCulture; } set { System.Threading.Thread.CurrentThread.CurrentCulture = value; } }
    
© www.soinside.com 2019 - 2024. All rights reserved.