ASP.net MVC 5多语言:语言资源有时不能始终如一地工作

wh6knrhe  于 2023-10-21  发布在  .NET
关注(0)|答案(2)|浏览(86)

我正在使用资源管理器在ASP.NET MVC 5应用程序中基于threadUI区域性加载资源。我在Application_AcquireRequestState Event中设置线程区域性,当前区域性按用户保存,它由数据库中的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区域性无效的资源
我对此做了调查,发现了以下相关链接:
ASP.Net MVC resource files are sometimes incorrectly loaded by the ResouceManager
ASP.NET MVC 5 localization resources freeze and do not change language despite changing CurrentThread.CurrentCulture
ASP.NET Resource Manager RaceCondition in Multilingual Application
我试图下载多语言的例子,但它也发生,我从以下链接下载:
MultiLanguageExample
在action:index control:Home中添加“ThreadSleep(4000)”以重现此问题。
我做了所有提到的事情,但没有任何工作。我可以尝试做些什么来使资源一致地工作?
谢谢.

mklgxw1f

mklgxw1f1#

有几种方法可以设置每种用户语言。IIS服务器保持会话20分钟。您需要通过添加行来更改web.config文件中的时间:

<sessionState timeout="120" cookieless="AutoDetect">
// Change timeout to your preferred value

或者,您需要在应用程序中更改设置。对于我的项目,我使用cookie来保存用户语言选项。Cookies设置为可使用很长时间。例如,我在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);
vngu2lb8

vngu2lb82#

我们能够找到解决问题的办法。需要在所有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;
    }
}

相关问题