简体   繁体   English

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

[英]ASP.net MVC 5 Multi-Language: language resources sometimes do not work consistently

I'm using the resource manager to load resources based on threadUI culture in an ASP.NET MVC 5 application.i set thread culture in Application_AcquireRequestState Event, current culture saves per user, it's loaded by a web server in DataBase as below Code: 我正在使用资源管理器在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;
    }

it's happening when setting the culture in a different thread, actually when 2 user or more changes languages at the same time, 这是在将区域性设置在不同的线程中时发生的,实际上是在2个或更多用户同时更改语言时,

I think there is a RaceCondition problem in resource manager that cause loading resources with invalid UI Culture for the current thread 我认为资源管理器中存在RaceCondition问题,导致使用当前线程的无效UI文化加载资源

I did a research about this and find the following relevant links: 我对此进行了研究,并找到以下相关链接:

ASP.Net MVC resource files are sometimes incorrectly loaded by the ResouceManager ResouceManager有时会错误地加载ASP.Net MVC资源文件

ASP.NET MVC 5 localization resources freeze and do not change language despite changing CurrentThread.CurrentCulture ASP.NET MVC 5本地化资源冻结,即使更改CurrentThread.CurrentCulture也不会更改语言

ASP.NET Resource Manager RaceCondition in Multilingual Application 多语言应用程序中的ASP.NET资源管理器RaceCondition

I try to download example for multi-language but it also happens, I download from the following link: 我尝试下载多语言示例,但这种情况也会发生,我从以下链接下载:

MultiLanguageExample MultiLanguageExample

add 'ThreadSleep(4000)' in action:index control:Home to reproduce this issue. 在action:index control:Home中添加“ ThreadSleep(4000)”以重现此问题。

I did everything mentioned but nothing work. 我做了提到的一切,但没有任何效果。 what can I try to make resources to work consistently? 我该如何尝试使资源持续工作?

thanks. 谢谢。

There are several ways to set each user language. 有几种方法可以设置每种用户语言。 IIS Server keep Sessions for 20 minutes. IIS服务器将会话保留20分钟。 You need to change time in web.config file by adding row: 您需要通过添加以下行来更改web.config文件中的时间:

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

Or you need to go and change settings in Application Pools. 或者您需要去更改应用程序池中的设置。 For my project I use cookies to save user language option. 对于我的项目,我使用cookie来保存用户语言选项。 Cookies set to be avaliable for very very long time. Cookie设置的时间非常长。 For example I've used in Global.asax Application_BeginRequest 例如我在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);
                    }
                }
            }
        }

And don't forget one thing when you are using resources you need to put it the end of filename your language short name. 而且,在使用资源时,请不要忘记一件事,您需要将其放在语言简称的文件名末尾。 For example you have default file MainPageLang.resx and you need English and Russian, you have to add files MainPageLang.en.resx and MainPageLang.ru.resx. 例如,您具有默认文件MainPageLang.resx,并且需要英语和俄语,则必须添加文件MainPageLang.en.resx和MainPageLang.ru.resx。

"YOUR USERS'S DB VALUE" must be "en" or "ru". “您的用户的数据库值”必须为“ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM